UNPKG

@villedemontreal/scripting

Version:
96 lines 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WatchScript = void 0; const general_utils_1 = require("@villedemontreal/general-utils"); const _ = require("lodash"); const path = require("path"); const src_1 = require("../src"); const configs_1 = require("../src/config/configs"); const notifier = 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 projectName = require(configs_1.configs.projectRoot + '/package.json').namae; 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(); this.logger.info(stdoutDataClean); if (this.options.dn) { return; } let error = false; if (errorRegEx.test(stdoutDataClean)) { error = true; notifier.notify({ title: projectName, message: 'incremental compilation error', icon: path.normalize(`${__dirname}/../../../assets/notifications/error.png`), sound: false, }); } else if (compilationCompletetRegEx.test(stdoutDataClean)) { if (!ignoreNextCompilationComplete) { notifier.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); } }; // eslint-disable-next-line no-constant-condition while (true) { try { await this.invokeShellCommand('node', [ `${configs_1.configs.projectRoot}/node_modules/typescript/lib/tsc.js`, '--project', configs_1.configs.projectRoot, '--watch', '--pretty', ], { outputHandler, }); } catch (err) { // ========================================== // @see https://stackoverflow.com/a/25444766/843699 // ========================================== if (_.isString(err) && err.indexOf('3221225786') >= 0) { this.logger.error('Exiting...'); process.exit(0); } 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