-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·73 lines (63 loc) · 2.26 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const kleur = require('kleur');
const {log} = console;
function program(...args) {
const isDevIndex = args.indexOf('--dev');
const isDev = isDevIndex !== -1;
const pathToPkg = isDev ? args[isDevIndex === 0 ? 1 : 0] : args[0];
const resolvedPath = path.resolve(pathToPkg);
if (!fs.existsSync(resolvedPath)) return log('File does not exist');
if (!resolvedPath.endsWith('.json'))
return log('Provided file is not a json file');
const pkg = require(resolvedPath);
const {dependencies, devDependencies} = pkg;
const depsToProcess = isDev ? devDependencies : dependencies;
if (depsToProcess === undefined) {
return log(`JSON does not have ${isDev ? 'devD' : 'd'}ependencies`);
}
Promise.all(
Object.entries(depsToProcess).map(([name, version]) =>
fetch(
[
'https://packagephobia.now.sh/v2/api.json?p=',
name,
'@',
version.replace(/[\^=<>~]/g, ''),
].join(''),
).then(res => res.json())
.catch(() => ({name, install: {bytes: -1}, publish: {bytes: -1}})),
),
)
.then(sizes => {
const title = `${pkg.name}'s ${isDev ? 'devD ' : 'd'}ependencies:\n`;
log(kleur.bold(title));
return sizes
.sort((a, b) => a.install.bytes - b.install.bytes)
.map(({name, version, install, publish}) => {
if (install.bytes === 0) {
return `${name} - unknown`;
}
if (install.bytes === -1) {
return `${name} - api failed`;
}
return `
${kleur.bold(name)} @ ${version}
\tinstall size\t${colorSize(install)}
\tpublish size\t${colorSize(publish)}
`.trim();
})
.join('\n\n');
})
.then(log);
}
function colorSize({bytes, pretty}) {
return bytes > 1048576
? kleur.bold(kleur.red(pretty))
: bytes > 307200
? kleur.yellow(pretty)
: kleur.green(pretty);
}
program(...process.argv.slice(2));