brave-browser/lib/util.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

const fs = require('fs')
2020-07-23 11:39:22 -07:00
const Log = require('./logging')
const { spawnSync } = require('child_process')
// This is the valid way of retrieving configuration values for NPM <= 6, with
// npm_package_config_* still working up to NPM 7, but no longer for NPM >= 8.
// See https://github.com/npm/rfcs/blob/main/implemented/0021-reduce-lifecycle-script-environment.md
const getNPMConfigFromEnv = (path) => {
const key = path.join('_')
// Npm <= 6 did not preserve dashes in package.json keys
const keyNoDashes = key.replace('-', '_')
2020-07-23 11:39:22 -07:00
const npm_prefix = 'npm_config_'
const package_config_prefix = 'npm_package_config_'
const package_prefix = 'npm_package_'
return process.env[npm_prefix + keyNoDashes] ||
process.env[package_config_prefix + keyNoDashes] ||
2020-07-23 11:39:22 -07:00
process.env[package_config_prefix + key] ||
process.env[package_prefix + keyNoDashes] ||
2021-11-26 10:49:39 -05:00
process.env[package_prefix + key]
2016-11-09 21:02:49 -07:00
}
// From NPM >= 8, we need to inspect the package.json file, which should
// be available via the 'npm_package_json' environment variable.
const getNPMConfigFromPackageJson = (path) => {
let packages = { config: {} }
if (fs.existsSync(process.env['npm_package_json'])) {
packages = require(process.env['npm_package_json'])
}
let obj = packages.config
for (var i = 0, len = path.length; i < len; i++) {
if (!obj) {
return obj
}
obj = obj[path[i]]
}
return obj
}
const getNPMConfig = (path) => {
return getNPMConfigFromEnv(path) || getNPMConfigFromPackageJson(path)
}
2020-07-23 11:39:22 -07:00
const getProjectVersion = (projectName) => {
return getNPMConfig(['projects', projectName, 'tag']) || getNPMConfig(['projects', projectName, 'branch'])
2016-11-09 21:02:49 -07:00
}
2020-07-23 11:39:22 -07:00
const run = (cmd, args = [], options = {}) => {
const { continueOnFail, ...cmdOptions } = options
Log.command(cmdOptions.cwd, cmd, args)
const prog = spawnSync(cmd, args, cmdOptions)
if (prog.status !== 0) {
if (!continueOnFail) {
console.log(prog.stdout && prog.stdout.toString())
console.error(prog.stderr && prog.stderr.toString())
process.exit(1)
}
}
2020-07-23 11:39:22 -07:00
return prog
}
2020-07-23 11:39:22 -07:00
const runGit = (repoPath, gitArgs, continueOnFail = false) => {
let prog = run('git', gitArgs, { cwd: repoPath, continueOnFail })
2020-07-23 11:39:22 -07:00
if (prog.status !== 0) {
return null
} else {
return prog.stdout.toString().trim()
2016-11-27 19:29:59 -07:00
}
2016-11-09 21:02:49 -07:00
}
2020-07-23 11:39:22 -07:00
module.exports = {
getNPMConfig,
getProjectVersion,
run,
runGit
}