summaryrefslogtreecommitdiff
path: root/deps/npm/lib/owner.js
blob: a64cb5e14ccefbda64efcf2e1f9df5ddce964bd7 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
module.exports = owner

const BB = require('bluebird')

const log = require('npmlog')
const npa = require('libnpm/parse-arg')
const npmConfig = require('./config/figgy-config.js')
const npmFetch = require('libnpm/fetch')
const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const packument = require('libnpm/packument')
const readLocalPkg = BB.promisify(require('./utils/read-local-package.js'))
const usage = require('./utils/usage')
const whoami = BB.promisify(require('./whoami.js'))

owner.usage = usage(
  'owner',
  'npm owner add <user> [<@scope>/]<pkg>' +
  '\nnpm owner rm <user> [<@scope>/]<pkg>' +
  '\nnpm owner ls [<@scope>/]<pkg>'
)

owner.completion = function (opts, cb) {
  const argv = opts.conf.argv.remain
  if (argv.length > 4) return cb()
  if (argv.length <= 2) {
    var subs = ['add', 'rm']
    if (opts.partialWord === 'l') subs.push('ls')
    else subs.push('ls', 'list')
    return cb(null, subs)
  }
  BB.try(() => {
    const opts = npmConfig()
    return whoami([], true).then(username => {
      const un = encodeURIComponent(username)
      let byUser, theUser
      switch (argv[2]) {
        case 'ls':
          // FIXME: there used to be registry completion here, but it stopped
          // making sense somewhere around 50,000 packages on the registry
          return
        case 'rm':
          if (argv.length > 3) {
            theUser = encodeURIComponent(argv[3])
            byUser = `/-/by-user/${theUser}|${un}`
            return npmFetch.json(byUser, opts).then(d => {
              return d[theUser].filter(
                // kludge for server adminery.
                p => un === 'isaacs' || d[un].indexOf(p) === -1
              )
            })
          }
          // else fallthrough
          /* eslint no-fallthrough:0 */
        case 'add':
          if (argv.length > 3) {
            theUser = encodeURIComponent(argv[3])
            byUser = `/-/by-user/${theUser}|${un}`
            return npmFetch.json(byUser, opts).then(d => {
              var mine = d[un] || []
              var theirs = d[theUser] || []
              return mine.filter(p => theirs.indexOf(p) === -1)
            })
          } else {
            // just list all users who aren't me.
            return npmFetch.json('/-/users', opts).then(list => {
              return Object.keys(list).filter(n => n !== un)
            })
          }

        default:
          return cb()
      }
    })
  }).nodeify(cb)
}

function UsageError () {
  throw Object.assign(new Error(owner.usage), {code: 'EUSAGE'})
}

function owner ([action, ...args], cb) {
  const opts = npmConfig()
  BB.try(() => {
    switch (action) {
      case 'ls': case 'list': return ls(args[0], opts)
      case 'add': return add(args[0], args[1], opts)
      case 'rm': case 'remove': return rm(args[0], args[1], opts)
      default: UsageError()
    }
  }).then(
    data => cb(null, data),
    err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
  )
}

function ls (pkg, opts) {
  if (!pkg) {
    return readLocalPkg().then(pkg => {
      if (!pkg) { UsageError() }
      return ls(pkg, opts)
    })
  }

  const spec = npa(pkg)
  return packument(spec, opts.concat({fullMetadata: true})).then(
    data => {
      var owners = data.maintainers
      if (!owners || !owners.length) {
        output('admin party!')
      } else {
        output(owners.map(o => `${o.name} <${o.email}>`).join('\n'))
      }
      return owners
    },
    err => {
      log.error('owner ls', "Couldn't get owner data", pkg)
      throw err
    }
  )
}

function add (user, pkg, opts) {
  if (!user) { UsageError() }
  if (!pkg) {
    return readLocalPkg().then(pkg => {
      if (!pkg) { UsageError() }
      return add(user, pkg, opts)
    })
  }
  log.verbose('owner add', '%s to %s', user, pkg)

  const spec = npa(pkg)
  return withMutation(spec, user, opts, (u, owners) => {
    if (!owners) owners = []
    for (var i = 0, l = owners.length; i < l; i++) {
      var o = owners[i]
      if (o.name === u.name) {
        log.info(
          'owner add',
          'Already a package owner: ' + o.name + ' <' + o.email + '>'
        )
        return false
      }
    }
    owners.push(u)
    return owners
  })
}

function rm (user, pkg, opts) {
  if (!user) { UsageError() }
  if (!pkg) {
    return readLocalPkg().then(pkg => {
      if (!pkg) { UsageError() }
      return add(user, pkg, opts)
    })
  }
  log.verbose('owner rm', '%s from %s', user, pkg)

  const spec = npa(pkg)
  return withMutation(spec, user, opts, function (u, owners) {
    let found = false
    const m = owners.filter(function (o) {
      var match = (o.name === user)
      found = found || match
      return !match
    })

    if (!found) {
      log.info('owner rm', 'Not a package owner: ' + user)
      return false
    }

    if (!m.length) {
      throw new Error(
        'Cannot remove all owners of a package.  Add someone else first.'
      )
    }

    return m
  })
}

function withMutation (spec, user, opts, mutation) {
  return BB.try(() => {
    if (user) {
      const uri = `/-/user/org.couchdb.user:${encodeURIComponent(user)}`
      return npmFetch.json(uri, opts).then(mutate_, err => {
        log.error('owner mutate', 'Error getting user data for %s', user)
        throw err
      })
    } else {
      return mutate_(null)
    }
  })

  function mutate_ (u) {
    if (user && (!u || u.error)) {
      throw new Error(
        "Couldn't get user data for " + user + ': ' + JSON.stringify(u)
      )
    }

    if (u) u = { name: u.name, email: u.email }
    return packument(spec, opts.concat({
      fullMetadata: true
    })).then(data => {
      // save the number of maintainers before mutation so that we can figure
      // out if maintainers were added or removed
      const beforeMutation = data.maintainers.length

      const m = mutation(u, data.maintainers)
      if (!m) return // handled
      if (m instanceof Error) throw m // error

      data = {
        _id: data._id,
        _rev: data._rev,
        maintainers: m
      }
      const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}`
      return otplease(opts, opts => {
        const reqOpts = opts.concat({
          method: 'PUT',
          body: data,
          spec
        })
        return npmFetch.json(dataPath, reqOpts)
      }).then(data => {
        if (data.error) {
          throw new Error('Failed to update package metadata: ' + JSON.stringify(data))
        } else if (m.length > beforeMutation) {
          output('+ %s (%s)', user, spec.name)
        } else if (m.length < beforeMutation) {
          output('- %s (%s)', user, spec.name)
        }
        return data
      })
    })
  }
}