Skip to content

Commit

Permalink
--amend
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Feb 25, 2021
1 parent 1a60c2d commit b879719
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 573 deletions.
372 changes: 191 additions & 181 deletions lib/config.js

Large diffs are not rendered by default.

45 changes: 27 additions & 18 deletions lib/dedupe.js
@@ -1,29 +1,38 @@
// dedupe duplicated packages, or find them in the tree
const npm = require('./npm.js')
const Arborist = require('@npmcli/arborist')
const usageUtil = require('./utils/usage.js')
const reifyFinish = require('./utils/reify-finish.js')

const usage = usageUtil('dedupe', 'npm dedupe')
class Dedupe {
constructor (npm) {
this.npm = npm
}

const cmd = (args, cb) => dedupe(args).then(() => cb()).catch(cb)
get usage () {
return usageUtil('dedupe', 'npm dedupe')
}

const dedupe = async (args) => {
if (npm.flatOptions.global) {
const er = new Error('`npm dedupe` does not work in global mode.')
er.code = 'EDEDUPEGLOBAL'
throw er
exec (args, cb) {
this.dedupe(args).then(() => cb()).catch(cb)
}

const dryRun = (args && args.dryRun) || npm.flatOptions.dryRun
const where = npm.prefix
const arb = new Arborist({
...npm.flatOptions,
path: where,
dryRun,
})
await arb.dedupe(npm.flatOptions)
await reifyFinish(arb)
async dedupe (args) {
if (this.npm.flatOptions.global) {
const er = new Error('`npm dedupe` does not work in global mode.')
er.code = 'EDEDUPEGLOBAL'
throw er
}

const dryRun = (args && args.dryRun) || this.npm.flatOptions.dryRun
const where = this.npm.prefix
const arb = new Arborist({
...this.npm.flatOptions,
path: where,
dryRun,
})
await arb.dedupe(this.npm.flatOptions)
await reifyFinish(arb)
}
}

module.exports = Object.assign(cmd, { usage })
module.exports = Dedupe
115 changes: 62 additions & 53 deletions lib/deprecate.js
@@ -1,4 +1,3 @@
const npm = require('./npm.js')
const fetch = require('npm-registry-fetch')
const otplease = require('./utils/otplease.js')
const npa = require('npm-package-arg')
Expand All @@ -7,67 +6,77 @@ const getIdentity = require('./utils/get-identity.js')
const libaccess = require('libnpmaccess')
const usageUtil = require('./utils/usage.js')

const UsageError = () =>
Object.assign(new Error(`\nUsage: ${usage}`), {
code: 'EUSAGE',
})
class Deprecate {
constructor (npm) {
this.npm = npm
}

const usage = usageUtil(
'deprecate',
'npm deprecate <pkg>[@<version>] <message>'
)
get usage () {
return usageUtil(
'deprecate',
'npm deprecate <pkg>[@<version>] <message>'
)
}

const completion = async (opts) => {
if (opts.conf.argv.remain.length > 1)
return []
async completion (opts) {
if (opts.conf.argv.remain.length > 1)
return []

const username = await getIdentity(npm.flatOptions)
const packages = await libaccess.lsPackages(username, npm.flatOptions)
return Object.keys(packages)
.filter((name) =>
packages[name] === 'write' &&
(opts.conf.argv.remain.length === 0 ||
name.startsWith(opts.conf.argv.remain[0])))
}

const cmd = (args, cb) =>
deprecate(args)
.then(() => cb())
.catch(err => cb(err.code === 'EUSAGE' ? err.message : err))
const username = await getIdentity(this.npm.flatOptions)
const packages = await libaccess.lsPackages(username, this.npm.flatOptions)
return Object.keys(packages)
.filter((name) =>
packages[name] === 'write' &&
(opts.conf.argv.remain.length === 0 ||
name.startsWith(opts.conf.argv.remain[0])))
}

const deprecate = async ([pkg, msg]) => {
if (!pkg || !msg)
throw UsageError()
exec (args, cb) {
this.deprecate(args)
.then(() => cb())
.catch(err => cb(err.code === 'EUSAGE' ? err.message : err))
}

// fetch the data and make sure it exists.
const p = npa(pkg)
// npa makes the default spec "latest", but for deprecation
// "*" is the appropriate default.
const spec = p.rawSpec === '' ? '*' : p.fetchSpec
async deprecate ([pkg, msg]) {
if (!pkg || !msg)
throw this.usageError()

if (semver.validRange(spec, true) === null)
throw new Error(`invalid version range: ${spec}`)
// fetch the data and make sure it exists.
const p = npa(pkg)
// npa makes the default spec "latest", but for deprecation
// "*" is the appropriate default.
const spec = p.rawSpec === '' ? '*' : p.fetchSpec

const uri = '/' + p.escapedName
const packument = await fetch.json(uri, {
...npm.flatOptions,
spec: p,
query: { write: true },
})
if (semver.validRange(spec, true) === null)
throw new Error(`invalid version range: ${spec}`)

Object.keys(packument.versions)
.filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
.forEach(v => {
packument.versions[v].deprecated = msg
const uri = '/' + p.escapedName
const packument = await fetch.json(uri, {
...this.npm.flatOptions,
spec: p,
query: { write: true },
})

return otplease(npm.flatOptions, opts => fetch(uri, {
...opts,
spec: p,
method: 'PUT',
body: packument,
ignoreBody: true,
}))
Object.keys(packument.versions)
.filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
.forEach(v => {
packument.versions[v].deprecated = msg
})

return otplease(this.npm.flatOptions, opts => fetch(uri, {
...opts,
spec: p,
method: 'PUT',
body: packument,
ignoreBody: true,
}))
}

usageError () {
return Object.assign(new Error(`\nUsage: ${this.usage}`), {
code: 'EUSAGE',
})
}
}

module.exports = Object.assign(cmd, { completion, usage })
module.exports = Deprecate

0 comments on commit b879719

Please sign in to comment.