Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Created March 3, 2017 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisabrams/99e5d4591156f23d93ff0faec86b9227 to your computer and use it in GitHub Desktop.
Save chrisabrams/99e5d4591156f23d93ff0faec86b9227 to your computer and use it in GitHub Desktop.
Bash script to update project dependencies quickly
#!/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}`)
}
@Rubenmp
Copy link

Rubenmp commented Aug 22, 2017

It has a mistake, search 'depenencies' in file, I can not modify it to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment