Skip to content

Commit

Permalink
feat: convert all internal functions to async/await
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukekarrys committed Oct 28, 2023
1 parent 7cba0db commit 56e7357
Show file tree
Hide file tree
Showing 20 changed files with 770 additions and 1,062 deletions.
36 changes: 17 additions & 19 deletions bin/node-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if (dir) {
}
}

function run () {
async function run () {
const command = prog.todo.shift()
if (!command) {
// done!
Expand All @@ -77,35 +77,33 @@ function run () {
return
}

prog.commands[command.name](command.args, function (err) {
if (err) {
log.error(command.name + ' error')
log.error('stack', err.stack)
errorMessage()
log.error('not ok')
return process.exit(1)
}
try {
const args = await prog.commands[command.name](command.args) ?? []

if (command.name === 'list') {
const versions = arguments[1]
if (versions.length > 0) {
versions.forEach(function (version) {
console.log(version)
})
if (args.length) {
args.forEach((version) => console.log(version))
} else {
console.log('No node development files installed. Use `node-gyp install` to install a version.')
}
} else if (arguments.length >= 2) {
console.log.apply(console, [].slice.call(arguments, 1))
} else if (args.length >= 1) {
console.log(...args.slice(1))
}

// now run the next command in the queue
process.nextTick(run)
})
return run()
} catch (err) {
log.error(command.name + ' error')
log.error('stack', err.stack)
errorMessage()
log.error('not ok')
return process.exit(1)
}
}

process.on('exit', function (code) {
if (!completed && !code) {
log.error('Completion callback never invoked!')
log.error('Command callback never invoked!')
issueMessage()
process.exit(6)
}
Expand Down
12 changes: 2 additions & 10 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,7 @@ async function build (gyp, argv) {
await new Promise((resolve, reject) => proc.on('exit', async (code, signal) => {
if (buildBinsDir) {
// Clean up the build-time dependency symlinks:
if (fs.rm) {
// fs.rm is only available in Node 14+
await fs.rm(buildBinsDir, { recursive: true })
} else {
// Only used for old node, as recursive rmdir is deprecated in Node 14+
await fs.rmdir(buildBinsDir, { recursive: true })
}
await fs.rm(buildBinsDir, { recursive: true })
}

if (code !== 0) {
Expand All @@ -222,7 +216,5 @@ async function build (gyp, argv) {
}
}

module.exports = function (gyp, argv, callback) {
build(gyp, argv).then(callback.bind(undefined, null), callback)
}
module.exports = build
module.exports.usage = 'Invokes `' + (win ? 'msbuild' : 'make') + '` and builds the module'
6 changes: 2 additions & 4 deletions lib/clean.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const fs = require('fs/promises')
const fs = require('graceful-fs').promises
const log = require('./log')

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

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

0 comments on commit 56e7357

Please sign in to comment.