1 | #!/usr/bin/env node
|
---|
2 |
|
---|
3 | var proc = require('child_process')
|
---|
4 | var os = require('os')
|
---|
5 | var path = require('path')
|
---|
6 |
|
---|
7 | if (!buildFromSource()) {
|
---|
8 | proc.exec('node-gyp-build-test', function (err, stdout, stderr) {
|
---|
9 | if (err) {
|
---|
10 | if (verbose()) console.error(stderr)
|
---|
11 | preinstall()
|
---|
12 | }
|
---|
13 | })
|
---|
14 | } else {
|
---|
15 | preinstall()
|
---|
16 | }
|
---|
17 |
|
---|
18 | function build () {
|
---|
19 | var args = [os.platform() === 'win32' ? 'node-gyp.cmd' : 'node-gyp', 'rebuild']
|
---|
20 |
|
---|
21 | try {
|
---|
22 | args = [
|
---|
23 | process.execPath,
|
---|
24 | path.join(require.resolve('node-gyp/package.json'), '..', require('node-gyp/package.json').bin['node-gyp']),
|
---|
25 | 'rebuild'
|
---|
26 | ]
|
---|
27 | } catch (_) {}
|
---|
28 |
|
---|
29 | proc.spawn(args[0], args.slice(1), { stdio: 'inherit' }).on('exit', function (code) {
|
---|
30 | if (code || !process.argv[3]) process.exit(code)
|
---|
31 | exec(process.argv[3]).on('exit', function (code) {
|
---|
32 | process.exit(code)
|
---|
33 | })
|
---|
34 | })
|
---|
35 | }
|
---|
36 |
|
---|
37 | function preinstall () {
|
---|
38 | if (!process.argv[2]) return build()
|
---|
39 | exec(process.argv[2]).on('exit', function (code) {
|
---|
40 | if (code) process.exit(code)
|
---|
41 | build()
|
---|
42 | })
|
---|
43 | }
|
---|
44 |
|
---|
45 | function exec (cmd) {
|
---|
46 | if (process.platform !== 'win32') {
|
---|
47 | var shell = os.platform() === 'android' ? 'sh' : '/bin/sh'
|
---|
48 | return proc.spawn(shell, ['-c', '--', cmd], {
|
---|
49 | stdio: 'inherit'
|
---|
50 | })
|
---|
51 | }
|
---|
52 |
|
---|
53 | return proc.spawn(process.env.comspec || 'cmd.exe', ['/s', '/c', '"' + cmd + '"'], {
|
---|
54 | windowsVerbatimArguments: true,
|
---|
55 | stdio: 'inherit'
|
---|
56 | })
|
---|
57 | }
|
---|
58 |
|
---|
59 | function buildFromSource () {
|
---|
60 | return hasFlag('--build-from-source') || process.env.npm_config_build_from_source === 'true'
|
---|
61 | }
|
---|
62 |
|
---|
63 | function verbose () {
|
---|
64 | return hasFlag('--verbose') || process.env.npm_config_loglevel === 'verbose'
|
---|
65 | }
|
---|
66 |
|
---|
67 | // TODO (next major): remove in favor of env.npm_config_* which works since npm
|
---|
68 | // 0.1.8 while npm_config_argv will stop working in npm 7. See npm/rfcs#90
|
---|
69 | function hasFlag (flag) {
|
---|
70 | if (!process.env.npm_config_argv) return false
|
---|
71 |
|
---|
72 | try {
|
---|
73 | return JSON.parse(process.env.npm_config_argv).original.indexOf(flag) !== -1
|
---|
74 | } catch (_) {
|
---|
75 | return false
|
---|
76 | }
|
---|
77 | }
|
---|