From 532fddfc6d8cf47b03459993b750ebdfeb7294bf Mon Sep 17 00:00:00 2001 From: Felix Mosheev <9304194+felixmosh@users.noreply.github.com> Date: Sun, 23 Feb 2025 18:03:26 +0200 Subject: [PATCH] feat: make start, stop & restart public methods # Conflicts: # src/index.ts --- src/index.ts | 94 +++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/src/index.ts b/src/index.ts index a53dfa1..990a00a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,48 +43,72 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance { } } + public restartServer(): void { + console.log('Restarting app...'); + + this.stopServer(); + + this.startServer(); + } + + public startServer(cb?: () => void): void { + const { args, nodeArgs, cwd, env } = this.options; + if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.'); + + const child = fork(this._entrypoint, args, { + execArgv: nodeArgs, + stdio: 'inherit', + cwd, + env, + }); + + setTimeout(() => { + this.worker = child; + cb?.(); + }, 0); + } + + public stopServer() { + const signal = getSignal(this.options.signal); + if (this.worker?.pid) { + process.kill(this.worker.pid, signal); + } + }; + + apply = (compiler: Compiler): void => { + compiler.hooks.afterEmit.tapAsync( + { name: 'RunScriptPlugin' }, + this.afterEmit, + ); + }; + private _enableRestarting(): void { if (this.options.keyboard) { process.stdin.setEncoding('utf8'); process.stdin.on('data', (data: string) => { if (data.trim() === 'rs') { - this._restartServer(); + this.restartServer(); } }); } } - private _restartServer(): void { - console.log('Restarting app...'); - - this._stopServer(); - - this._startServer(); - } - private afterEmit = (compilation: Compilation, cb: () => void): void => { if (this.worker && this.worker.connected && this.worker?.pid) { if (this.options.autoRestart) { - this._restartServer(); + this.restartServer(); cb(); return; } - this._stopServer(); + this.stopServer(); cb(); return; } - this.startServer(compilation, cb); + this._startServer(compilation, cb); }; - apply = (compiler: Compiler): void => { - compiler.hooks.afterEmit.tapAsync( - { name: 'RunScriptPlugin' }, - this.afterEmit, - ); - }; - - private startServer = (compilation: Compilation, cb: () => void): void => { + private _startServer = (compilation: Compilation, cb: () => void): void => { const { assets, compiler } = compilation; const { options } = this; let name; @@ -100,9 +124,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance { name = names[0]; if (names.length > 1) { console.log( - `More than one entry built, selected ${name}. All names: ${names.join( - ' ', - )}`, + `More than one entry built, selected ${name}. All names: ${names.join(' ')}`, ); } } @@ -111,30 +133,6 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance { } this._entrypoint = `${compiler.options.output.path}/${name}`; - this._startServer(cb); - }; - - private _startServer(cb?: () => void): void { - const { args, nodeArgs, cwd, env } = this.options; - if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.'); - - const child = fork(this._entrypoint, args, { - execArgv: nodeArgs, - stdio: 'inherit', - cwd, - env, - }); - - setTimeout(() => { - this.worker = child; - cb?.() - }, 0); - } - - private _stopServer() { - const signal = getSignal(this.options.signal); - if (this.worker?.pid) { - process.kill(this.worker.pid, signal); - } + this.startServer(cb); }; }