| 1 | const fs = require("fs"); |
| 2 | const path = require("path"); |
| 3 | |
| 4 | const logDirPath = path.join( |
| 5 | __dirname, |
| 6 | "..", |
| 7 | "extensions", |
| 8 | ".continue-debug", |
| 9 | "logs", |
| 10 | ); |
| 11 | const logFilePath = path.join(logDirPath, "prompt.log"); |
| 12 | |
| 13 | // Ensure the log directory exists |
| 14 | if (!fs.existsSync(logDirPath)) { |
| 15 | fs.mkdirSync(logDirPath, { recursive: true }); |
| 16 | console.log("Created log directory at " + logDirPath); |
| 17 | } |
| 18 | |
| 19 | // Create the log file if it doesn't exist |
| 20 | if (!fs.existsSync(logFilePath)) { |
| 21 | fs.writeFileSync(logFilePath, ""); // Create an empty file synchronously |
| 22 | console.log("Created empty log file at " + logFilePath); |
| 23 | } |
| 24 | |
| 25 | console.log("Watching logs at " + logFilePath); |
| 26 | |
| 27 | // Set up the file watcher |
| 28 | try { |
| 29 | fs.watch(logFilePath, () => { |
| 30 | try { |
| 31 | // console.clear(); |
| 32 | const stream = fs.createReadStream(logFilePath); |
| 33 | stream.pipe(process.stdout); |
| 34 | stream.on("error", (err) => { |
| 35 | console.error("Error reading log file:", err.message); |
| 36 | }); |
| 37 | } catch (err) { |
| 38 | console.error("Error while handling file change:", err.message); |
| 39 | } |
| 40 | }); |
| 41 | } catch (err) { |
| 42 | console.error("Error setting up file watcher:", err.message); |
| 43 | console.log( |
| 44 | "You may need to restart the script after the extension generates the first log entry.", |
| 45 | ); |
| 46 | } |