Skip to content

Commit 355622f

Browse files
committed
feat: convert all internal functions to async/await
BREAKING CHANGE: All internal functions have been coverted to return promises and no longer accept callbacks. This is not a breaking change for users but may be breaking to consumers of `node-gyp` if you are requiring internal functions directly.
1 parent 1b3bd34 commit 355622f

20 files changed

+767
-1061
lines changed

bin/node-gyp.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ if (dir) {
6868
}
6969
}
7070

71-
function run () {
71+
async function run () {
7272
const command = prog.todo.shift()
7373
if (!command) {
7474
// done!
@@ -77,30 +77,28 @@ function run () {
7777
return
7878
}
7979

80-
prog.commands[command.name](command.args, function (err) {
81-
if (err) {
82-
log.error(command.name + ' error')
83-
log.error('stack', err.stack)
84-
errorMessage()
85-
log.error('not ok')
86-
return process.exit(1)
87-
}
80+
try {
81+
const args = await prog.commands[command.name](command.args) ?? []
82+
8883
if (command.name === 'list') {
89-
const versions = arguments[1]
90-
if (versions.length > 0) {
91-
versions.forEach(function (version) {
92-
console.log(version)
93-
})
84+
if (args.length) {
85+
args.forEach((version) => console.log(version))
9486
} else {
9587
console.log('No node development files installed. Use `node-gyp install` to install a version.')
9688
}
97-
} else if (arguments.length >= 2) {
98-
console.log.apply(console, [].slice.call(arguments, 1))
89+
} else if (args.length >= 1) {
90+
console.log(...args.slice(1))
9991
}
10092

10193
// now run the next command in the queue
102-
process.nextTick(run)
103-
})
94+
return run()
95+
} catch (err) {
96+
log.error(command.name + ' error')
97+
log.error('stack', err.stack)
98+
errorMessage()
99+
log.error('not ok')
100+
return process.exit(1)
101+
}
104102
}
105103

106104
process.on('exit', function (code) {

lib/build.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,7 @@ async function build (gyp, argv) {
202202
await new Promise((resolve, reject) => proc.on('exit', async (code, signal) => {
203203
if (buildBinsDir) {
204204
// Clean up the build-time dependency symlinks:
205-
if (fs.rm) {
206-
// fs.rm is only available in Node 14+
207-
await fs.rm(buildBinsDir, { recursive: true })
208-
} else {
209-
// Only used for old node, as recursive rmdir is deprecated in Node 14+
210-
await fs.rmdir(buildBinsDir, { recursive: true })
211-
}
205+
await fs.rm(buildBinsDir, { recursive: true })
212206
}
213207

214208
if (code !== 0) {
@@ -222,7 +216,5 @@ async function build (gyp, argv) {
222216
}
223217
}
224218

225-
module.exports = function (gyp, argv, callback) {
226-
build(gyp, argv).then(callback.bind(undefined, null), callback)
227-
}
219+
module.exports = build
228220
module.exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'

lib/clean.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const fs = require('fs/promises')
3+
const fs = require('graceful-fs').promises
44
const log = require('./log')
55

66
async function clean (gyp, argv) {
@@ -11,7 +11,5 @@ async function clean (gyp, argv) {
1111
await fs.rm(buildDir, { recursive: true, force: true })
1212
}
1313

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

0 commit comments

Comments
 (0)