unpublish.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* eslint-disable standard/no-callback-literal */
  2. 'use strict'
  3. module.exports = unpublish
  4. const BB = require('bluebird')
  5. const figgyPudding = require('figgy-pudding')
  6. const libaccess = require('libnpm/access')
  7. const libunpub = require('libnpm/unpublish')
  8. const log = require('npmlog')
  9. const npa = require('npm-package-arg')
  10. const npm = require('./npm.js')
  11. const npmConfig = require('./config/figgy-config.js')
  12. const npmFetch = require('npm-registry-fetch')
  13. const otplease = require('./utils/otplease.js')
  14. const output = require('./utils/output.js')
  15. const path = require('path')
  16. const readJson = BB.promisify(require('read-package-json'))
  17. const usage = require('./utils/usage.js')
  18. const whoami = BB.promisify(require('./whoami.js'))
  19. unpublish.usage = usage(
  20. 'unpublish',
  21. '\nnpm unpublish [<@scope>/]<pkg>@<version>' +
  22. '\nnpm unpublish [<@scope>/]<pkg> --force'
  23. )
  24. function UsageError () {
  25. throw Object.assign(new Error(`Usage: ${unpublish.usage}`), {
  26. code: 'EUSAGE'
  27. })
  28. }
  29. const UnpublishConfig = figgyPudding({
  30. force: {},
  31. loglevel: {},
  32. silent: {}
  33. })
  34. unpublish.completion = function (cliOpts, cb) {
  35. if (cliOpts.conf.argv.remain.length >= 3) return cb()
  36. whoami([], true).then(username => {
  37. if (!username) { return [] }
  38. const opts = UnpublishConfig(npmConfig())
  39. return libaccess.lsPackages(username, opts).then(access => {
  40. // do a bit of filtering at this point, so that we don't need
  41. // to fetch versions for more than one thing, but also don't
  42. // accidentally a whole project.
  43. let pkgs = Object.keys(access)
  44. if (!cliOpts.partialWord || !pkgs.length) { return pkgs }
  45. const pp = npa(cliOpts.partialWord).name
  46. pkgs = pkgs.filter(p => !p.indexOf(pp))
  47. if (pkgs.length > 1) return pkgs
  48. return npmFetch.json(npa(pkgs[0]).escapedName, opts).then(doc => {
  49. const vers = Object.keys(doc.versions)
  50. if (!vers.length) {
  51. return pkgs
  52. } else {
  53. return vers.map(v => `${pkgs[0]}@${v}`)
  54. }
  55. })
  56. })
  57. }).nodeify(cb)
  58. }
  59. function unpublish (args, cb) {
  60. if (args.length > 1) return cb(unpublish.usage)
  61. const spec = args.length && npa(args[0])
  62. const opts = UnpublishConfig(npmConfig())
  63. const version = spec.rawSpec
  64. BB.try(() => {
  65. log.silly('unpublish', 'args[0]', args[0])
  66. log.silly('unpublish', 'spec', spec)
  67. if (!version && !opts.force) {
  68. throw Object.assign(new Error(
  69. 'Refusing to delete entire project.\n' +
  70. 'Run with --force to do this.\n' +
  71. unpublish.usage
  72. ), { code: 'EUSAGE' })
  73. }
  74. if (!spec || path.resolve(spec.name) === npm.localPrefix) {
  75. // if there's a package.json in the current folder, then
  76. // read the package name and version out of that.
  77. const cwdJson = path.join(npm.localPrefix, 'package.json')
  78. return readJson(cwdJson).then(data => {
  79. log.verbose('unpublish', data)
  80. return otplease(opts, opts => {
  81. return libunpub(npa.resolve(data.name, data.version), opts.concat(data.publishConfig))
  82. })
  83. }, err => {
  84. if (err && err.code !== 'ENOENT' && err.code !== 'ENOTDIR') {
  85. throw err
  86. } else {
  87. UsageError()
  88. }
  89. })
  90. } else {
  91. return otplease(opts, opts => libunpub(spec, opts))
  92. }
  93. }).then(
  94. ret => {
  95. if (!opts.silent && opts.loglevel !== 'silent') {
  96. output(`- ${spec.name}${
  97. spec.type === 'version' ? `@${spec.rawSpec}` : ''
  98. }`)
  99. }
  100. cb(null, ret)
  101. },
  102. err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
  103. )
  104. }