UNPKG

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