* Spawns a new process using the given `file`. * @param {string} file * @param {string[]} [args] * @param {{ * cwd?: string | URL; * env?: Record ; * argv0?: string; * stdio?: Array | string; * detached?: boolean; * uid?: number; * gid?: number; * serializat
(file, args, options)
| 787 | * @returns {ChildProcess} |
| 788 | */ |
| 789 | function spawn(file, args, options) { |
| 790 | options = normalizeSpawnArguments(file, args, options); |
| 791 | validateTimeout(options.timeout); |
| 792 | validateAbortSignal(options.signal, 'options.signal'); |
| 793 | const killSignal = sanitizeKillSignal(options.killSignal); |
| 794 | const child = new ChildProcess(); |
| 795 | |
| 796 | debug('spawn', options); |
| 797 | child.spawn(options); |
| 798 | |
| 799 | if (options.timeout > 0) { |
| 800 | let timeoutId = setTimeout(() => { |
| 801 | if (timeoutId) { |
| 802 | try { |
| 803 | child.kill(killSignal); |
| 804 | } catch (err) { |
| 805 | child.emit('error', err); |
| 806 | } |
| 807 | timeoutId = null; |
| 808 | } |
| 809 | }, options.timeout); |
| 810 | |
| 811 | child.once('exit', () => { |
| 812 | if (timeoutId) { |
| 813 | clearTimeout(timeoutId); |
| 814 | timeoutId = null; |
| 815 | } |
| 816 | }); |
| 817 | } |
| 818 | |
| 819 | if (options.signal) { |
| 820 | const signal = options.signal; |
| 821 | if (signal.aborted) { |
| 822 | process.nextTick(onAbortListener); |
| 823 | } else { |
| 824 | addAbortListener ??= require('internal/events/abort_listener').addAbortListener; |
| 825 | const disposable = addAbortListener(signal, onAbortListener); |
| 826 | child.once('exit', disposable[SymbolDispose]); |
| 827 | } |
| 828 | |
| 829 | function onAbortListener() { |
| 830 | abortChildProcess(child, killSignal, options.signal.reason); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | return child; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Spawns a new process synchronously using the given `file`. |
no test coverage detected
searching dependent graphs…