Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support listing out npm cache #194

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ cache.usage = 'npm cache add <tarball file>' +
'\nnpm cache add <git url>' +
'\nnpm cache add <name>@<version>' +
'\nnpm cache clean' +
'\nnpm cache list [--json]' +
'\nnpm cache verify'

cache.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
if (argv.length === 2) {
return cb(null, ['add', 'clean'])
return cb(null, ['add', 'clean', 'list', 'verify'])
}

// TODO - eventually...
switch (argv[2]) {
case 'clean':
case 'add':
case 'list':
case 'verify':
return cb(null, [])
}
}
Expand All @@ -52,6 +55,9 @@ function cache (args, cb) {
case 'verify': case 'check':
result = verify()
break
case 'list': case 'ls':
result = list()
break
default: return cb('Usage: ' + cache.usage)
}
if (!result || !result.then) {
Expand Down Expand Up @@ -135,3 +141,49 @@ function unpack (pkg, ver, unpackTarget, dmode, fmode, uid, gid) {
return pacote.extract(npa.resolve(pkg, ver), unpackTarget, opts)
})
}

cache.list = list
function list () {
const json = npm.config.get('json')
const cache = path.join(npm.config.get('cache'), '_cacache')
let writer
if (json) {
writer = {
item: undefined,
handleElement: function (item) {
if (this.item === undefined) {
output('[')
} else {
output(' ' + JSON.stringify(this.item) + ',')
}
this.item = item
},
done: function () {
if (this.item === undefined) {
output(JSON.stringify([]))
} else {
output(' ' + JSON.stringify(this.item))
output(']')
}
}
}
} else {
writer = {
handleElement: function (item) {
output(item.key)
output(' url = ' + (item.metadata === undefined ? 'undefined' : item.metadata.url))
output(' path = ' + item.path)
output(' size = ' + item.size)
output(' time = ' + item.time)
},
done: () => {}
}
}

return new BB((resolve, reject) => {
const stream = cacache.ls.stream(cache)
stream.on('data', (item) => writer.handleElement(item))
stream.on('error', error => { reject(error) })
stream.on('end', () => { writer.done(); resolve('done') })
})
}