source: trip-planner-front/node_modules/@npmcli/run-script/lib/run-script-pkg.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1const makeSpawnArgs = require('./make-spawn-args.js')
2const promiseSpawn = require('@npmcli/promise-spawn')
3const packageEnvs = require('./package-envs.js')
4const { isNodeGypPackage, defaultGypInstallScript } = require('@npmcli/node-gyp')
5const signalManager = require('./signal-manager.js')
6const isServerPackage = require('./is-server-package.js')
7
8// you wouldn't like me when I'm angry...
9const bruce = (id, event, cmd) =>
10 `\n> ${id ? id + ' ' : ''}${event}\n> ${cmd.trim().replace(/\n/g, '\n> ')}\n`
11
12const runScriptPkg = async options => {
13 const {
14 event,
15 path,
16 scriptShell,
17 env = {},
18 stdio = 'pipe',
19 pkg,
20 args = [],
21 stdioString = false,
22 // note: only used when stdio:inherit
23 banner = true,
24 // how long to wait for a process.kill signal
25 // only exposed here so that we can make the test go a bit faster.
26 signalTimeout = 500,
27 } = options
28
29 const {scripts = {}, gypfile} = pkg
30 let cmd = null
31 if (options.cmd)
32 cmd = options.cmd
33 else if (pkg.scripts && pkg.scripts[event])
34 cmd = pkg.scripts[event] + args.map(a => ` ${JSON.stringify(a)}`).join('')
35 else if ( // If there is no preinstall or install script, default to rebuilding node-gyp packages.
36 event === 'install' &&
37 !scripts.install &&
38 !scripts.preinstall &&
39 gypfile !== false &&
40 await isNodeGypPackage(path)
41 )
42 cmd = defaultGypInstallScript
43 else if (event === 'start' && await isServerPackage(path))
44 cmd = 'node server.js' + args.map(a => ` ${JSON.stringify(a)}`).join('')
45
46 if (!cmd)
47 return { code: 0, signal: null }
48
49 if (stdio === 'inherit' && banner !== false) {
50 // we're dumping to the parent's stdout, so print the banner
51 console.log(bruce(pkg._id, event, cmd))
52 }
53
54 const p = promiseSpawn(...makeSpawnArgs({
55 event,
56 path,
57 scriptShell,
58 env: packageEnvs(env, pkg),
59 stdio,
60 cmd,
61 stdioString,
62 }), {
63 event,
64 script: cmd,
65 pkgid: pkg._id,
66 path,
67 })
68
69 if (stdio === 'inherit')
70 signalManager.add(p.process)
71
72 if (p.stdin)
73 p.stdin.end()
74
75 return p.catch(er => {
76 const { signal } = er
77 if (stdio === 'inherit' && signal) {
78 process.kill(process.pid, signal)
79 // just in case we don't die, reject after 500ms
80 // this also keeps the node process open long enough to actually
81 // get the signal, rather than terminating gracefully.
82 return new Promise((res, rej) => setTimeout(() => rej(er), signalTimeout))
83 } else
84 throw er
85 })
86}
87
88module.exports = runScriptPkg
Note: See TracBrowser for help on using the repository browser.