@flun/nodejs-mobile-gyp 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/list.js CHANGED
@@ -1,30 +1,24 @@
1
- import gracefulFs from 'graceful-fs'
2
- import * as log from './log.js'
1
+ import gracefulFs from 'graceful-fs';
2
+ import * as log from './log.js';
3
3
 
4
- const fs = gracefulFs.promises
4
+ const fs = gracefulFs.promises,
5
+ /**
6
+ * 列出已安装的 Node 开发文件版本
7
+ */
8
+ list = async (gyp, args) => {
9
+ const devDir = gyp.devDir;
10
+ let versions = [];
5
11
 
6
- /**
7
- * 列出已安装的 Node 开发文件版本
8
- */
9
- const list = async (gyp, args) => {
10
- const devDir = gyp.devDir
11
- log.verbose('list', 'using node-gyp dir:', devDir)
12
-
13
- let versions = []
14
- try {
15
- const dir = await fs.readdir(devDir)
16
- if (Array.isArray(dir)) {
17
- versions = dir.filter((v) => v !== 'current')
18
- }
19
- } catch (err) {
20
- if (err && err.code !== 'ENOENT') {
21
- throw err
12
+ log.verbose('list', 'using node-gyp dir:', devDir);
13
+ try {
14
+ const dir = await fs.readdir(devDir);
15
+ if (Array.isArray(dir)) versions = dir.filter((v) => v !== 'current');
16
+ } catch (err) {
17
+ if (err && err.code !== 'ENOENT') throw err;
22
18
  }
23
- }
24
19
 
25
- return versions
26
- }
27
-
28
- list.usage = 'Prints a listing of the currently installed node development files'
20
+ return versions;
21
+ }
29
22
 
30
- export { list }
23
+ list.usage = 'Prints a listing of the currently installed node development files';
24
+ export { list }
package/lib/log.js CHANGED
@@ -1,174 +1,110 @@
1
- import procLog from 'proc-log'
2
- import { format } from 'util'
1
+ import procLog from 'proc-log';
2
+ import { format } from 'util';
3
3
 
4
4
  // 按级别生成带前缀的日志函数
5
- const logLevels = Object.keys(procLog).filter((k) => typeof procLog[k] === 'function')
6
- const withPrefix = (prefix) => logLevels.reduce((acc, level) => {
7
- acc[level] = (...args) => procLog[level](prefix, ...args)
8
- return acc
9
- }, {})
10
-
11
- // 极简 ANSI 颜色生成器
12
- const COLORS = {
13
- wrap: (str, colors) => {
14
- const codes = colors.filter(c => typeof c === 'number')
15
- return `\x1b[${codes.join(';')}m${str}\x1b[0m`
16
- },
17
- inverse: 7,
18
- fg: {
19
- black: 30,
20
- red: 31,
21
- green: 32,
22
- yellow: 33,
23
- blue: 34,
24
- magenta: 35,
25
- cyan: 36,
26
- white: 37
27
- },
28
- bg: {
29
- black: 40,
30
- red: 41,
31
- green: 42,
32
- yellow: 43,
33
- blue: 44,
34
- magenta: 45,
35
- cyan: 46,
36
- white: 47
37
- }
38
- }
5
+ const logLevels = Object.keys(procLog.log).filter((k) => typeof procLog.log[k] === 'function'),
6
+ withPrefix = (prefix) => logLevels.reduce((acc, level) => {
7
+ acc[level] = (...args) => procLog.log[level](prefix, ...args);
8
+ return acc;
9
+ }, {}),
10
+ // 极简 ANSI 颜色生成器
11
+ COLORS = {
12
+ wrap: (str, colors) => {
13
+ const codes = colors.filter(c => typeof c === 'number')
14
+ return `\x1b[${codes.join(';')}m${str}\x1b[0m`
15
+ },
16
+ inverse: 7,
17
+ fg: { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 },
18
+ bg: { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 }
19
+ };
39
20
 
40
21
  // 日志器:负责缓冲、暂停/恢复、着色输出
41
22
  class Logger {
42
- #buffer = []
43
- #paused = null
44
- #level = null
45
- #stream = null
23
+ #buffer = [];
24
+ #paused = null;
25
+ #level = null;
26
+ #stream = null;
46
27
 
47
28
  // 从吵闹到安静排序
48
- #levels = [{
49
- id: 'silly',
50
- display: 'sill',
51
- style: { inverse: true }
52
- }, {
53
- id: 'verbose',
54
- display: 'verb',
55
- style: { fg: 'cyan', bg: 'black' }
56
- }, {
57
- id: 'info',
58
- style: { fg: 'green' }
59
- }, {
60
- id: 'http',
61
- style: { fg: 'green', bg: 'black' }
62
- }, {
63
- id: 'notice',
64
- style: { fg: 'cyan', bg: 'black' }
65
- }, {
66
- id: 'warn',
67
- display: 'WARN',
68
- style: { fg: 'black', bg: 'yellow' }
69
- }, {
70
- id: 'error',
71
- display: 'ERR!',
72
- style: { fg: 'red', bg: 'black' }
73
- }]
74
-
75
- constructor (stream) {
76
- process.on('log', (...args) => this.#onLog(...args))
77
- this.#levels = new Map(this.#levels.map((level, index) => [level.id, { ...level, index }]))
78
- this.level = 'info'
79
- this.stream = stream
80
- procLog.pause()
29
+ #levels = [
30
+ { id: 'silly', display: 'sill', style: { inverse: true } },
31
+ { id: 'verbose', display: 'verb', style: { fg: 'cyan', bg: 'black' } },
32
+ { id: 'info', style: { fg: 'green' } },
33
+ { id: 'http', style: { fg: 'green', bg: 'black' } },
34
+ { id: 'notice', style: { fg: 'cyan', bg: 'black' } },
35
+ { id: 'warn', display: 'WARN', style: { fg: 'black', bg: 'yellow' } },
36
+ { id: 'error', display: 'ERR!', style: { fg: 'red', bg: 'black' } }];
37
+
38
+ constructor(stream) {
39
+ process.on('log', (...args) => this.#onLog(...args));
40
+ this.#levels = new Map(this.#levels.map((level, index) => [level.id, { ...level, index }]));
41
+ this.level = 'info', this.stream = stream, procLog.log.pause();
81
42
  }
82
43
 
83
- get stream () {
84
- return this.#stream
44
+ get stream() {
45
+ return this.#stream;
85
46
  }
86
47
 
87
- set stream (stream) {
88
- this.#stream = stream
48
+ set stream(stream) {
49
+ this.#stream = stream;
89
50
  }
90
51
 
91
- get level () {
92
- return this.#levels.get(this.#level) ?? null
52
+ get level() {
53
+ return this.#levels.get(this.#level) ?? null;
93
54
  }
94
55
 
95
- set level (level) {
96
- this.#level = this.#levels.get(level)?.id ?? null
56
+ set level(level) {
57
+ this.#level = this.#levels.get(level)?.id ?? null;
97
58
  }
98
59
 
99
- isVisible (level) {
100
- return this.level?.index <= this.#levels.get(level)?.index ?? -1
60
+ isVisible(level) {
61
+ return this.level?.index <= this.#levels.get(level)?.index ?? -1;
101
62
  }
102
63
 
103
- #onLog (...args) {
104
- const [level] = args
64
+ #onLog(...args) {
65
+ const [level] = args;
105
66
 
106
67
  if (level === 'pause') {
107
- this.#paused = true
68
+ this.#paused = true;
108
69
  return
109
70
  }
110
71
 
111
72
  if (level === 'resume') {
112
- this.#paused = false
113
- this.#buffer.forEach((b) => this.#log(...b))
114
- this.#buffer.length = 0
115
- return
73
+ this.#paused = false, this.#buffer.forEach((b) => this.#log(...b)), this.#buffer.length = 0;
74
+ return;
116
75
  }
117
76
 
118
77
  if (this.#paused) {
119
- this.#buffer.push(args)
120
- return
78
+ this.#buffer.push(args);
79
+ return;
121
80
  }
122
81
 
123
- this.#log(...args)
82
+ this.#log(...args);
124
83
  }
125
84
 
126
- #color (str, { fg, bg, inverse }) {
127
- if (!this.#stream?.isTTY) {
128
- return str
129
- }
130
-
131
- return COLORS.wrap(str, [
132
- COLORS.fg[fg],
133
- COLORS.bg[bg],
134
- inverse && COLORS.inverse
135
- ])
85
+ #color(str, { fg, bg, inverse }) {
86
+ if (!this.#stream?.isTTY) return str
87
+ return COLORS.wrap(str, [COLORS.fg[fg], COLORS.bg[bg], inverse && COLORS.inverse]);
136
88
  }
137
89
 
138
- #log (levelId, msgPrefix, ...args) {
139
- if (!this.isVisible(levelId) || typeof this.#stream?.write !== 'function') {
140
- return
141
- }
142
-
143
- const level = this.#levels.get(levelId)
144
-
145
- const prefixParts = [
146
- this.#color('gyp', { fg: 'white', bg: 'black' }),
147
- this.#color(level.display ?? level.id, level.style)
148
- ]
149
- if (msgPrefix) {
150
- prefixParts.push(this.#color(msgPrefix, { fg: 'magenta' }))
151
- }
90
+ #log(levelId, msgPrefix, ...args) {
91
+ if (!this.isVisible(levelId) || typeof this.#stream?.write !== 'function') return;
152
92
 
153
- const prefix = prefixParts.join(' ').trim() + ' '
154
- const lines = format(...args).split(/\r?\n/).map(l => prefix + l.trim())
93
+ const level = this.#levels.get(levelId),
94
+ prefixParts = [
95
+ this.#color('gyp', { fg: 'white', bg: 'black' }),
96
+ this.#color(level.display ?? level.id, level.style)
97
+ ];
98
+ if (msgPrefix) prefixParts.push(this.#color(msgPrefix, { fg: 'magenta' }));
155
99
 
156
- this.#stream.write(lines.join('\n') + '\n')
100
+ const prefix = prefixParts.join(' ').trim() + ' ', lines = format(...args).split(/\r?\n/).map(l => prefix + l.trim());
101
+ this.#stream.write(lines.join('\n') + '\n');
157
102
  }
158
103
  }
159
104
 
160
105
  // 用于测试时抑制日志
161
- const NULL_LOGGER = !!process.env.NODE_GYP_NULL_LOGGER
162
-
163
- const logger = new Logger(NULL_LOGGER ? null : process.stderr)
164
- const stdout = NULL_LOGGER ? () => {} : (...args) => console.log(...args)
165
-
166
- // 解构 proc-log 的函数,供末尾统一导出
167
- const { notice, error, warn, info, verbose, http, silly, pause, resume } = procLog
106
+ const NULL_LOGGER = !!process.env.NODE_GYP_NULL_LOGGER, logger = new Logger(NULL_LOGGER ? null : process.stderr),
107
+ stdout = NULL_LOGGER ? () => { } : (...args) => console.log(...args),
108
+ { notice, error, warn, info, verbose, http, silly, pause, resume } = procLog.log; // 解构 proc-log 的函数,供末尾统一导出
168
109
 
169
- export {
170
- logger,
171
- stdout,
172
- withPrefix,
173
- notice, error, warn, info, verbose, http, silly, pause, resume
174
- }
110
+ export { logger, stdout, withPrefix, notice, error, warn, info, verbose, http, silly, pause, resume }
package/lib/node-gyp.js CHANGED
@@ -1,32 +1,22 @@
1
- import path from 'path'
2
- import nopt from 'nopt'
3
- import * as log from './log.js'
4
- import childProcess from 'child_process'
5
- import { EventEmitter } from 'events'
6
- import { readFileSync } from 'fs'
7
- import { fileURLToPath } from 'url'
8
-
9
- import { build } from './build.js'
10
- import { clean } from './clean.js'
11
- import { configure } from './configure.js'
12
- import { rebuild } from './rebuild.js'
13
- import { install } from './install.js'
14
- import { list } from './list.js'
15
- import { remove } from './remove.js'
16
-
17
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
18
-
19
- // 命令名到实现函数的静态映射(ESM 不支持动态 require)
20
- const commandModules = {
21
- build,
22
- clean,
23
- configure,
24
- rebuild,
25
- install,
26
- list,
27
- remove
28
- }
29
- const commands = Object.keys(commandModules)
1
+ import path from 'path';
2
+ import nopt from 'nopt';
3
+ import * as log from './log.js';
4
+ import childProcess from 'child_process';
5
+ import { EventEmitter } from 'events';
6
+ import { readFileSync } from 'fs';
7
+ import { fileURLToPath } from 'url';
8
+
9
+ import { build } from './build.js';
10
+ import { clean } from './clean.js';
11
+ import { configure } from './configure.js';
12
+ import { rebuild } from './rebuild.js';
13
+ import { install } from './install.js';
14
+ import { list } from './list.js';
15
+ import { remove } from './remove.js';
16
+
17
+ const __dirname = path.dirname(fileURLToPath(import.meta.url)),
18
+ // 命令名到实现函数的静态映射(ESM 不支持动态 require)
19
+ commandModules = { build, clean, configure, rebuild, install, list, remove }, commands = Object.keys(commandModules);
30
20
 
31
21
  /**
32
22
  * node-gyp 主类:解析参数、分发命令、生成 usage
@@ -79,123 +69,88 @@ class Gyp extends EventEmitter {
79
69
  /**
80
70
  * 暴露命令别名供 bin 文件使用
81
71
  */
82
- aliases = {
83
- ls: 'list',
84
- rm: 'remove'
85
- }
86
-
87
- constructor (...args) {
88
- super(...args)
89
-
90
- this.devDir = ''
91
-
72
+ aliases = { ls: 'list', rm: 'remove' }
73
+ constructor(...args) {
74
+ super(...args), this.devDir = '';
92
75
  this.commands = commands.reduce((acc, command) => {
93
- acc[command] = (argv) => commandModules[command](this, argv)
94
- return acc
76
+ acc[command] = (argv) => commandModules[command](this, argv);
77
+ return acc;
95
78
  }, {})
96
79
 
97
80
  Object.defineProperty(this, 'version', {
98
- enumerable: true,
99
- get: function () { return this.package.version }
81
+ enumerable: true, get() { return this.package.version }
100
82
  })
101
83
  }
102
84
 
103
85
  /**
104
86
  * 解析传入的 argv 数组,设置 'opts'、'argv' 和 'command' 属性
105
87
  */
106
- parseArgv (argv) {
107
- this.opts = nopt(this.configDefs, this.shorthands, argv)
108
- this.argv = this.opts.argv.remain.slice()
109
-
110
- const commands = this.todo = []
88
+ parseArgv(argv) {
89
+ this.opts = nopt(this.configDefs, this.shorthands, argv), this.argv = this.opts.argv.remain.slice();
111
90
 
91
+ const commands = this.todo = [];
112
92
  // 创建一份把别名映射后的 argv 副本
113
- argv = this.argv.map((arg) => {
114
- // 是别名吗?
115
- if (arg in this.aliases) {
116
- arg = this.aliases[arg]
117
- }
93
+ argv = this.argv.map(arg => {
94
+ // 如果当前参数是命令别名(如 ls/rm),就替换成真实命令名
95
+ if (arg in this.aliases) arg = this.aliases[arg];
118
96
  return arg
119
97
  })
120
98
 
121
99
  // 把映射后的参数处理成 "command" 对象(name 和 args 属性)
122
- argv.slice().forEach((arg) => {
100
+ argv.slice().forEach(arg => {
123
101
  if (arg in this.commands) {
124
- const args = argv.splice(0, argv.indexOf(arg))
125
- argv.shift()
126
- if (commands.length > 0) {
127
- commands[commands.length - 1].args = args
128
- }
129
- commands.push({ name: arg, args: [] })
102
+ const args = argv.splice(0, argv.indexOf(arg));
103
+ argv.shift();
104
+ if (commands.length > 0) commands[commands.length - 1].args = args;
105
+ commands.push({ name: arg, args: [] });
130
106
  }
131
107
  })
132
- if (commands.length > 0) {
133
- commands[commands.length - 1].args = argv.splice(0)
134
- }
108
+ if (commands.length > 0) commands[commands.length - 1].args = argv.splice(0);
135
109
 
136
110
  // 支持从 npm 继承配置环境变量
137
- const npmConfigPrefix = 'npm_config_'
111
+ const npmConfigPrefix = 'npm_config_';
138
112
  Object.keys(process.env).forEach((name) => {
139
- if (name.indexOf(npmConfigPrefix) !== 0) {
140
- return
141
- }
142
- const val = process.env[name]
143
- if (name === npmConfigPrefix + 'loglevel') {
144
- log.logger.level = val
145
- } else {
146
- // 把用户定义的选项加入配置
147
- name = name.substring(npmConfigPrefix.length)
113
+ if (name.indexOf(npmConfigPrefix) !== 0) return;
114
+ const val = process.env[name];
115
+ if (name === npmConfigPrefix + 'loglevel') log.logger.level = val
116
+ else {
117
+ name = name.substring(npmConfigPrefix.length); // 把用户定义的选项加入配置
148
118
  // gyp@741b7f1 遇到零长度选项会死循环,确保过滤掉
149
119
  if (name) {
150
- // 把 force_process_config 这类名字转成 force-process-config
151
- if (name.includes('_')) {
152
- name = name.replace(/_/g, '-')
153
- }
154
- this.opts[name] = val
120
+ // 把 force_process_config 这类名字转成 force-process-config
121
+ if (name.includes('_')) name = name.replace(/_/g, '-');
122
+ this.opts[name] = val;
155
123
  }
156
124
  }
157
125
  })
158
126
 
159
- if (this.opts.loglevel) {
160
- log.logger.level = this.opts.loglevel
161
- }
162
- log.resume()
127
+ if (this.opts.loglevel) log.logger.level = this.opts.loglevel;
128
+ log.resume();
163
129
  }
164
130
 
165
131
  /**
166
132
  * 启动子进程并发出 'spawn' 事件
167
133
  */
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
134
+ spawn(command, args, opts) {
135
+ if (!opts) opts = {};
136
+ if (!opts.silent && !opts.stdio) opts.stdio = [0, 1, 2];
137
+ const cp = childProcess.spawn(command, args, opts);
138
+ log.info('spawn', command), log.info('spawn args', args);
139
+ return cp;
179
140
  }
180
141
 
181
142
  /**
182
143
  * 返回 node-gyp 的用法说明
183
144
  */
184
- usage () {
145
+ usage() {
185
146
  return [
186
- '',
187
- ' Usage: node-gyp <command> [options]',
188
- '',
189
- ' where <command> is one of:',
190
- commands.map((c) => ' - ' + c + ' - ' + commandModules[c].usage).join('\n'),
191
- '',
192
- 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..'),
193
- 'node@' + process.versions.node
194
- ].join('\n')
147
+ '', ' Usage: node-gyp <command> [options]', '', ' where <command> is one of:',
148
+ commands.map((c) => ' - ' + c + ' - ' + commandModules[c].usage).join('\n'), '',
149
+ 'node-gyp@' + this.version + ' ' + path.resolve(__dirname, '..'), 'node@' + process.versions.node
150
+ ].join('\n');
195
151
  }
196
152
  }
197
153
 
198
- const createGyp = () => new Gyp()
199
-
200
- export default createGyp
201
- export { Gyp }
154
+ const createGyp = () => new Gyp();
155
+ export default createGyp;
156
+ export { Gyp }