index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. 'use strict';
  2. const path = require('path');
  3. const childProcess = require('child_process');
  4. const crossSpawn = require('cross-spawn');
  5. const stripFinalNewline = require('strip-final-newline');
  6. const npmRunPath = require('npm-run-path');
  7. const onetime = require('onetime');
  8. const makeError = require('./lib/error');
  9. const normalizeStdio = require('./lib/stdio');
  10. const {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} = require('./lib/kill');
  11. const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream');
  12. const {mergePromise, getSpawnedPromise} = require('./lib/promise');
  13. const {joinCommand, parseCommand, getEscapedCommand} = require('./lib/command');
  14. const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
  15. const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, execPath}) => {
  16. const env = extendEnv ? {...process.env, ...envOption} : envOption;
  17. if (preferLocal) {
  18. return npmRunPath.env({env, cwd: localDir, execPath});
  19. }
  20. return env;
  21. };
  22. const handleArguments = (file, args, options = {}) => {
  23. const parsed = crossSpawn._parse(file, args, options);
  24. file = parsed.command;
  25. args = parsed.args;
  26. options = parsed.options;
  27. options = {
  28. maxBuffer: DEFAULT_MAX_BUFFER,
  29. buffer: true,
  30. stripFinalNewline: true,
  31. extendEnv: true,
  32. preferLocal: false,
  33. localDir: options.cwd || process.cwd(),
  34. execPath: process.execPath,
  35. encoding: 'utf8',
  36. reject: true,
  37. cleanup: true,
  38. all: false,
  39. windowsHide: true,
  40. ...options
  41. };
  42. options.env = getEnv(options);
  43. options.stdio = normalizeStdio(options);
  44. if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {
  45. // #116
  46. args.unshift('/q');
  47. }
  48. return {file, args, options, parsed};
  49. };
  50. const handleOutput = (options, value, error) => {
  51. if (typeof value !== 'string' && !Buffer.isBuffer(value)) {
  52. // When `execa.sync()` errors, we normalize it to '' to mimic `execa()`
  53. return error === undefined ? undefined : '';
  54. }
  55. if (options.stripFinalNewline) {
  56. return stripFinalNewline(value);
  57. }
  58. return value;
  59. };
  60. const execa = (file, args, options) => {
  61. const parsed = handleArguments(file, args, options);
  62. const command = joinCommand(file, args);
  63. const escapedCommand = getEscapedCommand(file, args);
  64. validateTimeout(parsed.options);
  65. let spawned;
  66. try {
  67. spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);
  68. } catch (error) {
  69. // Ensure the returned error is always both a promise and a child process
  70. const dummySpawned = new childProcess.ChildProcess();
  71. const errorPromise = Promise.reject(makeError({
  72. error,
  73. stdout: '',
  74. stderr: '',
  75. all: '',
  76. command,
  77. escapedCommand,
  78. parsed,
  79. timedOut: false,
  80. isCanceled: false,
  81. killed: false
  82. }));
  83. return mergePromise(dummySpawned, errorPromise);
  84. }
  85. const spawnedPromise = getSpawnedPromise(spawned);
  86. const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
  87. const processDone = setExitHandler(spawned, parsed.options, timedPromise);
  88. const context = {isCanceled: false};
  89. spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
  90. spawned.cancel = spawnedCancel.bind(null, spawned, context);
  91. const handlePromise = async () => {
  92. const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);
  93. const stdout = handleOutput(parsed.options, stdoutResult);
  94. const stderr = handleOutput(parsed.options, stderrResult);
  95. const all = handleOutput(parsed.options, allResult);
  96. if (error || exitCode !== 0 || signal !== null) {
  97. const returnedError = makeError({
  98. error,
  99. exitCode,
  100. signal,
  101. stdout,
  102. stderr,
  103. all,
  104. command,
  105. escapedCommand,
  106. parsed,
  107. timedOut,
  108. isCanceled: context.isCanceled,
  109. killed: spawned.killed
  110. });
  111. if (!parsed.options.reject) {
  112. return returnedError;
  113. }
  114. throw returnedError;
  115. }
  116. return {
  117. command,
  118. escapedCommand,
  119. exitCode: 0,
  120. stdout,
  121. stderr,
  122. all,
  123. failed: false,
  124. timedOut: false,
  125. isCanceled: false,
  126. killed: false
  127. };
  128. };
  129. const handlePromiseOnce = onetime(handlePromise);
  130. handleInput(spawned, parsed.options.input);
  131. spawned.all = makeAllStream(spawned, parsed.options);
  132. return mergePromise(spawned, handlePromiseOnce);
  133. };
  134. module.exports = execa;
  135. module.exports.sync = (file, args, options) => {
  136. const parsed = handleArguments(file, args, options);
  137. const command = joinCommand(file, args);
  138. const escapedCommand = getEscapedCommand(file, args);
  139. validateInputSync(parsed.options);
  140. let result;
  141. try {
  142. result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options);
  143. } catch (error) {
  144. throw makeError({
  145. error,
  146. stdout: '',
  147. stderr: '',
  148. all: '',
  149. command,
  150. escapedCommand,
  151. parsed,
  152. timedOut: false,
  153. isCanceled: false,
  154. killed: false
  155. });
  156. }
  157. const stdout = handleOutput(parsed.options, result.stdout, result.error);
  158. const stderr = handleOutput(parsed.options, result.stderr, result.error);
  159. if (result.error || result.status !== 0 || result.signal !== null) {
  160. const error = makeError({
  161. stdout,
  162. stderr,
  163. error: result.error,
  164. signal: result.signal,
  165. exitCode: result.status,
  166. command,
  167. escapedCommand,
  168. parsed,
  169. timedOut: result.error && result.error.code === 'ETIMEDOUT',
  170. isCanceled: false,
  171. killed: result.signal !== null
  172. });
  173. if (!parsed.options.reject) {
  174. return error;
  175. }
  176. throw error;
  177. }
  178. return {
  179. command,
  180. escapedCommand,
  181. exitCode: 0,
  182. stdout,
  183. stderr,
  184. failed: false,
  185. timedOut: false,
  186. isCanceled: false,
  187. killed: false
  188. };
  189. };
  190. module.exports.command = (command, options) => {
  191. const [file, ...args] = parseCommand(command);
  192. return execa(file, args, options);
  193. };
  194. module.exports.commandSync = (command, options) => {
  195. const [file, ...args] = parseCommand(command);
  196. return execa.sync(file, args, options);
  197. };
  198. module.exports.node = (scriptPath, args, options = {}) => {
  199. if (args && !Array.isArray(args) && typeof args === 'object') {
  200. options = args;
  201. args = [];
  202. }
  203. const stdio = normalizeStdio.node(options);
  204. const defaultExecArgv = process.execArgv.filter(arg => !arg.startsWith('--inspect'));
  205. const {
  206. nodePath = process.execPath,
  207. nodeOptions = defaultExecArgv
  208. } = options;
  209. return execa(
  210. nodePath,
  211. [
  212. ...nodeOptions,
  213. scriptPath,
  214. ...(Array.isArray(args) ? args : [])
  215. ],
  216. {
  217. ...options,
  218. stdin: undefined,
  219. stdout: undefined,
  220. stderr: undefined,
  221. stdio,
  222. shell: false
  223. }
  224. );
  225. };