提交 3db202b4 编写于 作者: D Daniel Imms

Clean up teardown code

上级 27f5a327
...@@ -182,7 +182,7 @@ function timeout(ms: number): Promise<void> { ...@@ -182,7 +182,7 @@ function timeout(ms: number): Promise<void> {
// function runInDriver(call: string, args: (string | boolean)[]): Promise<any> {} // function runInDriver(call: string, args: (string | boolean)[]): Promise<any> {}
let args; let args;
let server: ChildProcess; let server: ChildProcess | undefined;
let endpoint: string | undefined; let endpoint: string | undefined;
export async function launch(_args): Promise<void> { export async function launch(_args): Promise<void> {
...@@ -193,13 +193,20 @@ export async function launch(_args): Promise<void> { ...@@ -193,13 +193,20 @@ export async function launch(_args): Promise<void> {
await promisify(mkdir)(webUserDataDir); await promisify(mkdir)(webUserDataDir);
server = spawn(join(args[0], 'resources/server/web.sh'), ['--driver', 'web', '--web-user-data-dir', webUserDataDir]); server = spawn(join(args[0], 'resources/server/web.sh'), ['--driver', 'web', '--web-user-data-dir', webUserDataDir]);
server.stderr.on('data', e => console.log('Server error: ' + e)); server.stderr.on('data', e => console.log('Server error: ' + e));
process.on('exit', () => server.kill()); process.on('exit', teardown);
endpoint = await waitForEndpoint(); endpoint = await waitForEndpoint();
} }
function teardown(): void {
if (server) {
server.kill();
server = undefined;
}
}
function waitForEndpoint(): Promise<string> { function waitForEndpoint(): Promise<string> {
return new Promise<string>(r => { return new Promise<string>(r => {
server.stdout.on('data', d => { server!.stdout.on('data', d => {
const matches = d.toString('ascii').match(/Web UI available at (.+)/); const matches = d.toString('ascii').match(/Web UI available at (.+)/);
if (matches !== null) { if (matches !== null) {
r(matches[1]); r(matches[1]);
...@@ -222,11 +229,7 @@ export function connect(headless: boolean, outPath: string, handle: string): Pro ...@@ -222,11 +229,7 @@ export function connect(headless: boolean, outPath: string, handle: string): Pro
const endpointSplit = endpoint!.split('#'); const endpointSplit = endpoint!.split('#');
await page.goto(`${endpointSplit[0]}?folder=${args[1]}#${endpointSplit[1]}`); await page.goto(`${endpointSplit[0]}?folder=${args[1]}#${endpointSplit[1]}`);
const result = { const result = {
client: { client: { dispose: () => teardown },
dispose: () => {
server.kill();
}
},
driver: buildDriver(browser, page) driver: buildDriver(browser, page)
}; };
c(result); c(result);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册