fix(core): update
This commit is contained in:
3
ts/index.ts
Normal file
3
ts/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './tswatch.classes.tswatch';
|
||||
|
||||
import './tswatch.cli';
|
70
ts/tswatch.classes.tswatch.ts
Normal file
70
ts/tswatch.classes.tswatch.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import * as plugins from './tswatch.plugins';
|
||||
|
||||
/**
|
||||
* handles the management of watching for foes
|
||||
*/
|
||||
export class TsWatch {
|
||||
private smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
private currentExecution: plugins.smartshell.IExecResultStreaming;
|
||||
private watcher = plugins.fileWatcher();
|
||||
private filePathToWatch: string;
|
||||
private commandToExecute: string;
|
||||
|
||||
constructor(optionsArg: {
|
||||
filePathToWatch: string,
|
||||
commandToExecute: string
|
||||
}) {
|
||||
this.filePathToWatch = optionsArg.filePathToWatch;
|
||||
this.commandToExecute = optionsArg.commandToExecute;
|
||||
}
|
||||
|
||||
/**
|
||||
* start the file
|
||||
*/
|
||||
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
|
||||
this.watcher.on('change', async (file, stat) => {
|
||||
console.log('Noticed change!');
|
||||
if (!stat) {
|
||||
console.log('deleted');
|
||||
}
|
||||
this.updateCurrentExecution();
|
||||
});
|
||||
this.updateCurrentExecution();
|
||||
}
|
||||
|
||||
private async updateCurrentExecution() {
|
||||
if (this.currentExecution) {
|
||||
process.kill(-this.currentExecution.childProcess.pid);
|
||||
}
|
||||
this.currentExecution = await this.smartshellInstance.execStreaming(this.commandToExecute);
|
||||
this.currentExecution = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* this method sets up a clean exit strategy
|
||||
*/
|
||||
private setupCleanup() {
|
||||
const cleanup = () => {
|
||||
if (this.currentExecution) {
|
||||
process.kill(-this.currentExecution.childProcess.pid);
|
||||
}
|
||||
};
|
||||
process.on('exit', () => {
|
||||
console.log('');
|
||||
console.log('now exiting!');
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
process.on('SIGINT', () => {
|
||||
console.log('');
|
||||
console.log('ok! got SIGINT We are exiting! Just cleaning up to exit neatly :)');
|
||||
cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
}
|
16
ts/tswatch.cli.ts
Normal file
16
ts/tswatch.cli.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import * as plugins from './tswatch.plugins';
|
||||
import * as paths from './tswatch.paths';
|
||||
|
||||
import { TsWatch } from './tswatch.classes.tswatch';
|
||||
|
||||
const tswatchCli = new plugins.smartcli.Smartcli();
|
||||
|
||||
tswatchCli.addCommand('test').subscribe(argvArg => {
|
||||
const tsWatch = new TsWatch({
|
||||
filePathToWatch: paths.cwd,
|
||||
commandToExecute: 'npm run test2'
|
||||
});
|
||||
tsWatch.start();
|
||||
});
|
||||
|
||||
tswatchCli.startParse();
|
3
ts/tswatch.paths.ts
Normal file
3
ts/tswatch.paths.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import * as plugins from './tswatch.plugins';
|
||||
|
||||
export const cwd = process.cwd();
|
13
ts/tswatch.plugins.ts
Normal file
13
ts/tswatch.plugins.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import * as path from 'path';
|
||||
export { path };
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartcli from '@pushrocks/smartcli';
|
||||
import * as smartshell from '@pushrocks/smartshell';
|
||||
|
||||
export { smartshell, smartcli };
|
||||
|
||||
// Third Pary
|
||||
import * as fileWatcher from 'filewatcher';
|
||||
|
||||
export { fileWatcher };
|
Reference in New Issue
Block a user