codekingpro/portable-devtools
115k
1'use strict'
2
3const path = require('path')
4const nopt = require('nopt')
5const log = require('./log')
6const childProcess = require('child_process')
7const { EventEmitter } = require('events')
8
9const commands = [
10 // Module build commands
11 'build',
12 'clean',
13 'configure',
14 'rebuild',
15 // Development Header File management commands
16 'install',
17 'list',
18 'remove'
19]
20
21class Gyp extends EventEmitter {
22 /**
23 * Export the contents of the package.json.
24 */
25 package = require('../package.json')
26
27 /**
28 * nopt configuration definitions
29 */
30 configDefs = {
31 help: Boolean, // everywhere
32 arch: String, // 'configure'
33 cafile: String, // 'install'
34 debug: Boolean, // 'build'
35 directory: String, // bin
36 make: String, // 'build'
37 'msvs-version': String, // 'configure'
38 ensure: Boolean, // 'install'
39 solution: String, // 'build' (windows only)
40 proxy: String, // 'install'
41 noproxy: String, // 'install'
42 devdir: String, // everywhere
43 nodedir: String, // 'configure'
44 loglevel: String, // everywhere
45 python: String, // 'configure'
46 'dist-url': String, // 'install'
47 tarball: String, // 'install'
48 jobs: String, // 'build'
49 thin: String, // 'configure'
50 'force-process-config': Boolean // 'configure'
51 }
52
53 /**
54 * nopt shorthands
55 */
56 shorthands = {
57 release: '--no-debug',
58 C: '--directory',
59 debug: '--debug',
60 j: '--jobs',
61 silly: '--loglevel=silly',
62 verbose: '--loglevel=verbose',
63 silent: '--loglevel=silent'
64 }
65
66 /**
67 * expose the command aliases for the bin file to use.
68 */
69 aliases = {
70 ls: 'list',
71 rm: 'remove'
72 }
73
74 constructor (...args) {
75 super(...args)
76
77 this.devDir = ''
78
79 this.commands = commands.reduce((acc, command) => {
80 acc[command] = (argv) => require('./' + command)(this, argv)
81 return acc
82 }, {})
83
84 Object.defineProperty(this, 'version', {
85 enumerable: true,
86 get: function () { return this.package.version }
87 })
88 }
89
90 /**
91 * Parses the given argv array and sets the 'opts',
92 * 'argv' and 'command' properties.
93 */
94 parseArgv (argv) {
95 this.opts = nopt(this.configDefs, this.shorthands, argv)
96 this.argv = this.opts.argv.remain.slice()
97
98 const commands = this.todo = []
99
100 // create a copy of the argv array with aliases mapped
101 argv = this.argv.map((arg) => {
102 // is this an alias?
103 if (arg in this.aliases) {
104 arg = this.aliases[arg]
105 }
106 return arg
107 })
108
109 // process the mapped args into "command" objects ("name" and "args" props)
110 argv.slice().forEach((arg) => {
111 if (arg in this.commands) {
112 const args = argv.splice(0, argv.indexOf(arg))
113 argv.shift()
114 if (commands.length > 0) {
115 commands[commands.length - 1].args = args
116 }
117 commands.push({ name: arg, args: [] })
118 }
119 })
120 if (commands.length > 0) {
121 commands[commands.length - 1].args = argv.splice(0)
122 }
123
124 // support for inheriting config env variables from npm
125 // npm will set environment variables in the following forms:
126 // - `npm_config_<key>` for values from npm's own config. Setting arbitrary
127 // options on npm's config was deprecated in npm v11 but node-gyp still
128 // supports it for backwards compatibility.
129 // See https://github.com/nodejs/node-gyp/issues/3156
130 // - `npm_package_config_node_gyp_<key>` for values from the `config` object
131 // in package.json. This is the preferred way to set options for node-gyp
132 // since npm v11. The `node_gyp_` prefix is used to avoid conflicts with
133 // other tools.
134 // The `npm_package_config_node_gyp_` prefix will take precedence over
135 // `npm_config_` keys.
136 const npmConfigPrefix = /^npm_config_/i
137 const npmPackageConfigPrefix = /^npm_package_config_node_gyp_/i
138
139 const configEnvKeys = Object.keys(process.env)
140 .filter((k) => npmConfigPrefix.test(k) || npmPackageConfigPrefix.test(k))
141 // sort so that npm_package_config_node_gyp_ keys come last and will override
142 .sort((a) => npmConfigPrefix.test(a) ? -1 : 1)
143
144 for (const key of configEnvKeys) {
145 // add the user-defined options to the config
146 const name = npmConfigPrefix.test(key)
147 ? key.replace(npmConfigPrefix, '')
148 : key.replace(npmPackageConfigPrefix, '')
149 // gyp@741b7f1 enters an infinite loop when it encounters
150 // zero-length options so ensure those don't get through.
151 if (name) {
152 // convert names like force_process_config to force-process-config
153 // and convert to lowercase
154 this.opts[name.replaceAll('_', '-').toLowerCase()] = process.env[key]
155 }
156 }
157
158 if (this.opts.loglevel) {
159 log.logger.level = this.opts.loglevel
160 delete this.opts.loglevel
161 }
162 log.resume()
163 }
164
165 /**
166 * Spawns a child process and emits a 'spawn' event.
167 */
168 spawn (command, args, opts) {
169 if (!opts) {
170 opts = {}
171 }
172 if (!opts.silent && !opts.stdio) {
173 opts.stdio = [0, 1, 2]
174 }
175 const cp = childProcess.spawn(command, args, opts)
176 log.info('spawn', command)
177 log.info('spawn args', args)
178 return cp
179 }
180
181 /**
182 * Returns the usage instructions for node-gyp.
183 */
184 usage () {
185 return [
186 '',
187 ' Usage: node-gyp <command> [options]',
188 '',
189 ' where <command> is one of:',
190 commands.map((c) => ' - ' + c + ' - ' + require('./' + c).usage).join('\n'),
191 '',
192 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..'),
193 'node@' + process.versions.node
194 ].join('\n')
195 }
196}
197
198module.exports = () => new Gyp()
199module.exports.Gyp = Gyp
200 