Created
October 23, 2021 13:45
-
-
Save replete/c6460b2c6cbbac6fe567c59cfb931273 to your computer and use it in GitHub Desktop.
Beginnings of a node-based bootstrapper for apps, abandoned for better bash scripting but there is a handy log function here and a few patterns useful when working with child_process sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const exec = (cmd) => require('child_process').execSync(cmd, { shell: '/bin/bash' }) | |
try { | |
let nodePath = exec('which node').toString().replace('\n', '') | |
let nodeVer = exec('node -v').toString().replace('\n', '') | |
const expectedNodeVer = fs.readFileSync('.nvmrc', 'utf-8').replace('\n', '') | |
const npmVer = exec('npm -v').toString().replace('\n', '') | |
const nodeVerComparison = versionCompare(nodeVer.substring(1), expectedNodeVer.substring(1)) | |
log('INFO', `Detected node ${nodeVer} at ${nodePath}`) | |
log('INFO', `Detected npm ${npmVer}`) | |
if (nodeVerComparison === -1) { | |
log('ERROR', `Using node ${bold(nodeVer)}, expected ${bold(expectedNodeVer)}`) | |
log('>', 'Checking for Node Version Manager (nvm)...') | |
const nvmInfo = exec('command -v nvm').toString().replace('\n', '') | |
log('>', `Detected nvm at ${nvmInfo}`) | |
log('>', `nvm: Switching to ${expectedNodeVer}...`) | |
exec(`npm run setup-env`) | |
// execSync(`nvm use ${expectedNodeVer}`) | |
// let exitCode = spawn(`nvm install ${expectedNodeVer}`) | |
log( | |
'INFO', | |
`Detected node ${exec('source ~/.bashrc && node -v') | |
.toString() | |
.replace('\n', '')} at ${nodePath}`, | |
) | |
} else if (nodeVerComparison === 1) { | |
log('WARN', `node ${bold(nodeVer)} is newer, expected ${bold(expectedNodeVer)} in .nvmrc`) | |
} else { | |
log('OK', `Using node ${bold(nodeVer)} (npm ${npmVer}) at ${ul(nodePath)}`) | |
} | |
log('OK', nodeVer) | |
log('OK', expectedNodeVer) | |
log(exec('which bash && $0')) | |
} catch (err) { | |
log('ERROR', err) | |
} | |
function log(level, msg) { | |
const levels = { | |
INFO: `\x1b[37m[INFO]\x1b[0m`, | |
OK: '[\x1b[32mOK\x1b[0m]', | |
WARN: '[\x1b[33mWARN\x1b[0m]', | |
ERROR: '[\x1b[31mERROR\x1b[0m]', | |
} | |
const timestamp = new Date().toISOString().slice(11).substring(8, 0) | |
console.log(`${levels[level] || `[${level}]`} ${msg || ''}`) | |
} | |
function ul(str) { | |
return `\x1b[4m${str}\x1b[0m` | |
} | |
function bold(str) { | |
return `\x1b[1m${str}\x1b[0m` | |
} | |
function versionCompare(v1, v2) { | |
const v1Parts = v1.split('.') | |
const v2Parts = v2.split('.') | |
const length = Math.max(v1Parts.length, v2Parts.length) | |
for (let i = 0; i < length; i++) { | |
const value = (parseInt(v1Parts[i]) || 0) - (parseInt(v2Parts[i]) || 0) | |
if (value < 0) return -1 | |
if (value > 0) return 1 | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment