Created
March 3, 2017 13:37
-
-
Save chrisabrams/99e5d4591156f23d93ff0faec86b9227 to your computer and use it in GitHub Desktop.
Bash script to update project dependencies quickly
This file contains 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
#!/usr/bin/env node | |
/* | |
Usage: | |
Install all production depenencies to their current version: ~/npm/install --prod | |
Install all production depenencies to their latest: ~/npm/install --prod --latest | |
Install all production depenencies to their latest and update package.json: ~/npm/install --prod --latest --save | |
Install all development depenencies to their current version: ~/npm/install --dev | |
Install all development depenencies to their latest: ~/npm/install --dev --latest | |
Install all development depenencies to their latest and update package.json: ~/npm/install --dev --latest --save | |
*/ | |
const argv = require('minimist')(process.argv.slice(2)) | |
const latest = argv.latest | |
const iDev = argv.dev | |
const iProd = argv.prod | |
const execSync = require('child_process').execSync | |
const path = require('path') | |
function exec(cmd) { | |
execSync(cmd, {stdio: [0,1,2]}) | |
} | |
const pkg = require(path.join(process.cwd(), 'package.json')) | |
// Install production dependencies | |
if(iProd) { | |
const dependencies = [] | |
const keys = Object.keys(pkg.dependencies) | |
for(let i = 0, l = keys.length; i <l; i++) { | |
const key = keys[i] | |
const val = (latest) ? 'latest' : pkg.dependencies[key] | |
dependencies.push(`${key}@${val}`) | |
} | |
const dependenciesString = dependencies.join(' ') | |
const save = (argv.save) ? '--save' : '' | |
exec(`npm install ${dependenciesString} ${save}`) | |
} | |
// Install development dependencies | |
if(iDev) { | |
const devDependencies = [] | |
const keys = Object.keys(pkg.devDependencies) | |
for(let i = 0, l = keys.length; i <l; i++) { | |
const key = keys[i] | |
const val = (latest) ? 'latest' : pkg.dependencies[key] | |
devDependencies.push(`${key}@${val}`) | |
} | |
const devDependenciesString = devDependencies.join(' ') | |
const devSave = (argv.save) ? '--save-dev' : '' | |
exec(`npm install ${devDependenciesString} ${devSave}`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It has a mistake, search 'depenencies' in file, I can not modify it to help.