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