@@ -5,68 +5,72 @@ const fetch = require('npm-registry-fetch')
55const otplease = require ( './utils/otplease.js' )
66const npa = require ( 'npm-package-arg' )
77const semver = require ( 'semver' )
8- const getItentity = require ( './utils/get-identity' )
8+ const getIdentity = require ( './utils/get-identity.js' )
9+ const libaccess = require ( 'libnpmaccess' )
10+ const usageUtil = require ( './utils/usage.js' )
911
10- module . exports = deprecate
12+ const UsageError = ( ) =>
13+ Object . assign ( new Error ( `\nUsage: ${ usage } ` ) , {
14+ code : 'EUSAGE' ,
15+ } )
1116
12- deprecate . usage = 'npm deprecate <pkg>[@<version>] <message>'
17+ const usage = usageUtil (
18+ 'deprecate' ,
19+ 'npm deprecate <pkg>[@<version>] <message>'
20+ )
1321
14- deprecate . completion = function ( opts , cb ) {
15- return Promise . resolve ( ) . then ( ( ) => {
16- if ( opts . conf . argv . remain . length > 2 )
17- return
18- return getItentity ( npm . flatOptions ) . then ( username => {
19- if ( username ) {
20- // first, get a list of remote packages this user owns.
21- // once we have a user account, then don't complete anything.
22- // get the list of packages by user
23- return fetch (
24- `/-/by-user/${ encodeURIComponent ( username ) } ` ,
25- npm . flatOptions
26- ) . then ( list => list [ username ] )
27- }
22+ const completion = ( opts , cb ) => {
23+ if ( opts . conf . argv . remain . length > 1 )
24+ return cb ( null , [ ] )
25+
26+ return getIdentity ( npm . flatOptions ) . then ( ( username ) => {
27+ return libaccess . lsPackages ( username , npm . flatOptions ) . then ( ( packages ) => {
28+ return Object . keys ( packages )
29+ . filter ( ( name ) => packages [ name ] === 'write' &&
30+ ( opts . conf . argv . remain . length === 0 || name . startsWith ( opts . conf . argv . remain [ 0 ] ) )
31+ )
2832 } )
29- } ) . then ( ( ) => cb ( ) , er => cb ( er ) )
33+ } ) . then ( ( list ) => cb ( null , list ) , ( err ) => cb ( err ) )
3034}
3135
32- function deprecate ( [ pkg , msg ] , opts , cb ) {
33- if ( typeof cb !== 'function' ) {
34- cb = opts
35- opts = null
36- }
37- opts = opts || npm . flatOptions
38- return Promise . resolve ( ) . then ( ( ) => {
39- if ( msg == null )
40- throw new Error ( `Usage: ${ deprecate . usage } ` )
41- // fetch the data and make sure it exists.
42- const p = npa ( pkg )
36+ const cmd = ( args , cb ) =>
37+ deprecate ( args )
38+ . then ( ( ) => cb ( ) )
39+ . catch ( err => cb ( err . code === 'EUSAGE' ? err . message : err ) )
40+
41+ const deprecate = async ( [ pkg , msg ] ) => {
42+ if ( ! pkg || ! msg )
43+ throw UsageError ( )
44+
45+ // fetch the data and make sure it exists.
46+ const p = npa ( pkg )
47+ // npa makes the default spec "latest", but for deprecation
48+ // "*" is the appropriate default.
49+ const spec = p . rawSpec === '' ? '*' : p . fetchSpec
4350
44- // npa makes the default spec "latest", but for deprecation
45- // "*" is the appropriate default.
46- const spec = p . rawSpec === '' ? '*' : p . fetchSpec
51+ if ( semver . validRange ( spec , true ) === null )
52+ throw new Error ( `invalid version range: ${ spec } ` )
4753
48- if ( semver . validRange ( spec , true ) === null )
49- throw new Error ( 'invalid version range: ' + spec )
54+ const uri = '/' + p . escapedName
55+ const packument = await fetch . json ( uri , {
56+ ...npm . flatOptions ,
57+ spec : p ,
58+ query : { write : true } ,
59+ } )
5060
51- const uri = '/' + p . escapedName
52- return fetch . json ( uri , {
53- ...opts ,
54- spec : p ,
55- query : { write : true } ,
56- } ) . then ( packument => {
57- // filter all the versions that match
58- Object . keys ( packument . versions )
59- . filter ( v => semver . satisfies ( v , spec ) )
60- . forEach ( v => {
61- packument . versions [ v ] . deprecated = msg
62- } )
63- return otplease ( opts , opts => fetch ( uri , {
64- ...opts ,
65- spec : p ,
66- method : 'PUT' ,
67- body : packument ,
68- ignoreBody : true ,
69- } ) )
61+ Object . keys ( packument . versions )
62+ . filter ( v => semver . satisfies ( v , spec ) )
63+ . forEach ( v => {
64+ packument . versions [ v ] . deprecated = msg
7065 } )
71- } ) . then ( ( ) => cb ( ) , cb )
66+
67+ return otplease ( npm . flatOptions , opts => fetch ( uri , {
68+ ...opts ,
69+ spec : p ,
70+ method : 'PUT' ,
71+ body : packument ,
72+ ignoreBody : true ,
73+ } ) )
7274}
75+
76+ module . exports = Object . assign ( cmd , { completion, usage } )
0 commit comments