blob: 7814a9692dc0ccddefaa13dc121d71c8fccb35d9 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var cp = require('child_process');
4var parse = require('./lib/parse');
5var enoent = require('./lib/enoent');
6
7var cpSpawnSync = cp.spawnSync;
8
9function spawn(command, args, options) {
10 var parsed;
11 var spawned;
12
13 // Parse the arguments
14 parsed = parse(command, args, options);
15
16 // Spawn the child process
17 spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
18
19 // Hook into child process "exit" event to emit an error if the command
20 // does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
21 enoent.hookChildProcess(spawned, parsed);
22
23 return spawned;
24}
25
26function spawnSync(command, args, options) {
27 var parsed;
28 var result;
29
30 if (!cpSpawnSync) {
31 try {
32 cpSpawnSync = require('spawn-sync'); // eslint-disable-line global-require
33 } catch (ex) {
34 throw new Error(
35 'In order to use spawnSync on node 0.10 or older, you must ' +
36 'install spawn-sync:\n\n' +
37 ' npm install spawn-sync --save'
38 );
39 }
40 }
41
42 // Parse the arguments
43 parsed = parse(command, args, options);
44
45 // Spawn the child process
46 result = cpSpawnSync(parsed.command, parsed.args, parsed.options);
47
48 // Analyze if the command does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
49 result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
50
51 return result;
52}
53
54module.exports = spawn;
55module.exports.spawn = spawn;
56module.exports.sync = spawnSync;
57
58module.exports._parse = parse;
59module.exports._enoent = enoent;