-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (54 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict'
// NOTE: we're purposefully using some deprecated syntax instead of more modern
// alternatives for strict compatibility with node >= 4.
const dirname = require('path').dirname
const execa = require('execa')
const resolve = require('resolve')
const semver = require('semver')
const nodeVersions = require('./node-versions')
module.exports = function (path, opts) {
if (!path) {
throw new Error('missing required argument "path"')
}
if (!opts) {
throw new Error('missing required argument "opts"')
}
if (!opts.node) {
throw new Error('missing required argument "opts.node"')
}
if (!semver.validRange(opts.node)) {
throw new Error('invalid semver range "opts.node"')
}
opts.process = opts.process || process
opts.stdio = opts.stdio || 'inherit'
opts.quiet = !!opts.quiet
if (semver.satisfies(opts.process.version, opts.node)) {
const id = resolve.sync(path, {
basedir: dirname(module.parent.filename)
})
return require(id)
}
if (!opts.quiet) {
console.warn(`this program requires a more recent version of \`node ${opts.node}\``)
}
const nodeVersion = semver.maxSatisfying(nodeVersions, opts.node)
const args = [
opts.quiet && '-q',
'-p',
'node@' + nodeVersion,
'--',
'node'
]
.concat(opts.process.argv.slice(1))
.filter(Boolean)
try {
const child = execa.sync('npx', args, {
stdio: opts.stdio
})
opts.process.exit(0)
return child
} catch (err) {
opts.process.exit(err.code || 1)
throw err
}
}