MCPcopy Create free account
hub / github.com/npm/cli / spawn

Function spawn

scripts/util.js:58–114  ·  view source on GitHub ↗
(cmd, ...allArgs)

Source from the content-addressed store, hash-verified

56}
57
58const spawn = async (cmd, ...allArgs) => {
59 const {
60 args,
61 opts: { ok, input, out, lines, quiet, env, ...opts },
62 } = getArgs(allArgs)
63
64 log.info('spawn', `${cmd} ${args.join(' ')}`)
65
66 let res = null
67 try {
68 const spawnOpts = {
69 stdio: quiet || out || lines ? 'pipe' : 'inherit',
70 cwd: CWD,
71 env: { ...process.env, ...env },
72 ...opts,
73 }
74 const proc = cmd === 'git' ? npmGit.spawn(args, spawnOpts) : promiseSpawn(cmd, args, spawnOpts)
75 if (input && proc.stdin) {
76 log.silly('input', input)
77 proc.stdin.write(input)
78 proc.stdin.end()
79 }
80 res = await proc
81 } catch (err) {
82 if (!ok) {
83 throw err
84 }
85 log.silly('suppressed error', err)
86 }
87
88 if (res?.stdout) {
89 res.stdout = res.stdout.toString().trim()
90 if (res.stdout && !quiet) {
91 log.silly('stdout', res.stdout)
92 }
93 }
94
95 if (res?.stderr) {
96 res.stderr = res.stderr.toString().trim()
97 if (res.stderr) {
98 log.silly('stderr', res.stderr)
99 }
100 }
101
102 if (lines) {
103 return (res?.stdout || '')
104 .split('\n')
105 .map(l => l.trim())
106 .filter(Boolean)
107 }
108
109 if (out) {
110 return res?.stdout || ''
111 }
112
113 return res
114}
115

Callers 8

baseSpawnFunction · 0.85
editMethod · 0.85
viewManMethod · 0.85
retrieve-tag.jsFile · 0.85
version.jsFile · 0.85
index.jsFile · 0.85
util.jsFile · 0.85
opFunction · 0.85

Calls 5

getArgsFunction · 0.85
endMethod · 0.80
filterMethod · 0.80
writeMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected