From 451393c92d9e01a684c4187dea86c940c64c6664 Mon Sep 17 00:00:00 2001 From: Gavin1012 Date: Wed, 4 May 2022 17:29:40 +0800 Subject: [PATCH] fixed 7795931 from https://gitee.com/gavin1012_hw/ark_ts2abc/pulls/222 keep watching alive to speed up IDE watching Signed-off-by: Gavin1012 Change-Id: I4780bd6a6da4e89f9667f4cacf8f54c49b12ad69 --- ts2panda/src/cmdOptions.ts | 16 +++++++ ts2panda/src/index.ts | 97 +++++++++++++++++++++++++++++++------- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/ts2panda/src/cmdOptions.ts b/ts2panda/src/cmdOptions.ts index 854cf32635..9844d6bd94 100644 --- a/ts2panda/src/cmdOptions.ts +++ b/ts2panda/src/cmdOptions.ts @@ -27,6 +27,7 @@ const ts2pandaOptions = [ { name: 'dump-assembly', alias: 'a', type: Boolean, defaultValue: false, description: "dump assembly to file." }, { name: 'debug', alias: 'd', type: Boolean, defaultValue: false, description: "compile with debug info." }, { name: 'debug-add-watch', alias: 'w', type: String, lazyMultiple: true, defaultValue: [], description: "watch expression and abc file path in debug mode." }, + { name: 'keep-persistent-watch', alias: 'k', type: String, lazyMultiple: true, defaultValue: [], description: "keep persistent watch on js file with watched expression." }, { name: 'show-statistics', alias: 's', type: String, lazyMultiple: true, defaultValue: "", description: "show compile statistics(ast, histogram, hoisting, all)." }, { name: 'output', alias: 'o', type: String, defaultValue: "", description: "set output file." }, { name: 'timeout', alias: 't', type: Number, defaultValue: 0, description: "js to abc timeout threshold(unit: seconds)." }, @@ -90,6 +91,21 @@ export class CmdOptions { this.options["debug-add-watch"] = watchArgs; } + static getKeepWatchFile(): string[] { + if (!this.options) { + return []; + } + return this.options["keep-persistent-watch"]; + } + + static isKeepWatchMode(args: string[]): boolean { + return args.length == 2 && args[0] == "start"; + } + + static isStopWatchMode(args: string[]): boolean { + return args.length == 2 && args[0] == "stop"; + } + static isModules(): boolean { if (!this.options) { return false; diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 2491b89dca..6c849cac8e 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -154,17 +154,21 @@ function getDtsFiles(libDir: string): string[] { return dtsFiles; } -function parseWatch(files: string[]): string { +const stopWatchingStr = "####"; +const watchAbcFileTimeOut = 5000; +const watchFileName = "watch_expressions"; + +function updateWatchedFile() { let watchArgs = CmdOptions.getAddWatchArgs(); let ideIputStr = watchArgs[0]; if (watchArgs.length != 2 || !isBase64Str(ideIputStr)) { - throw new Error("Incorrect args' format or not enter base64 string in watch mode."); + throw new Error("Incorrect args' format or not enter base64 string in watch mode"); } let fragmentSep = "\\n"; let originExpre = Buffer.from(ideIputStr, 'base64').toString(); let expressiones = originExpre.split(fragmentSep); - let jsFileName = watchArgs[1] + path.sep + "watch_expres.js"; - let abcFileName = watchArgs[1] + path.sep + "watch_expres.abc"; + let jsFileName = watchArgs[1] + path.sep + watchFileName + ".js"; + let abcFileName = watchArgs[1] + path.sep + watchFileName + ".abc"; let writeFlag: Boolean = false; for (let index = 0; index < expressiones.length; index++) { let expreLine = expressiones[index].trim(); @@ -177,8 +181,64 @@ function parseWatch(files: string[]): string { } } } - files.unshift(jsFileName); - return abcFileName; + if (CmdOptions.isAssemblyMode()) { + return; + } + + fs.watchFile(abcFileName, { persistent: true, interval: 50 }, (curr, prev) => { + if (+curr.mtime <= +prev.mtime) { + fs.unwatchFile(jsFileName); + throw new Error("watched abc file has not been initialized"); + } + let base64data = fs.readFileSync(abcFileName); + let watchResStr = Buffer.from(base64data).toString('base64'); + console.log(watchResStr); + fs.unwatchFile(abcFileName); + process.exit(); + }); + setTimeout(() => { + fs.unwatchFile(jsFileName); + fs.unwatchFile(abcFileName); + fs.unlinkSync(jsFileName); + fs.unlinkSync(abcFileName); + throw new Error("watchFileServer has not been initialized"); + }, watchAbcFileTimeOut); +} + +function convertWatchExpression(jsfileName: string, parsed: ts.ParsedCommandLine | undefined) { + let files: string[] = parsed.fileNames; + files.unshift(jsfileName); + CmdOptions.setWatchArgs(['','']); + main(files.concat(CmdOptions.getIncludedFiles()), parsed.options); +} + +function keepWatchingFiles(filePath: string, parsed: ts.ParsedCommandLine | undefined) { + let jsFileName = filePath + path.sep + watchFileName + ".js"; + let abcFileName = filePath + path.sep + watchFileName + ".abc"; + if (fs.existsSync(jsFileName)) { + console.log("watchFileServer has been initialized"); + return; + } + fs.writeFileSync(jsFileName, "initJsFile\n"); + convertWatchExpression(jsFileName, parsed); + + fs.watchFile(jsFileName, { persistent: true, interval: 50 }, (curr, prev) => { + if (+curr.mtime <= +prev.mtime) { + throw new Error("watched js file has not been initialized"); + } + if (fs.readFileSync(jsFileName).toString() == stopWatchingStr) { + fs.unwatchFile(jsFileName); + console.log("stopWatchingSuccess"); + return; + } + convertWatchExpression(jsFileName, parsed); + }); + console.log("startWatchingSuccess"); + + process.on("exit", () => { + fs.unlinkSync(jsFileName); + fs.unlinkSync(abcFileName); + }); } namespace Compiler { @@ -209,19 +269,22 @@ function run(args: string[], options?: ts.CompilerOptions): void { } } try { - let files: string[] = parsed.fileNames; - let abcFileName: string = ''; - if (CmdOptions.isWatchMode()) { - abcFileName = parseWatch(files); + let keepWatchArgs = CmdOptions.getKeepWatchFile(); + if (keepWatchArgs.length != 0) { + if (CmdOptions.isKeepWatchMode(keepWatchArgs)) { + keepWatchingFiles(CmdOptions.getKeepWatchFile()[1], parsed); + } else if (CmdOptions.isStopWatchMode(keepWatchArgs)) { + fs.writeFileSync(CmdOptions.getKeepWatchFile()[1] + path.sep + watchFileName + ".js", stopWatchingStr); + } else { + throw new Error("Incorrect args' format for keep watching expression mode"); + } + return; } - main(files.concat(CmdOptions.getIncludedFiles()), parsed.options); - if (CmdOptions.isWatchMode() && !CmdOptions.isAssemblyMode()) { - process.on('exit', () => { - let base64data = fs.readFileSync(abcFileName); - let watchResStr = Buffer.from(base64data).toString('base64'); - console.log(watchResStr); - }); + if (CmdOptions.isWatchMode()) { + updateWatchedFile(); + return; } + main(parsed.fileNames.concat(CmdOptions.getIncludedFiles()), parsed.options); } catch (err) { if (err instanceof diag.DiagnosticError) { let diagnostic = diag.getDiagnostic(err.code); -- Gitee