Skip to content

Instantly share code, notes, and snippets.

@garretfick
Created December 5, 2016 07:54
Show Gist options
  • Select an option

  • Save garretfick/477918d4ac5c6e6596d7e72cdcb7e4ce to your computer and use it in GitHub Desktop.

Select an option

Save garretfick/477918d4ac5c6e6596d7e72cdcb7e4ce to your computer and use it in GitHub Desktop.
Install npm pacakges from a list
// Installs NPM packages from a generated list
// 1. Generate the list with ls node_modules > modules.txt
// 2. Run node npm-one-by-one.js
// This is primarily useful for populating a Nexus proxy when npm install sometimes fails
const fs = require('fs')
const cp = require('child_process')
// The maximum time we try to install any package before giving an error
const TIMEOUT_SECONDS = 30
const modules = fs.readFileSync('modules.txt', 'utf8').split('\n')
modules.forEach(moduleName => {
let fsStat = null
try {
fsStat = fs.statSync('node_modules/' + moduleName)
} catch (e) {
// Do nothing, all the logic is following
}
if (fsStat !== null) {
console.log('Skipping ' + moduleName)
return
}
if (moduleName.startsWith('@')) {
return
}
console.log('Installing ' + moduleName)
let output = cp.spawnSync('npm', ['install', moduleName], {timeout: TIMEOUT_SECONDS * 1000})
if (output.status !== 0) {
console.log('Install failed')
console.log(output.stderr.toString())
} else {
console.log('Install done')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment