const path = require('path')
const resolver = require('./install/resolver')
const installer = require('./install/installer')
const nodeGyp = require('./utils/node-gyp')
const rimraf = require('rimraf')
const nm = require('./utils/nm')

global.dependenciesCount = 0
global.dependenciesTree = {}
global.nativeBuildQueue = []
global.time = process.hrtime()

const install = (argv) => {
  argv._handled = true
  const pkgJSON = require(path.join(process.cwd(), 'package.json'))
  const dependencies = pkgJSON.dependencies || {}
  const devDependencies = pkgJSON.devDependencies || {}
  const optionalDependencies = pkgJSON.optionalDependencies || {}
  const allDependencies = Object.assign(
    optionalDependencies,
    devDependencies,
    dependencies
  )
  const deps = allDependencies
  const list = resolver(deps)
  process.stdout.write('Resolving dependencies\n')
  Promise.all(list).then(() => {
    const tasks = installer(global.dependenciesTree)
    process.stdout.write('Installing dependencies\n')
    rimraf.sync(nm)
    Promise.all(tasks).then(() => {
      global.nativeBuildQueue.forEach((cwd) => {
        try {
          process.stdout.write('Building dependencies\n')
          nodeGyp({cwd: cwd})
        } catch (e) {
          // remove the pkg since the deps could be optional
          rimraf.sync(cwd)
        }
      })
      const duration = process.hrtime(global.time)
      const time = duration[0] + duration[1] / 1e9
      const s = Math.round(time * 1000) / 1000
      process.stdout.write(
        `Installed ${global.dependenciesCount} packages in ${s}s\n`
      )
    }).catch((e) => { process.stderr.write(e.stack) })
  }).catch((e) => { process.stderr.write(e.stack) })
}

module.exports = {
  command: 'install',
  describe: 'Install dependencies defined in package.json',
  handler: install,
  aliases: ['i']
}

@@ -399,16 +399,95 @@
  = true%0A
+  const only = argv.only === 'dev' %7C%7C argv.only === 'prod' ? argv.only : 'all'%0A
   const 
@@ -554,17 +554,25 @@
   const 
-d
+optionalD
 ependenc
@@ -585,17 +585,25 @@
 pkgJSON.
-d
+optionalD
 ependenc
@@ -620,19 +620,19 @@
   const 
-dev
+all
 Dependen
@@ -642,126 +642,170 @@
 s = 
-pkgJSON.devDependencies %7C%7C %7B%7D%0A  const optionalDependencies = pkgJSON.optionalDependencies %7C%7C %7B%7D%0A  const allDependencie
+%7B%0A    all: %5B%0A      'dependencies',%0A      'devDependencies'%0A    %5D,%0A    'prod': %5B%0A      'dependencies'%0A    %5D,%0A    dev: %5B%0A      'devDependencies'%0A    %5D%0A  %7D%0A  var dep
 s = 
@@ -818,20 +818,19 @@
 .assign(
-%0A   
+%7B%7D,
  optiona
@@ -842,25 +842,23 @@
 ndencies
-,%0A    dev
+)%0A  all
 Dependen
@@ -865,61 +865,106 @@
 cies
-,%0A    dependencies%0A  )%0A  const deps = allDependencies
+%5Bonly%5D.forEach((key) =%3E %7B%0A    if (!pkgJSON%5Bkey%5D) return%0A    Object.assign(deps, pkgJSON%5Bkey%5D)%0A  %7D)
 %0A  c
@@ -1919,16 +1919,34 @@
 'install
+ %5B--only=prod%7Cdev%5D
 ',%0A  des

add --only option