UNPKG

4.45 kBJavaScriptView Raw
1#!/usr/bin/env node --harmony
2
3'use strict';
4
5// Dependencies
6const program = require('commander');
7const chalk = require('chalk');
8
9// Local dependencies
10const {
11 npmDependencies,
12 npmScripts,
13 npmRegistry,
14 npmRecent,
15 npmGlobal
16} = require('./../build/index');
17const {
18 npmList,
19 npmListDetails
20} = npmDependencies;
21
22
23program
24 .version('7.0.0', '-v, --version')
25 .usage(`[option] [name]`)
26 .description('Fuzzy list anything listable with npm package')
27
28 // Five main features:
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 // Flavor flag
36 .option('-a, --all', 'a flavor flag that shows all available information on any feature flag')
37
38 // Mode
39 .option('-F, --no-fuzzy', 'disable the default fuzzy mode and resort to stdout')
40
41 // Help
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
56if (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) { // Default to npm local packages listing if only --details flag present
66 npmListDetails();
67
68} else if (program.args.length > 0) { // execute if a module is specified, i.e. `npl express --all`
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] === '-') { // Typo of incomplete flag, i.e. 'npl -'
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 // If none of the feature flags were detected but --all ...
84 // --all is a flavor flag. It has no meaning if standing alone
85 console.log(`Please specify a feature`);
86
87} else { // default is list local dependencies when nothing specified...
88 listLocalDependencies();
89}
90
91
92/* Helper Functions */
93function listGlobalPackages() {
94 if (program.time) {
95 return console.log('Use `npl -t` instead');
96 }
97
98 if (!program.details) { // if details flag not specified
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
109function 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
117function listLocalDependencies() {
118 if (!program.details) { // if details flag not specified
119 if (program.fuzzy) {
120 npmList().fuzzy();
121 } else {
122 npmList().default();
123 }
124 } else {
125 npmListDetails();
126 }
127}
128
129function 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
142function listNpmScripts() {
143 if (program.fuzzy) {
144 npmScripts().fuzzy();
145 } else {
146 npmScripts().default();
147 }
148}