Skip to content

Commit 532fddf

Browse files
committed
feat: make start, stop & restart public methods
# Conflicts: # src/index.ts
1 parent 9ac34d0 commit 532fddf

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

src/index.ts

+46-48
Original file line numberDiff line numberDiff line change
@@ -43,48 +43,72 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
4343
}
4444
}
4545

46+
public restartServer(): void {
47+
console.log('Restarting app...');
48+
49+
this.stopServer();
50+
51+
this.startServer();
52+
}
53+
54+
public startServer(cb?: () => void): void {
55+
const { args, nodeArgs, cwd, env } = this.options;
56+
if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.');
57+
58+
const child = fork(this._entrypoint, args, {
59+
execArgv: nodeArgs,
60+
stdio: 'inherit',
61+
cwd,
62+
env,
63+
});
64+
65+
setTimeout(() => {
66+
this.worker = child;
67+
cb?.();
68+
}, 0);
69+
}
70+
71+
public stopServer() {
72+
const signal = getSignal(this.options.signal);
73+
if (this.worker?.pid) {
74+
process.kill(this.worker.pid, signal);
75+
}
76+
};
77+
78+
apply = (compiler: Compiler): void => {
79+
compiler.hooks.afterEmit.tapAsync(
80+
{ name: 'RunScriptPlugin' },
81+
this.afterEmit,
82+
);
83+
};
84+
4685
private _enableRestarting(): void {
4786
if (this.options.keyboard) {
4887
process.stdin.setEncoding('utf8');
4988
process.stdin.on('data', (data: string) => {
5089
if (data.trim() === 'rs') {
51-
this._restartServer();
90+
this.restartServer();
5291
}
5392
});
5493
}
5594
}
5695

57-
private _restartServer(): void {
58-
console.log('Restarting app...');
59-
60-
this._stopServer();
61-
62-
this._startServer();
63-
}
64-
6596
private afterEmit = (compilation: Compilation, cb: () => void): void => {
6697
if (this.worker && this.worker.connected && this.worker?.pid) {
6798
if (this.options.autoRestart) {
68-
this._restartServer();
99+
this.restartServer();
69100
cb();
70101
return;
71102
}
72-
this._stopServer();
103+
this.stopServer();
73104
cb();
74105
return;
75106
}
76107

77-
this.startServer(compilation, cb);
108+
this._startServer(compilation, cb);
78109
};
79110

80-
apply = (compiler: Compiler): void => {
81-
compiler.hooks.afterEmit.tapAsync(
82-
{ name: 'RunScriptPlugin' },
83-
this.afterEmit,
84-
);
85-
};
86-
87-
private startServer = (compilation: Compilation, cb: () => void): void => {
111+
private _startServer = (compilation: Compilation, cb: () => void): void => {
88112
const { assets, compiler } = compilation;
89113
const { options } = this;
90114
let name;
@@ -100,9 +124,7 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
100124
name = names[0];
101125
if (names.length > 1) {
102126
console.log(
103-
`More than one entry built, selected ${name}. All names: ${names.join(
104-
' ',
105-
)}`,
127+
`More than one entry built, selected ${name}. All names: ${names.join(' ')}`,
106128
);
107129
}
108130
}
@@ -111,30 +133,6 @@ export class RunScriptWebpackPlugin implements WebpackPluginInstance {
111133
}
112134

113135
this._entrypoint = `${compiler.options.output.path}/${name}`;
114-
this._startServer(cb);
115-
};
116-
117-
private _startServer(cb?: () => void): void {
118-
const { args, nodeArgs, cwd, env } = this.options;
119-
if (!this._entrypoint) throw new Error('run-script-webpack-plugin requires an entrypoint.');
120-
121-
const child = fork(this._entrypoint, args, {
122-
execArgv: nodeArgs,
123-
stdio: 'inherit',
124-
cwd,
125-
env,
126-
});
127-
128-
setTimeout(() => {
129-
this.worker = child;
130-
cb?.()
131-
}, 0);
132-
}
133-
134-
private _stopServer() {
135-
const signal = getSignal(this.options.signal);
136-
if (this.worker?.pid) {
137-
process.kill(this.worker.pid, signal);
138-
}
136+
this.startServer(cb);
139137
};
140138
}

0 commit comments

Comments
 (0)