Skip to content

Commit f82b36c

Browse files
authored
refactor: extract process.kill to stopServer method (#30)
1 parent 85c2592 commit f82b36c

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/index.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fork, ChildProcess } from 'child_process';
2-
import { Compiler, WebpackPluginInstance, Compilation } from 'webpack';
1+
import { ChildProcess, fork } from 'child_process';
2+
import { Compilation, Compiler, WebpackPluginInstance } from 'webpack';
33

44
export type RunScriptWebpackPluginOptions = {
55
autoRestart?: boolean;
@@ -48,21 +48,18 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
4848
process.stdin.setEncoding('utf8');
4949
process.stdin.on('data', (data: string) => {
5050
if (data.trim() === 'rs') {
51-
this._restartServer()
51+
this._restartServer();
5252
}
5353
});
5454
}
5555
}
5656

57-
private _restartServer():void {
57+
private _restartServer(): void {
5858
console.log('Restarting app...');
59-
if (this.worker?.pid) {
60-
const signal = getSignal(this.options.signal);
61-
process.kill(this.worker.pid, signal);
62-
}
63-
this._startServer((worker) => {
64-
this.worker = worker;
65-
});
59+
60+
this._stopServer();
61+
62+
this._startServer();
6663
}
6764

6865
private afterEmit = (compilation: Compilation, cb: () => void): void => {
@@ -72,10 +69,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
7269
cb();
7370
return;
7471
}
75-
const signal = getSignal(this.options.signal);
76-
if (signal) {
77-
process.kill(this.worker.pid, signal);
78-
}
72+
this._stopServer();
7973
cb();
8074
return;
8175
}
@@ -86,7 +80,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
8680
apply = (compiler: Compiler): void => {
8781
compiler.hooks.afterEmit.tapAsync(
8882
{ name: 'RunScriptPlugin' },
89-
this.afterEmit
83+
this.afterEmit,
9084
);
9185
};
9286

@@ -99,16 +93,16 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
9993
name = options.name;
10094
if (!assets[name]) {
10195
console.error(
102-
`Entry ${name} not found. Try one of: ${names.join(' ')}`
96+
`Entry ${name} not found. Try one of: ${names.join(' ')}`,
10397
);
10498
}
10599
} else {
106100
name = names[0];
107101
if (names.length > 1) {
108102
console.log(
109103
`More than one entry built, selected ${name}. All names: ${names.join(
110-
' '
111-
)}`
104+
' ',
105+
)}`,
112106
);
113107
}
114108
}
@@ -117,13 +111,10 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
117111
}
118112

119113
this._entrypoint = `${compiler.options.output.path}/${name}`;
120-
this._startServer((worker) => {
121-
this.worker = worker;
122-
cb();
123-
});
114+
this._startServer(cb);
124115
};
125116

126-
private _startServer(cb: (arg0: ChildProcess) => void): void {
117+
private _startServer(cb?: () => void): void {
127118
const { args, nodeArgs, cwd, env } = this.options;
128119
if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.');
129120

@@ -133,6 +124,17 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
133124
cwd,
134125
env,
135126
});
136-
setTimeout(() => cb(child), 0);
127+
128+
setTimeout(() => {
129+
this.worker = child;
130+
cb?.()
131+
}, 0);
137132
}
133+
134+
private _stopServer() {
135+
const signal = getSignal(this.options.signal);
136+
if (signal && (this.worker?.pid)) {
137+
process.kill(this.worker.pid, signal);
138+
}
139+
};
138140
}

0 commit comments

Comments
 (0)