Skip to content

Commit 2f29075

Browse files
committed
promisified async spawn helper rather
1 parent b7c235c commit 2f29075

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed
Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,44 @@
1-
import type { SpawnSyncReturns } from 'child_process';
1+
import { spawn } from 'child_process';
2+
import type { SpawnOptions } from 'child_process';
23

3-
export function assertSpawnSyncResult(
4-
result: SpawnSyncReturns<string>,
5-
name: string
6-
) {
7-
if (result.status === null) {
8-
if (result.signal !== null) {
9-
throw new Error(`${name} terminated due to signal ${result.signal}`);
10-
}
11-
12-
// not supposed to be possible to get here, but just in case
13-
throw new Error(`${name} terminated with no status or signal`);
14-
}
15-
16-
if (result.status !== 0) {
17-
throw new Error(`${name} failed with exit code ${result.status}`);
18-
}
4+
export function execute(
5+
command: string,
6+
args: string[],
7+
options?: SpawnOptions
8+
): Promise<void> {
9+
return new Promise((resolve, reject) => {
10+
const p = spawn(command, args, {
11+
stdio: 'inherit',
12+
...options,
13+
});
14+
p.on('error', (err: any) => {
15+
reject(err);
16+
});
17+
p.on('close', (code: number | null, signal: NodeJS.Signals | null) => {
18+
if (code !== null) {
19+
if (code === 0) {
20+
resolve();
21+
} else {
22+
reject(
23+
new Error(`${command} ${args.join(' ')} exited with code ${code}`)
24+
);
25+
}
26+
} else {
27+
if (signal !== null) {
28+
reject(
29+
new Error(
30+
`${command} ${args.join(' ')} exited with signal ${signal}`
31+
)
32+
);
33+
} else {
34+
// shouldn't happen
35+
reject(
36+
new Error(
37+
`${command} ${args.join(' ')} exited with no code or signal`
38+
)
39+
);
40+
}
41+
}
42+
});
43+
});
1944
}

packages/compass-e2e-tests/installers/mac-dmg.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import { existsSync } from 'fs';
2-
import { assertSpawnSyncResult } from './helpers';
32
import type { InstalledAppInfo, Package } from './types';
4-
import { spawnSync } from 'child_process';
5-
6-
function exec(command: string, args: string[]) {
7-
console.log(command, ...args);
8-
9-
assertSpawnSyncResult(
10-
spawnSync(command, args, {
11-
encoding: 'utf8',
12-
stdio: 'inherit',
13-
}),
14-
`${command} ${args.join(' ')}`
15-
);
16-
}
3+
import { execute } from './helpers';
174

185
export async function installMacDMG(
196
appName: string,
@@ -25,11 +12,15 @@ export async function installMacDMG(
2512
throw new Error(`${fullDestinationPath} already exists`);
2613
}
2714

28-
exec('hdiutil', ['attach', filepath]);
15+
await execute('hdiutil', ['attach', filepath]);
2916
try {
30-
exec('cp', ['-r', `/Volumes/${appName}/${appName}.app`, '/Applications']);
17+
await execute('cp', [
18+
'-r',
19+
`/Volumes/${appName}/${appName}.app`,
20+
'/Applications',
21+
]);
3122
} finally {
32-
exec('hdiutil', ['detach', `/Volumes/${appName}`]);
23+
await execute('hdiutil', ['detach', `/Volumes/${appName}`]);
3324
}
3425

3526
return Promise.resolve({

packages/compass-e2e-tests/smoke-test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env npx ts-node
2-
import { spawnSync } from 'child_process';
32
import { createWriteStream, existsSync, promises as fs } from 'fs';
43
import path from 'path';
54
import yargs from 'yargs';
@@ -10,7 +9,7 @@ import { pick } from 'lodash';
109
import { handler as writeBuildInfo } from 'hadron-build/commands/info';
1110
import type { InstalledAppInfo, Package } from './installers/types';
1211
import { installMacDMG } from './installers/mac-dmg';
13-
import { assertSpawnSyncResult } from './installers/helpers';
12+
import { execute } from './installers/helpers';
1413

1514
const argv = yargs(hideBin(process.argv))
1615
.scriptName('smoke-tests')
@@ -183,7 +182,7 @@ async function run() {
183182

184183
if (appInfo) {
185184
console.log('testing', appInfo.appPath);
186-
testInstalledApp(appInfo);
185+
await testInstalledApp(appInfo);
187186
} else {
188187
console.log(`no app got installed for ${pkg.filename}`);
189188
}
@@ -366,8 +365,8 @@ function verifyPackagesExist(packages: Package[]): void {
366365
}
367366
}
368367

369-
function testInstalledApp(appInfo: InstalledAppInfo) {
370-
const result = spawnSync(
368+
function testInstalledApp(appInfo: InstalledAppInfo): Promise<void> {
369+
return execute(
371370
'npm',
372371
[
373372
'run',
@@ -379,17 +378,13 @@ function testInstalledApp(appInfo: InstalledAppInfo) {
379378
'--test-filter=time-to-first-query',
380379
],
381380
{
382-
encoding: 'utf8',
383-
stdio: 'inherit',
384381
env: {
385382
...process.env,
386383
COMPASS_APP_NAME: appInfo.appName,
387384
COMPASS_APP_PATH: appInfo.appPath,
388385
},
389386
}
390387
);
391-
392-
assertSpawnSyncResult(result, 'npm run test-packaged');
393388
}
394389

395390
run()

0 commit comments

Comments
 (0)