diff --git a/package-lock.json b/package-lock.json index bef783f..df5c1dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -103,7 +103,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/@pushrocks/smartdelay/-/smartdelay-2.0.2.tgz", "integrity": "sha512-4xf6tMKwZcxBynKgXrM4SQKgeASfRvx43LUmR5DkStp26ZHAsarCXUdKJS6y8QIPygEOTOCP8we97JAcCzBuMg==", - "dev": true, "requires": { "@pushrocks/smartpromise": "^2.0.5" } @@ -168,9 +167,9 @@ } }, "@pushrocks/smartshell": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/@pushrocks/smartshell/-/smartshell-2.0.6.tgz", - "integrity": "sha512-D48KB3DDqLfMjOXGEutqJi+v3Z4RcWacu5BJXxUwrecvd6oetbKobfmNGxeHSQPmNGb7U3ISfKwV6c5T5EZkJg==", + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/@pushrocks/smartshell/-/smartshell-2.0.8.tgz", + "integrity": "sha512-OeGrJez0vSHe/E0auHNUqf7r0B3yS+pkVe6BuxdJemJx5LBRtcHuZnJCxU5pSLL8igzlTfR+jVpjKJXUMzCsrA==", "requires": { "@pushrocks/smartpromise": "^2.0.5", "@types/which": "^1.3.1", diff --git a/package.json b/package.json index ff96087..c181549 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "author": "Lossless GmbH", "license": "MIT", "scripts": { - "test": "node ./cli.ts.js test", + "test": "node ./cli.ts.js test --timeout 10000", "test2": "(tstest test/)", "build": "(tsbuild)", "format": "(gitzone format)" @@ -24,7 +24,8 @@ "dependencies": { "@gitzone/tsrun": "^1.1.13", "@pushrocks/smartcli": "^3.0.6", - "@pushrocks/smartshell": "^2.0.6", + "@pushrocks/smartdelay": "^2.0.2", + "@pushrocks/smartshell": "^2.0.8", "filewatcher": "^3.0.1" } } diff --git a/test/test.ts b/test/test.ts index de53171..77284fa 100644 --- a/test/test.ts +++ b/test/test.ts @@ -6,16 +6,18 @@ let testTsWatchInstance: tswatch.TsWatch; tap.test('should create a valid TsWatch instance', async () => { testTsWatchInstance = new tswatch.TsWatch({ filePathToWatch: process.cwd(), - commandToExecute: 'npm -v' + commandToExecute: 'npm -v', + timeout: 1000 }); }); tap.test('should start the tswatch instance', async () => { + testTsWatchInstance.start(); console.log('test executed'); }); tap.test('should run abitrary commands', async () => { - + }); tap.start(); diff --git a/ts/tswatch.classes.tswatch.ts b/ts/tswatch.classes.tswatch.ts index 66a495b..db4e1c4 100644 --- a/ts/tswatch.classes.tswatch.ts +++ b/ts/tswatch.classes.tswatch.ts @@ -1,5 +1,11 @@ import * as plugins from './tswatch.plugins'; +export interface ITsWatchConstructorOptions { + filePathToWatch: string; + commandToExecute: string; + timeout?: number; +} + /** * handles the management of watching for foes */ @@ -9,15 +15,10 @@ export class TsWatch { }); private currentExecution: plugins.smartshell.IExecResultStreaming; private watcher = plugins.fileWatcher(); - private filePathToWatch: string; - private commandToExecute: string; + private options: ITsWatchConstructorOptions; - constructor(optionsArg: { - filePathToWatch: string, - commandToExecute: string - }) { - this.filePathToWatch = optionsArg.filePathToWatch; - this.commandToExecute = optionsArg.commandToExecute; + constructor(optionsArg: ITsWatchConstructorOptions) { + this.options = optionsArg; } /** @@ -25,8 +26,8 @@ export class TsWatch { */ public async start() { this.setupCleanup(); - console.log(`Looking at ${this.filePathToWatch} for changes`); - this.watcher.add(this.filePathToWatch); // __dirname refers to the directory of this very file + console.log(`Looking at ${this.options.filePathToWatch} for changes`); + this.watcher.add(this.options.filePathToWatch); // __dirname refers to the directory of this very file this.watcher.on('change', async (file, stat) => { console.log('Noticed change!'); if (!stat) { @@ -41,14 +42,14 @@ export class TsWatch { if (this.currentExecution) { process.kill(-this.currentExecution.childProcess.pid); } - this.currentExecution = await this.smartshellInstance.execStreaming(this.commandToExecute); + this.currentExecution = await this.smartshellInstance.execStreaming(this.options.commandToExecute); this.currentExecution = null; } /** * this method sets up a clean exit strategy */ - private setupCleanup() { + private async setupCleanup() { const cleanup = () => { if (this.currentExecution) { process.kill(-this.currentExecution.childProcess.pid); @@ -66,5 +67,14 @@ export class TsWatch { cleanup(); process.exit(0); }); + + // handle timeout + if (this.options.timeout) { + plugins.smartdelay.delayFor(this.options.timeout).then(() => { + console.log(`timed out afer ${this.options.timeout} milliseconds! exiting!`); + cleanup(); + process.exit(0); + }); + } } } diff --git a/ts/tswatch.cli.ts b/ts/tswatch.cli.ts index 8b6626e..55d1479 100644 --- a/ts/tswatch.cli.ts +++ b/ts/tswatch.cli.ts @@ -8,7 +8,15 @@ const tswatchCli = new plugins.smartcli.Smartcli(); tswatchCli.addCommand('test').subscribe(argvArg => { const tsWatch = new TsWatch({ filePathToWatch: paths.cwd, - commandToExecute: 'npm run test2' + commandToExecute: 'npm run test2', + timeout: (() => { + if (argvArg.timeout) { + console.log(`timeing out after ${argvArg.timeout}`); + return argvArg.timeout; + } else { + return null; + } + })() }); tsWatch.start(); }); diff --git a/ts/tswatch.plugins.ts b/ts/tswatch.plugins.ts index 0b46d7a..51cacee 100644 --- a/ts/tswatch.plugins.ts +++ b/ts/tswatch.plugins.ts @@ -3,9 +3,10 @@ export { path }; // @pushrocks scope import * as smartcli from '@pushrocks/smartcli'; +import * as smartdelay from '@pushrocks/smartdelay'; import * as smartshell from '@pushrocks/smartshell'; -export { smartshell, smartcli }; +export { smartdelay, smartshell, smartcli }; // Third Pary import * as fileWatcher from 'filewatcher';