@villedemontreal/scripting
Version:
Scripting core utilities
86 lines • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WatchScript = void 0;
const general_utils_1 = require("@villedemontreal/general-utils");
const path = require("path");
const src_1 = require("../src");
const configs_1 = require("../src/config/configs");
const execa_1 = require("execa");
const node_notifier_1 = require("node-notifier");
class WatchScript extends src_1.ScriptBase {
get name() {
return 'watch';
}
get description() {
return `Start Typescript incremental compilation. \
You can run this script in an external terminal and then debug your \
application in your IDE. When you have made some modifications and want \
to test them, you stop your application and restart it \
using the "Debug Locally - fast" launch configuration (if you are \
in VSCode) or \`run start --nc\`. No compilation is required at \
that point since the incremental compilation is already done by this script.`;
}
async configure(command) {
command.option(`--dn`, `Disable the visual notifications`);
}
async main() {
this.logger.info(`\n==========================================\n` +
`Starting incremental compilation...\n` +
`==========================================\n`);
const pkg = await Promise.resolve(`${path.join(configs_1.configs.projectRoot, 'package.json')}`).then(s => require(s));
const projectName = pkg.name;
let ignoreNextCompilationComplete = false;
const compilationCompletetRegEx = /(Compilation complete)|(Found 0 errors)/;
// eslint-disable-next-line no-control-regex
const errorRegEx = /(: error)|(error)/;
const outputHandler = (stdoutData, stderrData) => {
if (stdoutData) {
const stdoutDataClean = stdoutData.toString();
if (this.options.dn) {
return;
}
let error = false;
if (errorRegEx.test(stdoutDataClean)) {
error = true;
(0, node_notifier_1.notify)({
title: projectName,
message: 'incremental compilation error',
icon: path.normalize(`${__dirname}/../../../assets/notifications/error.png`),
sound: false,
});
}
else if (compilationCompletetRegEx.test(stdoutDataClean)) {
if (!ignoreNextCompilationComplete) {
(0, node_notifier_1.notify)({
title: projectName,
message: 'incremental compilation done',
icon: path.normalize(`${__dirname}/../../../assets/notifications/success.png`),
sound: false,
});
}
}
ignoreNextCompilationComplete = error && !compilationCompletetRegEx.test(stdoutDataClean);
}
if (stderrData && !stderrData.match(/^Debugger attached.(\n|\r\n)$/)) {
this.logger.error(stderrData);
}
};
while (true) {
try {
for await (const line of (0, execa_1.execa)({
preferLocal: true,
}) `tsc --project ${configs_1.configs.projectRoot} --watch --pretty`) {
const lineStr = line.toString();
console.log(lineStr);
outputHandler(lineStr, '');
}
}
catch (err) {
this.logger.error(`Error, restarting incremental compilation in a second : ${String(err)}`);
await general_utils_1.utils.sleep(1000);
}
}
}
}
exports.WatchScript = WatchScript;
//# sourceMappingURL=watch.js.map