This repository was archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
feat(commands): add 'deprecate' command #23
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7dec0ee
feat(deps): semver@^5.6.0
4065096
feat(commands): add 'deprecate' command
326abfb
Merge branch 'latest' into add-deprecate
34db45a
Merge branch 'latest' into add-deprecate
0858f10
Merge branch 'latest' into add-deprecate
4596a7d
deps: add read
4abed68
feat(utils): add otplease.js
cdc0474
fix(deprecate): re-add otplease
ffad682
style(deprecate): linting
9ba8f6f
style(otplease): linting
43039e5
Merge branch 'latest' into add-deprecate
40237a7
test(deprecate): add unit tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| 'use strict' | ||
|
|
||
| const Deprecate = module.exports = { | ||
| command: 'deprecate <pkg>[@<version>] <message>', | ||
| describe: 'Deprecate a version of a package', | ||
| builder (y) { | ||
| return y.help().alias('help', 'h') | ||
| .options(Deprecate.options) | ||
| }, | ||
| options: Object.assign(require('../common-opts.js', {})), | ||
| handler: async argv => deprecate(argv) | ||
| } | ||
|
|
||
| async function deprecate (argv) { | ||
| const figgyPudding = require('figgy-pudding') | ||
| const BB = require('bluebird') | ||
| const libnpm = require('libnpm') | ||
| const npmConfig = require('../config.js') | ||
| const semver = require('semver') | ||
| const otplease = require('../utils/otplease.js') | ||
|
|
||
| const DeprecateConfig = figgyPudding({ | ||
| json: {}, | ||
| loglevel: {}, | ||
| parseable: {}, | ||
| silent: {} | ||
| }) | ||
|
|
||
| const opts = DeprecateConfig(npmConfig().concat(argv).concat({ | ||
| log: require('npmlog') | ||
| })) | ||
|
|
||
| return BB.try(() => { | ||
| // fetch the data and make sure it exists. | ||
| const p = libnpm.parseArg(argv['pkg@version']) | ||
|
|
||
| // npa makes the default spec "latest", but for deprecation | ||
| // "*" is the appropriate default. | ||
| const spec = p.rawSpec === '' ? '*' : p.fetchSpec | ||
|
|
||
| if (semver.validRange(spec, true) === null) { | ||
| throw new Error('invalid version range: ' + spec) | ||
| } | ||
|
|
||
| const uri = '/' + p.escapedName | ||
| return libnpm.fetch.json(uri, opts.concat({ | ||
| spec: p, | ||
| query: { write: true } | ||
| })).then(packument => { | ||
| // filter all the versions that match | ||
| Object.keys(packument.versions) | ||
| .filter(v => semver.satisfies(v, spec)) | ||
| .forEach(v => { packument.versions[v].deprecated = argv.message }) | ||
| return otplease(opts, opts => libnpm.fetch(uri, opts.concat({ | ||
| spec: p, | ||
| method: 'PUT', | ||
| body: packument, | ||
| ignoreBody: true | ||
| }))) | ||
| }) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use strict' | ||
|
|
||
| const BB = require('bluebird') | ||
|
|
||
| const optCheck = require('figgy-pudding')({ | ||
| prompt: { default: 'This operation requires a one-time password.\nEnter OTP:' }, | ||
| otp: {} | ||
| }) | ||
|
|
||
| module.exports = otplease | ||
| function otplease (opts, fn) { | ||
| opts = opts.concat ? opts : optCheck(opts) | ||
| return BB.try(() => { | ||
| return fn(opts) | ||
| }).catch(err => { | ||
| if (err.code !== 'EOTP' && !(err.code === 'E401' && /one-time pass/.test(err.body))) { | ||
| throw err | ||
| } else if (!process.stdin.isTTY || !process.stdout.isTTY) { | ||
| throw err | ||
| } else { | ||
| return readOTP( | ||
| optCheck(opts).prompt | ||
| ).then(otp => fn(opts.concat({ otp }))) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function readOTP (msg, otp, isRetry) { | ||
| const read = require('read') | ||
| if (!msg) { | ||
| msg = [ | ||
| 'This command requires a one-time password (OTP) from your authenticator app.', | ||
| 'Enter one below. You can also pass one on the command line by appending --otp=123456.', | ||
| 'For more information, see:', | ||
| 'https://docs.npmjs.com/getting-started/using-two-factor-authentication', | ||
| 'Enter OTP: ' | ||
| ].join('\n') | ||
| } | ||
| if (isRetry && otp && /^[\d ]+$|^[A-Fa-f0-9]{64,64}$/.test(otp)) return otp.replace(/\s+/g, '') | ||
|
|
||
| return read({ prompt: msg, default: otp || '' }) | ||
| .then((otp) => readOTP(msg, otp, true)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so strange. Is this seriously handled just like that by yargs?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I first thought I would have to manually join the package name and the version, but then I ran the debugger and spotted that:

Fortunately it seems to work the way we want, but even the yargs docs don't seem to document what happens when two positional arguments are character-separated instead of whitespace-separated. I can't find a more comprehensive document for how it parses command templates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mind=blown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can tell from the code it seems to just consider those cases as one positional argument, which makes sense.