Skip to content

Commit

Permalink
view: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Jul 27, 2018
1 parent 11c07b3 commit bb23517
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 108 deletions.
229 changes: 122 additions & 107 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ const BB = require('bluebird')
const byteSize = require('byte-size')
const color = require('ansicolors')
const columns = require('cli-columns')
const config = require('./config/figgy-config.js')
const log = require('npmlog')
const fetch = require('npm-registry-fetch')
const npa = require('npm-package-arg')
const npm = require('./npm.js')
const path = require('path')
const readJson = require('read-package-json')
const relativeDate = require('tiny-relative-date')
const semver = require('semver')
const style = require('ansistyles')
var npm = require('./npm.js')
var readJson = require('read-package-json')
var log = require('npmlog')
var util = require('util')
var semver = require('semver')
var mapToRegistry = require('./utils/map-to-registry.js')
var npa = require('npm-package-arg')
var path = require('path')
var usage = require('./utils/usage')
const usage = require('./utils/usage')
const util = require('util')
const validateName = require('validate-npm-package-name')

view.usage = usage(
'view',
Expand All @@ -32,31 +34,25 @@ view.completion = function (opts, cb) {
return cb()
}
// have the package, get the fields.
var tag = npm.config.get('tag')
mapToRegistry(opts.conf.argv.remain[2], npm.config, function (er, uri, auth) {
if (er) return cb(er)

npm.registry.get(uri, { auth: auth }, function (er, d) {
if (er) return cb(er)
var dv = d.versions[d['dist-tags'][tag]]
var fields = []
d.versions = Object.keys(d.versions).sort(semver.compareLoose)
fields = getFields(d).concat(getFields(dv))
cb(null, fields)
})
})
const tag = npm.config.get('tag')
const spec = npa(opts.conf.argv.remain[2])
return fetch.json(spec.escapedName, config({spec})).then(d => {
const dv = d.versions[d['dist-tags'][tag]]
d.versions = Object.keys(d.versions).sort(semver.compareLoose)
return getFields(d).concat(getFields(dv))
}).nodeify(cb)

function getFields (d, f, pref) {
f = f || []
if (!d) return f
pref = pref || []
Object.keys(d).forEach(function (k) {
if (k.charAt(0) === '_' || k.indexOf('.') !== -1) return
var p = pref.concat(k).join('.')
const p = pref.concat(k).join('.')
f.push(p)
if (Array.isArray(d[k])) {
d[k].forEach(function (val, i) {
var pi = p + '[' + i + ']'
const pi = p + '[' + i + ']'
if (val && typeof val === 'object') getFields(val, f, [p])
else f.push(pi)
})
Expand All @@ -76,28 +72,28 @@ function view (args, silent, cb) {

if (!args.length) args = ['.']

var pkg = args.shift()
var nv
const pkg = args.shift()
let nv
if (/^[.]@/.test(pkg)) {
nv = npa.resolve(null, pkg.slice(2))
} else {
nv = npa(pkg)
}
var name = nv.name
var local = (name === '.' || !name)
const name = nv.name
const local = (name === '.' || !name)

if (npm.config.get('global') && local) {
return cb(new Error('Cannot use view command in global mode.'))
}

if (local) {
var dir = npm.prefix
const dir = npm.prefix
readJson(path.resolve(dir, 'package.json'), function (er, d) {
d = d || {}
if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
if (!d.name) return cb(new Error('Invalid package.json'))

var p = d.name
const p = d.name
nv = npa(p)
if (pkg && ~pkg.indexOf('@')) {
nv.rawSpec = pkg.split('@')[pkg.indexOf('@')]
Expand All @@ -112,72 +108,91 @@ function view (args, silent, cb) {

function fetchAndRead (nv, args, silent, cb) {
// get the data about this package
var name = nv.name
var version = nv.rawSpec || npm.config.get('tag')

mapToRegistry(name, npm.config, function (er, uri, auth) {
if (er) return cb(er)

npm.registry.get(uri, { auth: auth }, function (er, data) {
if (er) return cb(er)
if (data['dist-tags'] && data['dist-tags'][version]) {
version = data['dist-tags'][version]
}

if (data.time && data.time.unpublished) {
var u = data.time.unpublished
er = new Error('Unpublished by ' + u.name + ' on ' + u.time)
er.statusCode = 404
er.code = 'E404'
er.pkgid = data._id
return cb(er, data)
const name = nv.name
let version = nv.rawSpec || npm.config.get('tag')

return fetch.json(name, config({
spec: nv,
'prefer-online': true
})).catch(err => {
// TODO - this should probably go into pacote, but the tests expect it.
if (err.code === 'E404') {
err.message = `'${nv.name}' is not in the npm registry.`
const validated = validateName(nv.name)
if (!validated.validForNewPackages) {
err.message += '\n'
err.message += (validated.errors || []).join('\n')
err.message += (validated.warnings || []).join('\n')
} else {
err.message += '\nYou should bug the author to publish it'
err.message += '\n(or use the name yourself!)'
err.message += '\n'
err.message += '\nNote that you can also install from a'
err.message += '\ntarball, folder, http url, or git url.'
}
}
throw err
}).then(data => {
if (data['dist-tags'] && data['dist-tags'][version]) {
version = data['dist-tags'][version]
}

var results = []
var error = null
var versions = data.versions || {}
data.versions = Object.keys(versions).sort(semver.compareLoose)
if (!args.length) args = ['']

// remove readme unless we asked for it
if (args.indexOf('readme') === -1) {
delete data.readme
}
if (data.time && data.time.unpublished) {
const u = data.time.unpublished
let er = new Error('Unpublished by ' + u.name + ' on ' + u.time)
er.statusCode = 404
er.code = 'E404'
er.pkgid = data._id
throw er
}

Object.keys(versions).forEach(function (v) {
if (semver.satisfies(v, version, true)) {
args.forEach(function (args) {
// remove readme unless we asked for it
if (args.indexOf('readme') !== -1) {
delete versions[v].readme
}
results.push(showFields(data, versions[v], args))
})
}
})
var retval = results.reduce(reducer, {})
const results = []
let error = null
const versions = data.versions || {}
data.versions = Object.keys(versions).sort(semver.compareLoose)
if (!args.length) args = ['']

if (args.length === 1 && args[0] === '') {
retval = cleanBlanks(retval)
log.silly('cleanup', retval)
}
// remove readme unless we asked for it
if (args.indexOf('readme') === -1) {
delete data.readme
}

if (error || silent) {
cb(error, retval)
} else if (
!npm.config.get('json') &&
args.length === 1 &&
args[0] === ''
) {
data.version = version
BB.all(results.map((v) => prettyView(data, v[Object.keys(v)[0]][''])))
.nodeify(cb)
.then(() => retval)
} else {
printData(retval, data._id, cb.bind(null, error, retval))
Object.keys(versions).forEach(function (v) {
if (semver.satisfies(v, version, true)) {
args.forEach(function (args) {
// remove readme unless we asked for it
if (args.indexOf('readme') !== -1) {
delete versions[v].readme
}
results.push(showFields(data, versions[v], args))
})
}
})
})
let retval = results.reduce(reducer, {})

if (args.length === 1 && args[0] === '') {
retval = cleanBlanks(retval)
log.silly('cleanup', retval)
}

if (silent) {
} else if (error) {
throw error
} else if (
!npm.config.get('json') &&
args.length === 1 &&
args[0] === ''
) {
data.version = version
return BB.all(
results.map((v) => prettyView(data, v[Object.keys(v)[0]]['']))
).then(() => retval)
} else {
return BB.fromNode(cb => {
printData(retval, data._id, cb)
}).then(() => retval)
}
}).nodeify(cb)
}

function prettyView (packument, manifest) {
Expand Down Expand Up @@ -312,7 +327,7 @@ function prettyView (packument, manifest) {
}

function cleanBlanks (obj) {
var clean = {}
const clean = {}
Object.keys(obj).forEach(function (version) {
clean[version] = obj[version]['']
})
Expand All @@ -334,7 +349,7 @@ function reducer (l, r) {

// return whatever was printed
function showFields (data, version, fields) {
var o = {}
const o = {}
;[data, version].forEach(function (s) {
Object.keys(s).forEach(function (k) {
o[k] = s[k]
Expand All @@ -344,18 +359,18 @@ function showFields (data, version, fields) {
}

function search (data, fields, version, title) {
var field
var tail = fields
let field
const tail = fields
while (!field && fields.length) field = tail.shift()
fields = [field].concat(tail)
var o
let o
if (!field && !tail.length) {
o = {}
o[version] = {}
o[version][title] = data
return o
}
var index = field.match(/(.+)\[([^\]]+)\]$/)
let index = field.match(/(.+)\[([^\]]+)\]$/)
if (index) {
field = index[1]
index = index[2]
Expand All @@ -369,10 +384,10 @@ function search (data, fields, version, title) {
if (data.length === 1) {
return search(data[0], fields, version, title)
}
var results = []
let results = []
data.forEach(function (data, i) {
var tl = title.length
var newt = title.substr(0, tl - fields.join('.').length - 1) +
const tl = title.length
const newt = title.substr(0, tl - fields.join('.').length - 1) +
'[' + i + ']' + [''].concat(fields).join('.')
results.push(search(data, fields.slice(), version, newt))
})
Expand All @@ -396,18 +411,18 @@ function search (data, fields, version, title) {
}

function printData (data, name, cb) {
var versions = Object.keys(data)
var msg = ''
var msgJson = []
var includeVersions = versions.length > 1
var includeFields
const versions = Object.keys(data)
let msg = ''
let msgJson = []
const includeVersions = versions.length > 1
let includeFields

versions.forEach(function (v) {
var fields = Object.keys(data[v])
const fields = Object.keys(data[v])
includeFields = includeFields || (fields.length > 1)
if (npm.config.get('json')) msgJson.push({})
fields.forEach(function (f) {
var d = cleanup(data[v][f])
let d = cleanup(data[v][f])
if (fields.length === 1 && npm.config.get('json')) {
msgJson[msgJson.length - 1][f] = d
}
Expand All @@ -431,7 +446,7 @@ function printData (data, name, cb) {

if (npm.config.get('json')) {
if (msgJson.length && Object.keys(msgJson[0]).length === 1) {
var k = Object.keys(msgJson[0])[0]
const k = Object.keys(msgJson[0])[0]
msgJson = msgJson.map(function (m) { return m[k] })
}

Expand Down Expand Up @@ -465,7 +480,7 @@ function cleanup (data) {
data.versions = Object.keys(data.versions || {})
}

var keys = Object.keys(data)
let keys = Object.keys(data)
keys.forEach(function (d) {
if (d.charAt(0) === '_') delete data[d]
else if (typeof data[d] === 'object') data[d] = cleanup(data[d])
Expand Down
2 changes: 1 addition & 1 deletion test/tap/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test('npm view . with no published package', function (t) {
], { cwd: t3dir }, function (err, code, stdout, stderr) {
t.ifError(err, 'view command finished successfully')
t.equal(code, 1, 'exit not ok')
t.similar(stderr, /version not found/m)
t.similar(stderr, /not in the npm registry/m)
t.end()
})
})
Expand Down

0 comments on commit bb23517

Please sign in to comment.