Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync deps and engines with npm #2770

Merged
merged 8 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions bin/node-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ process.title = 'node-gyp'

const envPaths = require('env-paths')
const gyp = require('../')
const log = require('npmlog')
const log = require('../lib/log')
const os = require('os')

/**
* Process and execute the selected commands.
*/

const prog = gyp()
var completed = false
let completed = false
prog.parseArgv(process.argv)
prog.devDir = prog.opts.devdir

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

var dir = prog.opts.directory
const dir = prog.opts.directory
if (dir) {
var fs = require('fs')
const fs = require('fs')
try {
var stat = fs.statSync(dir)
const stat = fs.statSync(dir)
if (stat.isDirectory()) {
log.info('chdir', dir)
process.chdir(dir)
Expand All @@ -69,7 +69,7 @@ if (dir) {
}

function run () {
var command = prog.todo.shift()
const command = prog.todo.shift()
if (!command) {
// done!
completed = true
Expand All @@ -86,7 +86,7 @@ function run () {
return process.exit(1)
}
if (command.name === 'list') {
var versions = arguments[1]
const versions = arguments[1]
if (versions.length > 0) {
versions.forEach(function (version) {
console.log(version)
Expand Down Expand Up @@ -120,7 +120,7 @@ process.on('uncaughtException', function (err) {

function errorMessage () {
// copied from npm's lib/utils/error-handler.js
var os = require('os')
const os = require('os')
log.error('System', os.type() + ' ' + os.release())
log.error('command', process.argv
.map(JSON.stringify).join(' '))
Expand Down
175 changes: 84 additions & 91 deletions lib/build.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

const fs = require('graceful-fs')
const fs = require('graceful-fs').promises
const { promisify } = require('util')
const path = require('path')
const glob = require('glob')
const log = require('npmlog')
const glob = promisify(require('glob'))
const log = require('./log')
const which = require('which')
const win = process.platform === 'win32'

function build (gyp, argv, callback) {
var platformMake = 'make'
async function build (gyp, argv) {
let platformMake = 'make'
if (process.platform === 'aix') {
platformMake = 'gmake'
} else if (process.platform === 'os400') {
Expand All @@ -21,113 +22,103 @@ function build (gyp, argv, callback) {
})
}

var makeCommand = gyp.opts.make || process.env.MAKE || platformMake
var command = win ? 'msbuild' : makeCommand
var jobs = gyp.opts.jobs || process.env.JOBS
var buildType
var config
var arch
var nodeDir
var guessedSolution
const makeCommand = gyp.opts.make || process.env.MAKE || platformMake
let command = win ? 'msbuild' : makeCommand
const jobs = gyp.opts.jobs || process.env.JOBS
let buildType
let config
let arch
let nodeDir
let guessedSolution

loadConfigGypi()
await loadConfigGypi()

/**
* Load the "config.gypi" file that was generated during "configure".
*/

function loadConfigGypi () {
var configPath = path.resolve('build', 'config.gypi')

fs.readFile(configPath, 'utf8', function (err, data) {
if (err) {
if (err.code === 'ENOENT') {
callback(new Error('You must run `node-gyp configure` first!'))
} else {
callback(err)
}
return
async function loadConfigGypi () {
let data
try {
const configPath = path.resolve('build', 'config.gypi')
data = await fs.readFile(configPath, 'utf8')
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('You must run `node-gyp configure` first!')
} else {
throw err
}
config = JSON.parse(data.replace(/#.+\n/, ''))
}

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

if ('debug' in gyp.opts) {
buildType = gyp.opts.debug ? 'Debug' : 'Release'
}
if (!buildType) {
buildType = 'Release'
}
// get the 'arch', 'buildType', and 'nodeDir' vars from the config
buildType = config.target_defaults.default_configuration
arch = config.variables.target_arch
nodeDir = config.variables.nodedir

log.verbose('build type', buildType)
log.verbose('architecture', arch)
log.verbose('node dev dir', nodeDir)
if ('debug' in gyp.opts) {
buildType = gyp.opts.debug ? 'Debug' : 'Release'
}
if (!buildType) {
buildType = 'Release'
}

if (win) {
findSolutionFile()
} else {
doWhich()
}
})
log.verbose('build type', buildType)
log.verbose('architecture', arch)
log.verbose('node dev dir', nodeDir)

if (win) {
await findSolutionFile()
} else {
await doWhich()
}
}

/**
* On Windows, find the first build/*.sln file.
*/

function findSolutionFile () {
glob('build/*.sln', function (err, files) {
if (err) {
return callback(err)
}
if (files.length === 0) {
return callback(new Error('Could not find *.sln file. Did you run "configure"?'))
}
guessedSolution = files[0]
log.verbose('found first Solution file', guessedSolution)
doWhich()
})
async function findSolutionFile () {
const files = await glob('build/*.sln')
if (files.length === 0) {
throw new Error('Could not find *.sln file. Did you run "configure"?')
}
guessedSolution = files[0]
log.verbose('found first Solution file', guessedSolution)
await doWhich()
}

/**
* Uses node-which to locate the msbuild / make executable.
*/

function doWhich () {
async function doWhich () {
// On Windows use msbuild provided by node-gyp configure
if (win) {
if (!config.variables.msbuild_path) {
return callback(new Error(
'MSBuild is not set, please run `node-gyp configure`.'))
throw new Error('MSBuild is not set, please run `node-gyp configure`.')
}
command = config.variables.msbuild_path
log.verbose('using MSBuild:', command)
doBuild()
await doBuild()
return
}

// First make sure we have the build command in the PATH
which(command, function (err, execPath) {
if (err) {
// Some other error or 'make' not found on Unix, report that to the user
callback(err)
return
}
log.verbose('`which` succeeded for `' + command + '`', execPath)
doBuild()
})
const execPath = await which(command)
log.verbose('`which` succeeded for `' + command + '`', execPath)
await doBuild()
}

/**
* Actually spawn the process and compile the module.
*/

function doBuild () {
async function doBuild () {
// Enable Verbose build
var verbose = log.levels[log.level] <= log.levels.verbose
var j
const verbose = log.logger.isVisible('verbose')
let j

if (!win && verbose) {
argv.push('V=1')
Expand All @@ -147,10 +138,12 @@ function build (gyp, argv, callback) {
// Convert .gypi config target_arch to MSBuild /Platform
// Since there are many ways to state '32-bit Intel', default to it.
// N.B. msbuild's Condition string equality tests are case-insensitive.
var archLower = arch.toLowerCase()
var p = archLower === 'x64' ? 'x64'
: (archLower === 'arm' ? 'ARM'
: (archLower === 'arm64' ? 'ARM64' : 'Win32'))
const archLower = arch.toLowerCase()
const p = archLower === 'x64'
? 'x64'
: (archLower === 'arm'
? 'ARM'
: (archLower === 'arm64' ? 'ARM64' : 'Win32'))
argv.push('/p:Configuration=' + buildType + ';Platform=' + p)
if (jobs) {
j = parseInt(jobs, 10)
Expand Down Expand Up @@ -179,7 +172,7 @@ function build (gyp, argv, callback) {

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

var proc = gyp.spawn(command, argv)
proc.on('exit', onExit)
}

function onExit (code, signal) {
if (code !== 0) {
return callback(new Error('`' + command + '` failed with exit code: ' + code))
}
if (signal) {
return callback(new Error('`' + command + '` got signal: ' + signal))
}
callback()
const proc = gyp.spawn(command, argv)
await new Promise((resolve, reject) => proc.on('exit', (code, signal) => {
if (code !== 0) {
return reject(new Error('`' + command + '` failed with exit code: ' + code))
}
if (signal) {
return reject(new Error('`' + command + '` got signal: ' + signal))
}
resolve()
}))
}
}

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

const rm = require('rimraf')
const log = require('npmlog')
const fs = require('fs/promises')
const log = require('./log')

function clean (gyp, argv, callback) {
async function clean (gyp, argv) {
// Remove the 'build' dir
var buildDir = 'build'
const buildDir = 'build'

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

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