Skip to content

Commit 66c700c

Browse files
authored
Avoid synchronous functions in the NPM wrapper (#575)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent d8a77d8 commit 66c700c

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

npm/main.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const os = require('os');
22
const path = require('path');
3-
const fs = require('fs');
3+
const fs = require('fs/promises');
44
const child_process = require('child_process');
55

66
const PLATFORM = os.platform() === 'win32' ? 'windows' : os.platform();
@@ -9,31 +9,9 @@ const EXTENSION = PLATFORM === 'windows' ? '.exe' : '';
99
const EXECUTABLE = path.join(__dirname, '..', 'build', 'github-releases',
1010
`jsonschema-${PLATFORM}-${ARCH}${EXTENSION}`);
1111

12-
function spawn(args, options = {}) {
13-
const json = options.json === true;
14-
const spawnArgs = json ? [...args, '--json'] : args;
15-
12+
function spawnProcess(executable, args, options) {
1613
return new Promise((resolve, reject) => {
17-
if (!fs.existsSync(EXECUTABLE)) {
18-
reject(new Error(
19-
`The JSON Schema CLI NPM package does not support ${os.platform()} for ${os.arch()} yet. ` +
20-
'Please open a GitHub issue at https://github.com/sourcemeta/jsonschema'
21-
));
22-
return;
23-
}
24-
25-
if (PLATFORM === 'darwin') {
26-
child_process.spawnSync('/usr/bin/xattr', ['-c', EXECUTABLE], { stdio: 'inherit' });
27-
}
28-
29-
const spawnOptions = {
30-
windowsHide: true,
31-
...options
32-
};
33-
34-
delete spawnOptions.json;
35-
36-
const process = child_process.spawn(EXECUTABLE, spawnArgs, spawnOptions);
14+
const process = child_process.spawn(executable, args, options);
3715

3816
let stdout = '';
3917
let stderr = '';
@@ -55,13 +33,42 @@ function spawn(args, options = {}) {
5533
});
5634

5735
process.on('close', (code) => {
58-
resolve({
59-
code: code,
60-
stdout: json ? JSON.parse(stdout) : stdout,
61-
stderr: stderr
62-
});
36+
resolve({ code, stdout, stderr });
6337
});
6438
});
6539
}
6640

41+
async function spawn(args, options = {}) {
42+
const json = options.json === true;
43+
const spawnArgs = json ? [...args, '--json'] : args;
44+
45+
try {
46+
await fs.access(EXECUTABLE);
47+
} catch {
48+
throw new Error(
49+
`The JSON Schema CLI NPM package does not support ${os.platform()} for ${os.arch()} yet. ` +
50+
'Please open a GitHub issue at https://github.com/sourcemeta/jsonschema'
51+
);
52+
}
53+
54+
if (PLATFORM === 'darwin') {
55+
await spawnProcess('/usr/bin/xattr', ['-c', EXECUTABLE], { stdio: 'inherit' });
56+
}
57+
58+
const spawnOptions = {
59+
windowsHide: true,
60+
...options
61+
};
62+
63+
delete spawnOptions.json;
64+
65+
const result = await spawnProcess(EXECUTABLE, spawnArgs, spawnOptions);
66+
67+
return {
68+
code: result.code,
69+
stdout: json ? JSON.parse(result.stdout) : result.stdout,
70+
stderr: result.stderr
71+
};
72+
}
73+
6774
module.exports = { spawn };

0 commit comments

Comments
 (0)