-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
86 lines (73 loc) · 2.65 KB
/
cli.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
#!/usr/bin/env node
import chalk from 'chalk';
import Table from 'cli-table3';
import { program } from 'commander';
import config from './lib/config.js';
import * as dates from './lib/dates.js';
import * as dependencies from './lib/dependencies.js';
import * as versions from './lib/versions.js';
const SHORT = {
devDependencies: chalk.blue.bold('dev'),
peerDependencies: chalk.magenta.bold('peer'),
bundledDependencies: chalk.cyan.bold('bundled'),
};
const table = new Table({
head: [
chalk.rgb(255, 165, 0).underline('Name'),
chalk.rgb(255, 165, 0).underline('Type'),
chalk.rgb(255, 165, 0).underline('Version'),
chalk.rgb(255, 165, 0).underline('Last Publish')
]
});
program
.name('package-age')
.version(config.version, '-v, --version')
.description('A CLI for detecting old dependencies used in your project')
.option('-f, --file [optional]', 'path to the package.json', 'package.json')
.option('-y, --year [optional]', 'after how much years a package should be considered old', 2)
.option('-m, --month [optional]', 'after how much months a package should be considered old', 0)
.option('-a, --all', 'parameter to get all kinds of dependencies', false)
.option('-d, --dev', 'parameter to get the devDependencies', false)
.option('-p, --peer', 'parameter to get the peerDependencies', false)
.option('-b, --bundled', 'parameter to get the bundledDependencies', false)
// The following two options, if present, are automatically detected by chalk
// For more information see: https://github.com/chalk/supports-color/blob/v7.1.0/index.js#L8-L19
.option('--color', 'force colored output')
.option('--no-color', 'force non-colored output')
.parse(process.argv);
async function cli() {
const options = program.opts();
const results = await dependencies.get(Object.assign(config, {
file: options.file,
year: options.year,
month: options.month,
dependencies: {
all: options.all,
dev: options.dev,
peer: options.peer,
bundled: options.bundled
}
}));
// Print the results to the console
Object.entries(results).forEach(([key, dependencies]) => {
dependencies.forEach(dependency => {
let version;
let date;
if (dependency.valid) {
version = versions.compare(dependency.version, dependency.latest);
date = dates.compare(dependency.date, config.year, config.month);
} else {
version = chalk.bgRed.bold(`supplied invalid version: '${version}'`);
date = null;
}
table.push([
dependency.name,
SHORT[key] || null,
version,
date
]);
});
});
process.stdout.write(table.toString());
}
cli();