1 | #!/usr/bin/env node --harmony
|
2 |
|
3 | 'use strict';
|
4 |
|
5 |
|
6 | const program = require('commander');
|
7 | const chalk = require('chalk');
|
8 |
|
9 |
|
10 | const {
|
11 | npmDependencies,
|
12 | npmScripts,
|
13 | npmRegistry,
|
14 | npmRecent,
|
15 | npmGlobal
|
16 | } = require('./../build/index');
|
17 | const {
|
18 | npmList,
|
19 | npmListDetails
|
20 | } = npmDependencies;
|
21 |
|
22 |
|
23 | program
|
24 | .version('7.0.0', '-v, --version')
|
25 | .usage(`[option] [name]`)
|
26 | .description('Fuzzy list anything listable with npm package')
|
27 |
|
28 |
|
29 | .option('-l, --local', 'list local dependencies, which is also the default feature')
|
30 | .option('-g, --global', 'list global modules')
|
31 | .option('-d, --details', 'include details to each dependency, but disable the default fuzzy mode')
|
32 | .option('-t, --time', 'show the latest global installs')
|
33 | .option('-s, --scripts', 'list/execute npm scripts')
|
34 |
|
35 |
|
36 | .option('-a, --all', 'a flavor flag that shows all available information on any feature flag')
|
37 |
|
38 |
|
39 | .option('-F, --no-fuzzy', 'disable the default fuzzy mode and resort to stdout')
|
40 |
|
41 |
|
42 | .on('--help', function () {
|
43 | console.log();
|
44 | console.log(' Examples:');
|
45 | console.log(' ' + chalk.blueBright(`npl, ${chalk.white('a fuzzy list of local dependencies')}`));
|
46 | console.log(' ' + chalk.blueBright(`npl -t, ${chalk.white('a fuzzy list of latest global installs')}`));
|
47 | console.log(' ' + chalk.blueBright(`npl -s, ${chalk.white("a fuzzy list of npm scripts using fzf")}`));
|
48 | console.log(' ' + chalk.blueBright(`npl -s --no-fuzzy, ${chalk.white("a normal list of npm scripts")}`));
|
49 | console.log(' ' + chalk.blueBright(`npl -g --details, ${chalk.white('a normal, detailed list of global installs')}`));
|
50 | console.log(' ' + chalk.blueBright(`npl [module], ${chalk.white("a fuzzy list of a module's dependencies fetched from NPM registry")}`));
|
51 | console.log();
|
52 | })
|
53 | .parse(process.argv);
|
54 |
|
55 |
|
56 | if (program.global) {
|
57 | listGlobalPackages();
|
58 |
|
59 | } else if (program.local) {
|
60 | listLocalDependencies();
|
61 |
|
62 | } else if (program.time) {
|
63 | listGlobalPackagesSortedByTime();
|
64 |
|
65 | } else if (program.details) {
|
66 | npmListDetails();
|
67 |
|
68 | } else if (program.args.length > 0) {
|
69 | if (!program.args.length == 1) {
|
70 | console.log(chalk.blueBright('Only one module is allowed'));
|
71 |
|
72 | } else if (program.args.length == 1 && program.args[0] === '-') {
|
73 | console.log(chalk.blueBright('Invalid input'));
|
74 |
|
75 | } else {
|
76 | fetchModuleInfoFromNpmRegistry();
|
77 | }
|
78 |
|
79 | } else if (program.scripts) {
|
80 | listNpmScripts();
|
81 |
|
82 | } else if (program.all) {
|
83 |
|
84 |
|
85 | console.log(`Please specify a feature`);
|
86 |
|
87 | } else {
|
88 | listLocalDependencies();
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 | function listGlobalPackages() {
|
94 | if (program.time) {
|
95 | return console.log('Use `npl -t` instead');
|
96 | }
|
97 |
|
98 | if (!program.details) {
|
99 | if (program.fuzzy) {
|
100 | npmGlobal().then(i => i.simple().fuzzy()).catch(err => console.log(chalk.redBright(err)));
|
101 | } else {
|
102 | npmGlobal().then(i => i.simple().print()).catch(err => console.log(chalk.redBright(err)));
|
103 | }
|
104 | } else {
|
105 | npmGlobal().then(i => i.details()).catch(err => console.log(chalk.redBright(err)));
|
106 | }
|
107 | }
|
108 |
|
109 | function listGlobalPackagesSortedByTime() {
|
110 | if (program.fuzzy) {
|
111 | npmRecent().all().then(i => i.fuzzy()).catch(err => console.log(chalk.redBright(err)));
|
112 | } else {
|
113 | npmRecent().all().then(i => i.default()).catch(err => console.log(chalk.redBright(err)));
|
114 | }
|
115 | }
|
116 |
|
117 | function listLocalDependencies() {
|
118 | if (!program.details) {
|
119 | if (program.fuzzy) {
|
120 | npmList().fuzzy();
|
121 | } else {
|
122 | npmList().default();
|
123 | }
|
124 | } else {
|
125 | npmListDetails();
|
126 | }
|
127 | }
|
128 |
|
129 | function fetchModuleInfoFromNpmRegistry() {
|
130 | const module = program.args;
|
131 | if (!program.all) {
|
132 | if (program.fuzzy) {
|
133 | npmRegistry(module).then(i => i.simple().fuzzy()).catch(err => console.log(chalk.redBright(err)));
|
134 | } else {
|
135 | npmRegistry(module).then(i => i.simple().default()).catch(err => console.log(chalk.redBright(err)));
|
136 | }
|
137 | } else {
|
138 | npmRegistry(module).then(i => i.all()).catch(err => console.log(chalk.redBright(err)));
|
139 | }
|
140 | }
|
141 |
|
142 | function listNpmScripts() {
|
143 | if (program.fuzzy) {
|
144 | npmScripts().fuzzy();
|
145 | } else {
|
146 | npmScripts().default();
|
147 | }
|
148 | }
|