Skip to content

Commit

Permalink
outdated: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 10, 2018
1 parent dec07eb commit 0847419
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,39 @@ var color = require('ansicolors')
var styles = require('ansistyles')
var table = require('text-table')
var semver = require('semver')
var npa = require('npm-package-arg')
var npa = require('libnpm/parse-arg')
var pickManifest = require('npm-pick-manifest')
var fetchPackageMetadata = require('./fetch-package-metadata.js')
var mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
var npm = require('./npm.js')
const npmConfig = require('./config/figgy-config.js')
const figgyPudding = require('figgy-pudding')
const packument = require('libnpm/packument')
var long = npm.config.get('long')
var mapToRegistry = require('./utils/map-to-registry.js')
var isExtraneous = require('./install/is-extraneous.js')
var computeMetadata = require('./install/deps.js').computeMetadata
var computeVersionSpec = require('./install/deps.js').computeVersionSpec
var moduleName = require('./utils/module-name.js')
var output = require('./utils/output.js')
var ansiTrim = require('./utils/ansi-trim')

const OutdatedConfig = figgyPudding({
also: {},
color: {},
depth: {},
dev: 'development',
development: {},
global: {},
json: {},
only: {},
parseable: {},
prod: 'production',
production: {},
save: {},
'save-dev': {},
'save-optional': {}
})

function uniq (list) {
// we maintain the array because we need an array, not iterator, return
// value.
Expand All @@ -68,26 +87,27 @@ function outdated (args, silent, cb) {
cb = silent
silent = false
}
let opts = OutdatedConfig(npmConfig())
var dir = path.resolve(npm.dir, '..')

// default depth for `outdated` is 0 (cf. `ls`)
if (npm.config.get('depth') === Infinity) npm.config.set('depth', 0)
if (opts.depth) opts = opts.concat({depth: 0})

readPackageTree(dir, andComputeMetadata(function (er, tree) {
if (!tree) return cb(er)
mutateIntoLogicalTree(tree)
outdated_(args, '', tree, {}, 0, function (er, list) {
outdated_(args, '', tree, {}, 0, opts, function (er, list) {
list = uniq(list || []).sort(function (aa, bb) {
return aa[0].path.localeCompare(bb[0].path) ||
aa[1].localeCompare(bb[1])
})
if (er || silent || list.length === 0) return cb(er, list)
if (npm.config.get('json')) {
output(makeJSON(list))
} else if (npm.config.get('parseable')) {
output(makeParseable(list))
if (opts.json) {
output(makeJSON(list, opts))
} else if (opts.parseable) {
output(makeParseable(list, opts))
} else {
var outList = list.map(makePretty)
var outList = list.map(x => makePretty(x, opts))
var outHead = [ 'Package',
'Current',
'Wanted',
Expand All @@ -97,7 +117,7 @@ function outdated (args, silent, cb) {
if (long) outHead.push('Package Type', 'Homepage')
var outTable = [outHead].concat(outList)

if (npm.color) {
if (opts.color) {
outTable[0] = outTable[0].map(function (heading) {
return styles.underline(heading)
})
Expand All @@ -116,7 +136,7 @@ function outdated (args, silent, cb) {
}

// [[ dir, dep, has, want, latest, type ]]
function makePretty (p) {
function makePretty (p, opts) {
var depname = p[1]
var has = p[2]
var want = p[3]
Expand All @@ -136,7 +156,7 @@ function makePretty (p) {
columns[6] = homepage
}

if (npm.color) {
if (opts.color) {
columns[0] = color[has === want || want === 'linked' ? 'yellow' : 'red'](columns[0]) // dep
columns[2] = color.green(columns[2]) // want
columns[3] = color.magenta(columns[3]) // latest
Expand Down Expand Up @@ -167,7 +187,7 @@ function makeParseable (list) {
}).join(os.EOL)
}

function makeJSON (list) {
function makeJSON (list, opts) {
var out = {}
list.forEach(function (p) {
var dep = p[0]
Expand All @@ -177,7 +197,7 @@ function makeJSON (list) {
var want = p[3]
var latest = p[4]
var type = p[6]
if (!npm.config.get('global')) {
if (!opts.global) {
dir = path.relative(process.cwd(), dir)
}
out[depname] = { current: has,
Expand All @@ -193,11 +213,11 @@ function makeJSON (list) {
return JSON.stringify(out, null, 2)
}

function outdated_ (args, path, tree, parentHas, depth, cb) {
function outdated_ (args, path, tree, parentHas, depth, opts, cb) {
if (!tree.package) tree.package = {}
if (path && tree.package.name) path += ' > ' + tree.package.name
if (!path && tree.package.name) path = tree.package.name
if (depth > npm.config.get('depth')) {
if (depth > opts.depth) {
return cb(null, [])
}
var types = {}
Expand Down Expand Up @@ -227,11 +247,14 @@ function outdated_ (args, path, tree, parentHas, depth, cb) {
// (All the save checking here is because this gets called from npm-update currently
// and that requires this logic around dev deps.)
// FIXME: Refactor npm update to not be in terms of outdated.
var dev = npm.config.get('dev') || /^dev(elopment)?$/.test(npm.config.get('also'))
var prod = npm.config.get('production') || /^prod(uction)?$/.test(npm.config.get('only'))
if ((dev || !prod) &&
(npm.config.get('save-dev') || (
!npm.config.get('save') && !npm.config.get('save-optional')))) {
var dev = opts.dev || /^dev(elopment)?$/.test(opts.also)
var prod = opts.production || /^prod(uction)?$/.test(opts.only)
if (
(dev || !prod) &&
(
opts['save-dev'] || (!opts.save && !opts['save-optional'])
)
) {
Object.keys(tree.missingDevDeps).forEach(function (name) {
deps.push({
package: { name: name },
Expand All @@ -245,15 +268,15 @@ function outdated_ (args, path, tree, parentHas, depth, cb) {
})
}

if (npm.config.get('save-dev')) {
if (opts['save-dev']) {
deps = deps.filter(function (dep) { return pkg.devDependencies[moduleName(dep)] })
deps.forEach(function (dep) {
types[moduleName(dep)] = 'devDependencies'
})
} else if (npm.config.get('save')) {
} else if (opts.save) {
// remove optional dependencies from dependencies during --save.
deps = deps.filter(function (dep) { return !pkg.optionalDependencies[moduleName(dep)] })
} else if (npm.config.get('save-optional')) {
} else if (opts['save-optional']) {
deps = deps.filter(function (dep) { return pkg.optionalDependencies[moduleName(dep)] })
deps.forEach(function (dep) {
types[moduleName(dep)] = 'optionalDependencies'
Expand All @@ -262,7 +285,7 @@ function outdated_ (args, path, tree, parentHas, depth, cb) {
var doUpdate = dev || (
!prod &&
!Object.keys(parentHas).length &&
!npm.config.get('global')
!opts.global
)
if (doUpdate) {
Object.keys(pkg.devDependencies || {}).forEach(function (k) {
Expand Down Expand Up @@ -300,13 +323,13 @@ function outdated_ (args, path, tree, parentHas, depth, cb) {
required = computeVersionSpec(tree, dep)
}

if (!long) return shouldUpdate(args, dep, name, has, required, depth, path, cb)
if (!long) return shouldUpdate(args, dep, name, has, required, depth, path, opts, cb)

shouldUpdate(args, dep, name, has, required, depth, path, cb, types[name])
shouldUpdate(args, dep, name, has, required, depth, path, opts, cb, types[name])
}, cb)
}

function shouldUpdate (args, tree, dep, has, req, depth, pkgpath, cb, type) {
function shouldUpdate (args, tree, dep, has, req, depth, pkgpath, opts, cb, type) {
// look up the most recent version.
// if that's what we already have, or if it's not on the args list,
// then dive into it. Otherwise, cb() with the data.
Expand All @@ -322,6 +345,7 @@ function shouldUpdate (args, tree, dep, has, req, depth, pkgpath, cb, type) {
tree,
has,
depth + 1,
opts,
cb)
}

Expand Down Expand Up @@ -350,11 +374,9 @@ function shouldUpdate (args, tree, dep, has, req, depth, pkgpath, cb, type) {
} else if (parsed.type === 'file') {
return updateLocalDeps()
} else {
return mapToRegistry(dep, npm.config, function (er, uri, auth) {
if (er) return cb(er)

npm.registry.get(uri, { auth: auth }, updateDeps)
})
return packument(dep, opts.concat({
'prefer-online': true
})).nodeify(updateDeps)
}

function updateLocalDeps (latestRegistryVersion) {
Expand Down

0 comments on commit 0847419

Please sign in to comment.