1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 |
|
12 |
|
13 | function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
14 |
|
15 | const execChildProcess = require('child_process').exec;
|
16 | const spawn = require('child_process').spawn;
|
17 | const chalk = require('chalk');
|
18 | const pkgInfo = require('pkginfo');
|
19 | const cwd = process.cwd();
|
20 | const iPipeTo = require('ipt');
|
21 | const StringUtil = require('./utils/stringUtil');
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | module.exports.npmList = function () {
|
28 | let pkg = collectDependencies();
|
29 | let list = parseListFromPkgOutput(pkg);
|
30 |
|
31 |
|
32 | return {
|
33 | default() {
|
34 | if (!list || list.length === 0) return;
|
35 |
|
36 | return list.forEach(i => console.log('├── ' + i));
|
37 | },
|
38 | fuzzy() {
|
39 | if (!list || list.length === 0) return;
|
40 |
|
41 | return iPipeTo(list, {
|
42 | size: 20,
|
43 | autocomplete: true,
|
44 | message: ' '
|
45 | }).then(keys => {
|
46 | return keys.forEach(function (key) {
|
47 | key = StringUtil.getRidOfColors(key);
|
48 |
|
49 | spawn(`npm info ${key} | less -r`, {
|
50 | stdio: 'inherit',
|
51 | shell: true
|
52 | });
|
53 | });
|
54 | }).catch(err => {
|
55 | console.log(err, "Error building interactive interface");
|
56 | });
|
57 | },
|
58 |
|
59 |
|
60 | raw: (() => {
|
61 | var _ref = _asyncToGenerator(function* () {
|
62 | if (!list || list.length === 0) return;
|
63 |
|
64 | return list;
|
65 | });
|
66 |
|
67 | return function raw() {
|
68 | return _ref.apply(this, arguments);
|
69 | };
|
70 | })(),
|
71 |
|
72 | rawNoColor: (() => {
|
73 | var _ref2 = _asyncToGenerator(function* () {
|
74 | if (!list || list.length === 0) return;
|
75 |
|
76 | return list.map(function (key) {
|
77 | let result = StringUtil.getRidOfColors(key);
|
78 | result = StringUtil.getRidOfQuotationMarks(result);
|
79 | return result;
|
80 | });
|
81 | });
|
82 |
|
83 | return function rawNoColor() {
|
84 | return _ref2.apply(this, arguments);
|
85 | };
|
86 | })()
|
87 | };
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | module.exports.npmListDetails = function () {
|
94 | const cmd = 'npm ll --depth=0 --long=true ';
|
95 |
|
96 |
|
97 | execChildProcess(cmd + '--local', function (error, stdout, stderr) {
|
98 | if (error) {
|
99 |
|
100 | console.log(chalk.red.bold.underline("exec error:") + error);
|
101 | }
|
102 | if (stdout) {
|
103 | let list = parseNpmListFromStdout(stdout);
|
104 | return list.forEach(i => {
|
105 | console.log(i);
|
106 | });
|
107 | }
|
108 | if (stderr) {
|
109 | return console.log(chalk.red("Error: ") + stderr);
|
110 | }
|
111 | });
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function collectDependencies() {
|
119 | let pkg;
|
120 | try {
|
121 | pkg = {
|
122 | exports: {}
|
123 | };
|
124 | pkgInfo(pkg, {
|
125 | dir: cwd,
|
126 | include: ["name", "version", "dependencies", "devDependencies"]
|
127 | });
|
128 | } catch (e) {
|
129 | console.log(chalk.redBright("No package.json found"));
|
130 | }
|
131 | return pkg;
|
132 | }
|
133 | module.exports.collectDependencies = collectDependencies;
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function parseListFromPkgOutput({
|
140 | exports: {
|
141 | name,
|
142 | version,
|
143 | dependencies,
|
144 | devDependencies
|
145 | }
|
146 | }) {
|
147 | if (!name && !version) return;
|
148 |
|
149 | let list = [];
|
150 |
|
151 | (function printTitle() {
|
152 | if (version) {
|
153 | console.log(chalk.blueBright(name + '@' + version));
|
154 | } else {
|
155 | console.log(name);
|
156 | }
|
157 | })();
|
158 |
|
159 | if (dependencies) {
|
160 | list.push(chalk.underline('Dependencies'));
|
161 | list = list.concat(objectToList(dependencies));
|
162 | }
|
163 |
|
164 | if (devDependencies) {
|
165 | list.push(chalk.underline('DevDependencies'));
|
166 | list = list.concat(objectToList(devDependencies));
|
167 | }
|
168 |
|
169 | return list;
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | function objectToList(deps = {}) {
|
179 | return Object.keys(deps).map(key => {
|
180 | let value = deps[key] ? deps[key].replace(/[^0-9.,]/g, "") : '';
|
181 | return key + '@' + chalk.grey(value);
|
182 | });
|
183 | }
|
184 | }
|
185 | module.exports.parseListFromPkgOutput = parseListFromPkgOutput;
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | function parseNpmListFromStdout(stdout) {
|
194 | if (!stdout) return;
|
195 | const lines = stdout.split('\n');
|
196 |
|
197 | return lines.map(i => {
|
198 | if (isTitle(i)) {
|
199 | return chalk.blueBright(i);
|
200 | } else if (isAddress(i)) {
|
201 | return chalk.grey(i);
|
202 | } else if (isSymlink(i)) {
|
203 | return chalk.magenta(i);
|
204 | } else {
|
205 | return i;
|
206 | }
|
207 | });
|
208 |
|
209 | function isTitle(i) {
|
210 | return i.includes('@') && !i.includes('->') && !i.includes('//');
|
211 | }
|
212 |
|
213 | function isAddress(i) {
|
214 |
|
215 | return i.includes('//');
|
216 | }
|
217 |
|
218 | function isSymlink(i) {
|
219 | return i.includes('@') && i.includes('->');
|
220 | }
|
221 | }
|
222 | module.exports.parseNpmListFromStdout = parseNpmListFromStdout; |
\ | No newline at end of file |