summaryrefslogtreecommitdiff
path: root/deps/npm/lib/publish.js
blob: e81fc1a057454694d0da4d8250dcf4782893e253 (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
'use strict'

const BB = require('bluebird')

const cacache = require('cacache')
const figgyPudding = require('figgy-pudding')
const libpub = require('libnpm/publish')
const libunpub = require('libnpm/unpublish')
const lifecycle = BB.promisify(require('./utils/lifecycle.js'))
const log = require('npmlog')
const npa = require('libnpm/parse-arg')
const npmConfig = require('./config/figgy-config.js')
const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const pack = require('./pack')
const { tarball, extract } = require('libnpm')
const path = require('path')
const readFileAsync = BB.promisify(require('graceful-fs').readFile)
const readJson = BB.promisify(require('read-package-json'))
const semver = require('semver')
const statAsync = BB.promisify(require('graceful-fs').stat)

publish.usage = 'npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>] [--dry-run]' +
                "\n\nPublishes '.' if no argument supplied" +
                '\n\nSets tag `latest` if no --tag specified'

publish.completion = function (opts, cb) {
  // publish can complete to a folder with a package.json
  // or a tarball, or a tarball url.
  // for now, not yet implemented.
  return cb()
}

const PublishConfig = figgyPudding({
  dryRun: 'dry-run',
  'dry-run': { default: false },
  force: { default: false },
  json: { default: false },
  Promise: { default: () => Promise },
  tag: { default: 'latest' },
  tmp: {}
})

module.exports = publish
function publish (args, isRetry, cb) {
  if (typeof cb !== 'function') {
    cb = isRetry
    isRetry = false
  }
  if (args.length === 0) args = ['.']
  if (args.length !== 1) return cb(publish.usage)

  log.verbose('publish', args)

  const opts = PublishConfig(npmConfig())
  const t = opts.tag.trim()
  if (semver.validRange(t)) {
    return cb(new Error('Tag name must not be a valid SemVer range: ' + t))
  }

  return publish_(args[0], opts)
    .then((tarball) => {
      const silent = log.level === 'silent'
      if (!silent && opts.json) {
        output(JSON.stringify(tarball, null, 2))
      } else if (!silent) {
        output(`+ ${tarball.id}`)
      }
    })
    .nodeify(cb)
}

function publish_ (arg, opts) {
  return statAsync(arg).then((stat) => {
    if (stat.isDirectory()) {
      return stat
    } else {
      const err = new Error('not a directory')
      err.code = 'ENOTDIR'
      throw err
    }
  }).then(() => {
    return publishFromDirectory(arg, opts)
  }, (err) => {
    if (err.code !== 'ENOENT' && err.code !== 'ENOTDIR') {
      throw err
    } else {
      return publishFromPackage(arg, opts)
    }
  })
}

function publishFromDirectory (arg, opts) {
  // All this readJson is because any of the given scripts might modify the
  // package.json in question, so we need to refresh after every step.
  let contents
  return pack.prepareDirectory(arg).then(() => {
    return readJson(path.join(arg, 'package.json'))
  }).then((pkg) => {
    return lifecycle(pkg, 'prepublishOnly', arg)
  }).then(() => {
    return readJson(path.join(arg, 'package.json'))
  }).then((pkg) => {
    return cacache.tmp.withTmp(opts.tmp, {tmpPrefix: 'fromDir'}, (tmpDir) => {
      const target = path.join(tmpDir, 'package.tgz')
      return pack.packDirectory(pkg, arg, target, null, true)
        .tap((c) => { contents = c })
        .then((c) => !opts.json && pack.logContents(c))
        .then(() => upload(pkg, false, target, opts))
    })
  }).then(() => {
    return readJson(path.join(arg, 'package.json'))
  }).tap((pkg) => {
    return lifecycle(pkg, 'publish', arg)
  }).tap((pkg) => {
    return lifecycle(pkg, 'postpublish', arg)
  })
    .then(() => contents)
}

function publishFromPackage (arg, opts) {
  return cacache.tmp.withTmp(opts.tmp, {tmpPrefix: 'fromPackage'}, tmp => {
    const extracted = path.join(tmp, 'package')
    const target = path.join(tmp, 'package.json')
    return tarball.toFile(arg, target, opts)
      .then(() => extract(arg, extracted, opts))
      .then(() => readJson(path.join(extracted, 'package.json')))
      .then((pkg) => {
        return BB.resolve(pack.getContents(pkg, target))
          .tap((c) => !opts.json && pack.logContents(c))
          .tap(() => upload(pkg, false, target, opts))
      })
  })
}

function upload (pkg, isRetry, cached, opts) {
  if (!opts.dryRun) {
    return readFileAsync(cached).then(tarball => {
      return otplease(opts, opts => {
        return libpub(pkg, tarball, opts)
      }).catch(err => {
        if (
          err.code === 'EPUBLISHCONFLICT' &&
          opts.force &&
          !isRetry
        ) {
          log.warn('publish', 'Forced publish over ' + pkg._id)
          return otplease(opts, opts => libunpub(
            npa.resolve(pkg.name, pkg.version), opts
          )).finally(() => {
            // ignore errors.  Use the force.  Reach out with your feelings.
            return otplease(opts, opts => {
              return upload(pkg, true, tarball, opts)
            }).catch(() => {
              // but if it fails again, then report the first error.
              throw err
            })
          })
        } else {
          throw err
        }
      })
    })
  } else {
    return opts.Promise.resolve(true)
  }
}