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 win32 = os.platform() === 'win32'
|
20 | var args = [win32 ? 'node-gyp.cmd' : 'node-gyp', 'rebuild']
|
21 |
|
22 | try {
|
23 | var pkg = require('node-gyp/package.json')
|
24 | args = [
|
25 | process.execPath,
|
26 | path.join(require.resolve('node-gyp/package.json'), '..', typeof pkg.bin === 'string' ? pkg.bin : pkg.bin['node-gyp']),
|
27 | 'rebuild'
|
28 | ]
|
29 | } catch (_) {}
|
30 |
|
31 | proc.spawn(args[0], args.slice(1), { stdio: 'inherit', shell: win32, windowsHide: true }).on('exit', function (code) {
|
32 | if (code || !process.argv[3]) process.exit(code)
|
33 | exec(process.argv[3]).on('exit', function (code) {
|
34 | process.exit(code)
|
35 | })
|
36 | })
|
37 | }
|
38 |
|
39 | function preinstall () {
|
40 | if (!process.argv[2]) return build()
|
41 | exec(process.argv[2]).on('exit', function (code) {
|
42 | if (code) process.exit(code)
|
43 | build()
|
44 | })
|
45 | }
|
46 |
|
47 | function exec (cmd) {
|
48 | if (process.platform !== 'win32') {
|
49 | var shell = os.platform() === 'android' ? 'sh' : true
|
50 | return proc.spawn(cmd, [], {
|
51 | shell,
|
52 | stdio: 'inherit'
|
53 | })
|
54 | }
|
55 |
|
56 | return proc.spawn(cmd, [], {
|
57 | windowsVerbatimArguments: true,
|
58 | stdio: 'inherit',
|
59 | shell: true,
|
60 | windowsHide: true
|
61 | })
|
62 | }
|
63 |
|
64 | function buildFromSource () {
|
65 | return hasFlag('--build-from-source') || process.env.npm_config_build_from_source === 'true'
|
66 | }
|
67 |
|
68 | function verbose () {
|
69 | return hasFlag('--verbose') || process.env.npm_config_loglevel === 'verbose'
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | function hasFlag (flag) {
|
75 | if (!process.env.npm_config_argv) return false
|
76 |
|
77 | try {
|
78 | return JSON.parse(process.env.npm_config_argv).original.indexOf(flag) !== -1
|
79 | } catch (_) {
|
80 | return false
|
81 | }
|
82 | }
|