2021-03-30 17:16:46 +03:00
|
|
|
const https = require('https')
|
|
|
|
const spawn = require('child_process').spawn
|
2021-11-05 14:51:43 +03:00
|
|
|
const exec = require('child_process').exec
|
2021-03-30 17:16:46 +03:00
|
|
|
|
|
|
|
function download(url) {
|
2021-11-05 14:51:43 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
https
|
|
|
|
.get(url, res => {
|
|
|
|
let data = ''
|
|
|
|
res.on('data', chunk => (data += chunk))
|
|
|
|
res.on('end', () => resolve(data))
|
|
|
|
})
|
|
|
|
.on('error', error => reject(error))
|
2021-03-30 17:16:46 +03:00
|
|
|
})
|
|
|
|
}
|
2020-03-05 08:25:00 +03:00
|
|
|
|
|
|
|
function section(title) {
|
|
|
|
let border = '-'.repeat(8 + title.length)
|
|
|
|
let middle = '--- ' + title + ' ---'
|
|
|
|
console.log()
|
|
|
|
console.log()
|
|
|
|
console.log()
|
|
|
|
console.log(border)
|
|
|
|
console.log(middle)
|
|
|
|
console.log(border)
|
|
|
|
console.log()
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
async function with_cwd(dir, fn) {
|
2020-03-05 08:25:00 +03:00
|
|
|
let cwd = process.cwd()
|
|
|
|
process.chdir(dir)
|
|
|
|
let out = await fn()
|
|
|
|
process.chdir(cwd)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
function run(cmd, args) {
|
2020-03-05 08:25:00 +03:00
|
|
|
let out = ''
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
console.log(`Calling '${cmd} ${args.join(' ')}'`)
|
2021-11-05 14:51:43 +03:00
|
|
|
let proc = spawn(cmd, args, { stdio: 'inherit', shell: true })
|
|
|
|
proc.on('exit', code => {
|
2020-03-27 15:25:55 +03:00
|
|
|
if (code) process.exit(code)
|
|
|
|
resolve(out)
|
|
|
|
})
|
2020-03-05 08:25:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
function run_read(cmd, args) {
|
2020-03-05 08:25:00 +03:00
|
|
|
let out = ''
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-05 14:51:43 +03:00
|
|
|
let proc = spawn(cmd, args, { shell: true })
|
2020-03-05 08:25:00 +03:00
|
|
|
proc.stderr.pipe(process.stderr)
|
2021-11-05 14:51:43 +03:00
|
|
|
proc.stdout.on('data', data => {
|
|
|
|
out += data
|
|
|
|
})
|
|
|
|
proc.on('exit', code => {
|
|
|
|
if (code) process.exit(code)
|
2020-03-27 15:25:55 +03:00
|
|
|
resolve(out)
|
|
|
|
})
|
2020-03-05 08:25:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
async function check_version(name, required, cfg) {
|
|
|
|
if (!cfg) {
|
|
|
|
cfg = {}
|
|
|
|
}
|
|
|
|
let version = await run_read(name, ['--version'])
|
|
|
|
version = version.trim()
|
|
|
|
if (cfg.preprocess) {
|
|
|
|
version = cfg.preprocess(version)
|
|
|
|
}
|
2020-03-05 08:25:00 +03:00
|
|
|
if (cfg.silent !== true) {
|
2020-10-26 04:18:40 +03:00
|
|
|
console.log(`Checking if '${name}' version is '${required}'.`)
|
2020-03-05 08:25:00 +03:00
|
|
|
}
|
|
|
|
if (version != required) {
|
|
|
|
throw `[ERROR] The '${name}' version '${version}' does not match the required one '${required}'.`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
async function get_npm_info(name) {
|
|
|
|
let info = await run_read('npm', ['info', name, '--json'])
|
2020-10-26 04:18:40 +03:00
|
|
|
return JSON.parse(info)
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
async function get_npm_lts_version_of(name) {
|
2020-10-26 04:18:40 +03:00
|
|
|
let info = await get_npm_info(name)
|
2021-11-05 14:51:43 +03:00
|
|
|
version = info['dist-tags'].lts
|
2020-10-26 04:18:40 +03:00
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:16:46 +03:00
|
|
|
async function get_node_dist_index() {
|
|
|
|
let index = await download('https://nodejs.org/dist/index.json')
|
|
|
|
return JSON.parse(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function get_node_lts_version() {
|
|
|
|
let index = await get_node_dist_index()
|
|
|
|
let newest = null
|
|
|
|
for (let entry of index) {
|
|
|
|
if (entry.lts !== false) {
|
|
|
|
newest = entry
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!newest) {
|
2021-11-05 14:51:43 +03:00
|
|
|
throw 'Cannot fetch the info about node LTS version.'
|
2021-03-30 17:16:46 +03:00
|
|
|
}
|
|
|
|
let node = newest.version
|
2021-11-05 14:51:43 +03:00
|
|
|
let npm = newest.npm
|
|
|
|
return [node, npm]
|
2021-03-30 17:16:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-05 14:51:43 +03:00
|
|
|
module.exports = {
|
|
|
|
section,
|
|
|
|
run,
|
|
|
|
run_read,
|
|
|
|
check_version,
|
|
|
|
get_npm_info,
|
|
|
|
get_npm_lts_version_of,
|
|
|
|
with_cwd,
|
|
|
|
get_node_dist_index,
|
|
|
|
get_node_lts_version,
|
|
|
|
}
|