Skip to content

Commit 192eec2

Browse files
authored
Sync deps and engines with npm (#2770)
* feat!: update `engines.node` to `^14.17.0 || ^16.13.0 || >=18.0.0` * deps: nopt@^7.0.0 * feat: replace npmlog with proc-log * deps: standard@17.0.0 and fix linting errors * deps: which@3.0.0 - this also promiisifies the build command * deps: glob@8.0.3 * feat: drop rimraf dependency * fix: use fs/promises in favor of fs.promises
1 parent 33391db commit 192eec2

27 files changed

+533
-415
lines changed

bin/node-gyp.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ process.title = 'node-gyp'
66

77
const envPaths = require('env-paths')
88
const gyp = require('../')
9-
const log = require('npmlog')
9+
const log = require('../lib/log')
1010
const os = require('os')
1111

1212
/**
1313
* Process and execute the selected commands.
1414
*/
1515

1616
const prog = gyp()
17-
var completed = false
17+
let completed = false
1818
prog.parseArgv(process.argv)
1919
prog.devDir = prog.opts.devdir
2020

21-
var homeDir = os.homedir()
21+
const homeDir = os.homedir()
2222
if (prog.devDir) {
2323
prog.devDir = prog.devDir.replace(/^~/, homeDir)
2424
} else if (homeDir) {
@@ -48,11 +48,11 @@ log.info('using', 'node@%s | %s | %s', process.versions.node, process.platform,
4848
* Change dir if -C/--directory was passed.
4949
*/
5050

51-
var dir = prog.opts.directory
51+
const dir = prog.opts.directory
5252
if (dir) {
53-
var fs = require('fs')
53+
const fs = require('fs')
5454
try {
55-
var stat = fs.statSync(dir)
55+
const stat = fs.statSync(dir)
5656
if (stat.isDirectory()) {
5757
log.info('chdir', dir)
5858
process.chdir(dir)
@@ -69,7 +69,7 @@ if (dir) {
6969
}
7070

7171
function run () {
72-
var command = prog.todo.shift()
72+
const command = prog.todo.shift()
7373
if (!command) {
7474
// done!
7575
completed = true
@@ -86,7 +86,7 @@ function run () {
8686
return process.exit(1)
8787
}
8888
if (command.name === 'list') {
89-
var versions = arguments[1]
89+
const versions = arguments[1]
9090
if (versions.length > 0) {
9191
versions.forEach(function (version) {
9292
console.log(version)
@@ -120,7 +120,7 @@ process.on('uncaughtException', function (err) {
120120

121121
function errorMessage () {
122122
// copied from npm's lib/utils/error-handler.js
123-
var os = require('os')
123+
const os = require('os')
124124
log.error('System', os.type() + ' ' + os.release())
125125
log.error('command', process.argv
126126
.map(JSON.stringify).join(' '))

lib/build.js

+84-91
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict'
22

3-
const fs = require('graceful-fs')
3+
const fs = require('graceful-fs').promises
4+
const { promisify } = require('util')
45
const path = require('path')
5-
const glob = require('glob')
6-
const log = require('npmlog')
6+
const glob = promisify(require('glob'))
7+
const log = require('./log')
78
const which = require('which')
89
const win = process.platform === 'win32'
910

10-
function build (gyp, argv, callback) {
11-
var platformMake = 'make'
11+
async function build (gyp, argv) {
12+
let platformMake = 'make'
1213
if (process.platform === 'aix') {
1314
platformMake = 'gmake'
1415
} else if (process.platform === 'os400') {
@@ -21,113 +22,103 @@ function build (gyp, argv, callback) {
2122
})
2223
}
2324

24-
var makeCommand = gyp.opts.make || process.env.MAKE || platformMake
25-
var command = win ? 'msbuild' : makeCommand
26-
var jobs = gyp.opts.jobs || process.env.JOBS
27-
var buildType
28-
var config
29-
var arch
30-
var nodeDir
31-
var guessedSolution
25+
const makeCommand = gyp.opts.make || process.env.MAKE || platformMake
26+
let command = win ? 'msbuild' : makeCommand
27+
const jobs = gyp.opts.jobs || process.env.JOBS
28+
let buildType
29+
let config
30+
let arch
31+
let nodeDir
32+
let guessedSolution
3233

33-
loadConfigGypi()
34+
await loadConfigGypi()
3435

3536
/**
3637
* Load the "config.gypi" file that was generated during "configure".
3738
*/
3839

39-
function loadConfigGypi () {
40-
var configPath = path.resolve('build', 'config.gypi')
41-
42-
fs.readFile(configPath, 'utf8', function (err, data) {
43-
if (err) {
44-
if (err.code === 'ENOENT') {
45-
callback(new Error('You must run `node-gyp configure` first!'))
46-
} else {
47-
callback(err)
48-
}
49-
return
40+
async function loadConfigGypi () {
41+
let data
42+
try {
43+
const configPath = path.resolve('build', 'config.gypi')
44+
data = await fs.readFile(configPath, 'utf8')
45+
} catch (err) {
46+
if (err.code === 'ENOENT') {
47+
throw new Error('You must run `node-gyp configure` first!')
48+
} else {
49+
throw err
5050
}
51-
config = JSON.parse(data.replace(/#.+\n/, ''))
51+
}
5252

53-
// get the 'arch', 'buildType', and 'nodeDir' vars from the config
54-
buildType = config.target_defaults.default_configuration
55-
arch = config.variables.target_arch
56-
nodeDir = config.variables.nodedir
53+
config = JSON.parse(data.replace(/#.+\n/, ''))
5754

58-
if ('debug' in gyp.opts) {
59-
buildType = gyp.opts.debug ? 'Debug' : 'Release'
60-
}
61-
if (!buildType) {
62-
buildType = 'Release'
63-
}
55+
// get the 'arch', 'buildType', and 'nodeDir' vars from the config
56+
buildType = config.target_defaults.default_configuration
57+
arch = config.variables.target_arch
58+
nodeDir = config.variables.nodedir
6459

65-
log.verbose('build type', buildType)
66-
log.verbose('architecture', arch)
67-
log.verbose('node dev dir', nodeDir)
60+
if ('debug' in gyp.opts) {
61+
buildType = gyp.opts.debug ? 'Debug' : 'Release'
62+
}
63+
if (!buildType) {
64+
buildType = 'Release'
65+
}
6866

69-
if (win) {
70-
findSolutionFile()
71-
} else {
72-
doWhich()
73-
}
74-
})
67+
log.verbose('build type', buildType)
68+
log.verbose('architecture', arch)
69+
log.verbose('node dev dir', nodeDir)
70+
71+
if (win) {
72+
await findSolutionFile()
73+
} else {
74+
await doWhich()
75+
}
7576
}
7677

7778
/**
7879
* On Windows, find the first build/*.sln file.
7980
*/
8081

81-
function findSolutionFile () {
82-
glob('build/*.sln', function (err, files) {
83-
if (err) {
84-
return callback(err)
85-
}
86-
if (files.length === 0) {
87-
return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
88-
}
89-
guessedSolution = files[0]
90-
log.verbose('found first Solution file', guessedSolution)
91-
doWhich()
92-
})
82+
async function findSolutionFile () {
83+
const files = await glob('build/*.sln')
84+
if (files.length === 0) {
85+
throw new Error('Could not find *.sln file. Did you run "configure"?')
86+
}
87+
guessedSolution = files[0]
88+
log.verbose('found first Solution file', guessedSolution)
89+
await doWhich()
9390
}
9491

9592
/**
9693
* Uses node-which to locate the msbuild / make executable.
9794
*/
9895

99-
function doWhich () {
96+
async function doWhich () {
10097
// On Windows use msbuild provided by node-gyp configure
10198
if (win) {
10299
if (!config.variables.msbuild_path) {
103-
return callback(new Error(
104-
'MSBuild is not set, please run `node-gyp configure`.'))
100+
throw new Error('MSBuild is not set, please run `node-gyp configure`.')
105101
}
106102
command = config.variables.msbuild_path
107103
log.verbose('using MSBuild:', command)
108-
doBuild()
104+
await doBuild()
109105
return
110106
}
107+
111108
// First make sure we have the build command in the PATH
112-
which(command, function (err, execPath) {
113-
if (err) {
114-
// Some other error or 'make' not found on Unix, report that to the user
115-
callback(err)
116-
return
117-
}
118-
log.verbose('`which` succeeded for `' + command + '`', execPath)
119-
doBuild()
120-
})
109+
const execPath = await which(command)
110+
log.verbose('`which` succeeded for `' + command + '`', execPath)
111+
await doBuild()
121112
}
122113

123114
/**
124115
* Actually spawn the process and compile the module.
125116
*/
126117

127-
function doBuild () {
118+
async function doBuild () {
128119
// Enable Verbose build
129-
var verbose = log.levels[log.level] <= log.levels.verbose
130-
var j
120+
const verbose = log.logger.isVisible('verbose')
121+
let j
131122

132123
if (!win && verbose) {
133124
argv.push('V=1')
@@ -147,10 +138,12 @@ function build (gyp, argv, callback) {
147138
// Convert .gypi config target_arch to MSBuild /Platform
148139
// Since there are many ways to state '32-bit Intel', default to it.
149140
// N.B. msbuild's Condition string equality tests are case-insensitive.
150-
var archLower = arch.toLowerCase()
151-
var p = archLower === 'x64' ? 'x64'
152-
: (archLower === 'arm' ? 'ARM'
153-
: (archLower === 'arm64' ? 'ARM64' : 'Win32'))
141+
const archLower = arch.toLowerCase()
142+
const p = archLower === 'x64'
143+
? 'x64'
144+
: (archLower === 'arm'
145+
? 'ARM'
146+
: (archLower === 'arm64' ? 'ARM64' : 'Win32'))
154147
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
155148
if (jobs) {
156149
j = parseInt(jobs, 10)
@@ -179,7 +172,7 @@ function build (gyp, argv, callback) {
179172

180173
if (win) {
181174
// did the user specify their own .sln file?
182-
var hasSln = argv.some(function (arg) {
175+
const hasSln = argv.some(function (arg) {
183176
return path.extname(arg) === '.sln'
184177
})
185178
if (!hasSln) {
@@ -194,20 +187,20 @@ function build (gyp, argv, callback) {
194187
log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`)
195188
}
196189

197-
var proc = gyp.spawn(command, argv)
198-
proc.on('exit', onExit)
199-
}
200-
201-
function onExit (code, signal) {
202-
if (code !== 0) {
203-
return callback(new Error('`' + command + '` failed with exit code: ' + code))
204-
}
205-
if (signal) {
206-
return callback(new Error('`' + command + '` got signal: ' + signal))
207-
}
208-
callback()
190+
const proc = gyp.spawn(command, argv)
191+
await new Promise((resolve, reject) => proc.on('exit', (code, signal) => {
192+
if (code !== 0) {
193+
return reject(new Error('`' + command + '` failed with exit code: ' + code))
194+
}
195+
if (signal) {
196+
return reject(new Error('`' + command + '` got signal: ' + signal))
197+
}
198+
resolve()
199+
}))
209200
}
210201
}
211202

212-
module.exports = build
203+
module.exports = function (gyp, argv, callback) {
204+
build(gyp, argv).then(callback.bind(undefined, null), callback)
205+
}
213206
module.exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'

lib/clean.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict'
22

3-
const rm = require('rimraf')
4-
const log = require('npmlog')
3+
const fs = require('fs/promises')
4+
const log = require('./log')
55

6-
function clean (gyp, argv, callback) {
6+
async function clean (gyp, argv) {
77
// Remove the 'build' dir
8-
var buildDir = 'build'
8+
const buildDir = 'build'
99

1010
log.verbose('clean', 'removing "%s" directory', buildDir)
11-
rm(buildDir, callback)
11+
await fs.rm(buildDir, { recursive: true, force: true })
1212
}
1313

14-
module.exports = clean
14+
module.exports = function (gyp, argv, callback) {
15+
clean(gyp, argv).then(callback.bind(undefined, null), callback)
16+
}
1517
module.exports.usage = 'Removes any generated build files and the "out" dir'

0 commit comments

Comments
 (0)