keep watching alive to speed up IDE watching <wanggang203@huawei.com>

Signed-off-by: Gavin1012 <wanggang203@huawei.com>
Change-Id: I4780bd6a6da4e89f9667f4cacf8f54c49b12ad69
This commit is contained in:
Gavin1012
2022-05-04 17:29:40 +08:00
parent 491ece23d1
commit 77959315de
2 changed files with 96 additions and 17 deletions
+16
View File
@@ -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;
+80 -17
View File
@@ -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 {
@@ -210,19 +270,22 @@ function run(args: string[], options?: ts.CompilerOptions): void {
}
}
try {
let files: string[] = parsed.fileNames;
let abcFileName: string = '';
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;
}
if (CmdOptions.isWatchMode()) {
abcFileName = parseWatch(files);
}
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);
});
updateWatchedFile();
return;
}
main(parsed.fileNames.concat(CmdOptions.getIncludedFiles()), parsed.options);
} catch (err) {
if (err instanceof diag.DiagnosticError) {
let diagnostic = diag.getDiagnostic(err.code);