-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (96 loc) · 3.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
/* eslint-disable standard/no-callback-literal */
/* jshint esversion:6*/
/* jshint node:true */
const cacache = require('cacache');
const path = require('path');
//const child_process = require("child_process"); //I still dont know why this gave error on some user
const npm = require('npm');
// const npm_cache = child_process.execSync("npm config get cache").toString().trim();
// console.log("Cache is at '" + npm_cache + "'");
const usage = '' +
'\n\tnpm-cache-list (show this list)' +
'\n\tnpm-cache-list help (show this list)' +
'\n\tnpm-cache-list all (show all versioned packages - takes long to complete)' +
'\n\tnpm-cache-list list <search terms> (search the cache - terms are combined with "or", not "and")';
exports = module.exports = cache;
function npmconfig() {
return new Promise(resolve =>
npm.load({
some: 'config'
}, function (er, conf) {
if (er) console.log(er);
if (!er) resolve(conf.config.get("cache"));
}));
}
function cache(args) {
const cmd = args.shift();
let result;
switch (cmd) {
case 'all':
console.log("Listing all versioned packages, this will take some time");
result = list([]);
break;
case 'ls':
case 'list':
console.log("Listing packages including search terms:", args);
result = list(args);
break;
case 'help':
default:
console.log('Usage: ' + cache.usage);
npmconfig().then(npm_cache => console.log("\nCache Location is:", npm_cache))
.catch(err => console.log("Error on getting cache location", err.message));
}
}
cache.usage = usage;
cache.list = list;
function list(searchterms) {
npmconfig().then(npm_cache => {
console.log("\nCache Location is:", npm_cache);
const cache = path.join(npm_cache, '_cacache');
let prefix = cache;
if (prefix.indexOf(process.env.HOME) === 0) {
prefix = '~' + prefix.substr(process.env.HOME.length);
}
return cacache.ls(cache).then((files) => {
var allpackages = [];
for (var file in files) {
if (files[file].metadata !== undefined) {
const id = files[file].metadata.id;
if (id !== undefined) {
let name = id.replace(/@/g, " ").trim().split(" ");
let at = id[0] === "@" ? "@" : " ";
allpackages.push({
"package": name[0],
"version": name[1],
"scoped": at
});
}
}
}
allpackages.sort((a, b) => {
if (a.version < b.version) {
return 1;
} else {
return -1;
}
}).sort((a, b) => {
if (a.package.toLowerCase() > b.package.toLowerCase()) {
return 1;
} else {
return -1;
}
});
if (searchterms.length > 0) {
allpackages = allpackages.filter(function (onepackage) {
return onepackage.package.toLowerCase().includes(searchterms[0].toLowerCase());
});
}
for (var index in allpackages) {
var onepackage = allpackages[index];
console.log(onepackage.scoped + onepackage.package + "@" + onepackage.version);
}
});
}).catch(err => console.log("Error happened", err.message));
}