Created
December 5, 2016 07:54
-
-
Save garretfick/477918d4ac5c6e6596d7e72cdcb7e4ce to your computer and use it in GitHub Desktop.
Install npm pacakges from a list
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
| // 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