summaryrefslogtreecommitdiff
path: root/deps/npm/lib/install/actions.js
blob: a34d03ffe214651c7d191960a9e329b656ae81a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'

const BB = require('bluebird')

const andAddParentToErrors = require('./and-add-parent-to-errors.js')
const failedDependency = require('./deps.js').failedDependency
const isInstallable = BB.promisify(require('./validate-args.js').isInstallable)
const moduleName = require('../utils/module-name.js')
const npm = require('../npm.js')
const reportOptionalFailure = require('./report-optional-failure.js')
const validate = require('aproba')

const actions = {}

actions.fetch = require('./action/fetch.js')
actions.extract = require('./action/extract.js')
actions.build = require('./action/build.js')
actions.preinstall = require('./action/preinstall.js')
actions.install = require('./action/install.js')
actions.postinstall = require('./action/postinstall.js')
actions.prepare = require('./action/prepare.js')
actions.finalize = require('./action/finalize.js')
actions.remove = require('./action/remove.js')
actions.unbuild = require('./action/unbuild.js')
actions.move = require('./action/move.js')
actions['global-install'] = require('./action/global-install.js')
actions['global-link'] = require('./action/global-link.js')
actions['refresh-package-json'] = require('./action/refresh-package-json.js')

// FIXME: We wrap actions like three ways to sunday here.
// Rewrite this to only work one way.

Object.keys(actions).forEach(function (actionName) {
  var action = actions[actionName]
  actions[actionName] = (staging, pkg, log) => {
    validate('SOO', [staging, pkg, log])
    // refuse to run actions for failed packages
    if (pkg.failed) return BB.resolve()
    if (action.rollback) {
      if (!pkg.rollback) pkg.rollback = []
      pkg.rollback.unshift(action.rollback)
    }
    if (action.commit) {
      if (!pkg.commit) pkg.commit = []
      pkg.commit.push(action.commit)
    }

    let actionP
    if (pkg.knownInstallable) {
      actionP = runAction(action, staging, pkg, log)
    } else {
      actionP = isInstallable(pkg.package).then(() => {
        pkg.knownInstallable = true
        return runAction(action, staging, pkg, log)
      })
    }

    return actionP.then(() => {
      log.finish()
    }, (err) => {
      return BB.fromNode((cb) => {
        andAddParentToErrors(pkg.parent, cb)(err)
      }).catch((err) => {
        return handleOptionalDepErrors(pkg, err)
      })
    })
  }
  actions[actionName].init = action.init || (() => BB.resolve())
  actions[actionName].teardown = action.teardown || (() => BB.resolve())
})
exports.actions = actions

function runAction (action, staging, pkg, log) {
  return BB.fromNode((cb) => {
    const result = action(staging, pkg, log, cb)
    if (result && result.then) {
      result.then(() => cb(), cb)
    }
  })
}

function markAsFailed (pkg) {
  if (pkg.failed) return
  pkg.failed = true
  pkg.requires.forEach((req) => {
    var requiredBy = req.requiredBy.filter((reqReqBy) => !reqReqBy.failed)
    if (requiredBy.length === 0 && !req.userRequired) {
      markAsFailed(req)
    }
  })
}

function handleOptionalDepErrors (pkg, err) {
  markAsFailed(pkg)
  var anyFatal = failedDependency(pkg)
  if (anyFatal) {
    throw err
  } else {
    reportOptionalFailure(pkg, null, err)
  }
}

exports.doOne = doOne
function doOne (cmd, staging, pkg, log, next) {
  validate('SSOOF', arguments)
  const prepped = prepareAction([cmd, pkg], staging, log)
  return withInit(actions[cmd], () => {
    return execAction(prepped)
  }).nodeify(next)
}

exports.doParallel = doParallel
function doParallel (type, staging, actionsToRun, log, next) {
  validate('SSAOF', arguments)
  const acts = actionsToRun.reduce((acc, todo) => {
    if (todo[0] === type) {
      acc.push(prepareAction(todo, staging, log))
    }
    return acc
  }, [])
  log.silly('doParallel', type + ' ' + acts.length)
  time(log)
  if (!acts.length) { return next() }
  return withInit(actions[type], () => {
    return BB.map(acts, execAction, {
      concurrency: npm.limit.action
    })
  }).nodeify((err) => {
    log.finish()
    timeEnd(log)
    next(err)
  })
}

exports.doSerial = doSerial
function doSerial (type, staging, actionsToRun, log, next) {
  validate('SSAOF', arguments)
  log.silly('doSerial', '%s %d', type, actionsToRun.length)
  runSerial(type, staging, actionsToRun, log, next)
}

exports.doReverseSerial = doReverseSerial
function doReverseSerial (type, staging, actionsToRun, log, next) {
  validate('SSAOF', arguments)
  log.silly('doReverseSerial', '%s %d', type, actionsToRun.length)
  runSerial(type, staging, [].concat(actionsToRun).reverse(), log, next)
}

function runSerial (type, staging, actionsToRun, log, next) {
  const acts = actionsToRun.reduce((acc, todo) => {
    if (todo[0] === type) {
      acc.push(prepareAction(todo, staging, log))
    }
    return acc
  }, [])
  time(log)
  if (!acts.length) { return next() }
  return withInit(actions[type], () => {
    return BB.each(acts, execAction)
  }).nodeify((err) => {
    log.finish()
    timeEnd(log)
    next(err)
  })
}

function time (log) {
  process.emit('time', 'action:' + log.name)
}
function timeEnd (log) {
  process.emit('timeEnd', 'action:' + log.name)
}

function withInit (action, body) {
  return BB.using(
    action.init().disposer(() => action.teardown()),
    body
  )
}

function prepareAction (action, staging, log) {
  validate('ASO', arguments)
  validate('SO', action)
  var cmd = action[0]
  var pkg = action[1]
  if (!actions[cmd]) throw new Error('Unknown decomposed command "' + cmd + '" (is it new?)')
  return [actions[cmd], staging, pkg, log.newGroup(cmd + ':' + moduleName(pkg))]
}

function execAction (todo) {
  return todo[0].apply(null, todo.slice(1))
}