1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.printTable = exports.ls = exports.cmd = exports.args = exports.description = exports.alias = exports.name = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("../common");
|
6 | exports.name = 'ls';
|
7 | exports.alias = 'l';
|
8 | exports.description = 'Lists modules in dependency order.';
|
9 | exports.args = {
|
10 | '-D': 'Show all module dependencies (omit for local only).',
|
11 | '-i': 'Include ignored modules.',
|
12 | '-p': 'Show path to module.',
|
13 | '-n': 'Retrieve registry details from NPM.',
|
14 | '--no-formatting': 'Simple list without table formatting.',
|
15 | };
|
16 | function cmd(args) {
|
17 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
18 | var options;
|
19 | return tslib_1.__generator(this, function (_a) {
|
20 | switch (_a.label) {
|
21 | case 0:
|
22 | options = (args && args.options) || {};
|
23 | return [4, ls({
|
24 | dependencies: options.D ? 'all' : 'local',
|
25 | includeIgnored: options.i,
|
26 | showPath: options.p,
|
27 | npm: options.n,
|
28 | formatting: options.formatting,
|
29 | })];
|
30 | case 1:
|
31 | _a.sent();
|
32 | return [2];
|
33 | }
|
34 | });
|
35 | });
|
36 | }
|
37 | exports.cmd = cmd;
|
38 | function ls(options) {
|
39 | if (options === void 0) { options = {}; }
|
40 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
41 | var _a, includeIgnored, _b, npm, formatting, settings, modules;
|
42 | return tslib_1.__generator(this, function (_c) {
|
43 | switch (_c.label) {
|
44 | case 0:
|
45 | _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a, _b = options.npm, npm = _b === void 0 ? false : _b;
|
46 | formatting = options.formatting === false ? false : true;
|
47 | return [4, common_1.loadSettings({ npm: npm, spinner: npm })];
|
48 | case 1:
|
49 | settings = _c.sent();
|
50 | if (!settings) {
|
51 | common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR);
|
52 | return [2];
|
53 | }
|
54 | modules = settings.modules.filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); });
|
55 | if (formatting) {
|
56 | printTable(modules, tslib_1.__assign(tslib_1.__assign({}, options), { basePath: common_1.fs.dirname(settings.path), columns: options.columns }));
|
57 | common_1.log.info();
|
58 | }
|
59 | else {
|
60 | common_1.log.info();
|
61 | modules.forEach(function (pkg) { return common_1.log.info(pkg.name); });
|
62 | common_1.log.info();
|
63 | }
|
64 | return [2, {
|
65 | modules: modules,
|
66 | settings: settings,
|
67 | }];
|
68 | }
|
69 | });
|
70 | });
|
71 | }
|
72 | exports.ls = ls;
|
73 | function printTable(modules, options) {
|
74 | if (options === void 0) { options = {}; }
|
75 | var _a = options.dependencies, dependencies = _a === void 0 ? 'none' : _a, _b = options.includeIgnored, includeIgnored = _b === void 0 ? false : _b, _c = options.showPath, showPath = _c === void 0 ? false : _c, dependants = options.dependants, basePath = options.basePath, _d = options.columns, columns = _d === void 0 ? [] : _d;
|
76 | var showAllDependencies = dependencies === 'all';
|
77 | var showDependants = dependants !== undefined;
|
78 | var listDependences = function (pkg, modules) {
|
79 | return pkg.dependencies
|
80 | .filter(function (dep) { return (showAllDependencies ? true : dep.isLocal); })
|
81 | .filter(function (dep) { return (dep.package ? common_1.filter.includeIgnored(dep.package, includeIgnored) : true); })
|
82 | .map(function (dep) {
|
83 | var isIgnored = dep.package && dep.package.isIgnored;
|
84 | var name = isIgnored
|
85 | ? common_1.log.gray(dep.name)
|
86 | : dep.isLocal
|
87 | ? common_1.log.cyan(dep.name)
|
88 | : common_1.log.gray(dep.name);
|
89 | return name + " " + common_1.log.gray(dep.version);
|
90 | })
|
91 | .join('\n');
|
92 | };
|
93 | var listDependants = function (dependants) {
|
94 | if (!dependants || dependants.length === 0) {
|
95 | return common_1.log.yellow('dependant');
|
96 | }
|
97 | return dependants
|
98 | .filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); })
|
99 | .map(function (pkg) {
|
100 | var name = pkg.isIgnored ? common_1.log.gray(pkg.name) : common_1.formatModuleName(pkg.name);
|
101 | return name + " " + common_1.log.gray(pkg.version);
|
102 | })
|
103 | .join('\n');
|
104 | };
|
105 | var column = {
|
106 | module: {
|
107 | head: 'module',
|
108 | render: function (pkg) {
|
109 | var text = common_1.formatModuleName(pkg.name);
|
110 | return " " + text;
|
111 | },
|
112 | },
|
113 | version: {
|
114 | head: 'version ',
|
115 | render: function (pkg) {
|
116 | var npmVersion = pkg.npm && pkg.npm.latest;
|
117 | var format = function (args) {
|
118 | var from = args.from, to = args.to;
|
119 | var max = Math.max(from.text.length, 12);
|
120 | var left = (from.text + " ").substring(0, max);
|
121 | var right = to.color("\u2190 NPM " + to.text);
|
122 | return from.color(left) + " " + right;
|
123 | };
|
124 | if (npmVersion && common_1.semver.gt(pkg.version, npmVersion)) {
|
125 | return format({
|
126 | from: { text: pkg.version, color: common_1.log.yellow },
|
127 | to: { text: npmVersion, color: common_1.log.gray },
|
128 | });
|
129 | }
|
130 | else if (npmVersion && common_1.semver.lt(pkg.version, npmVersion)) {
|
131 | return format({
|
132 | from: { text: pkg.version, color: common_1.log.gray },
|
133 | to: { text: npmVersion, color: common_1.log.magenta },
|
134 | });
|
135 | }
|
136 | else {
|
137 | return common_1.log.magenta(pkg.version);
|
138 | }
|
139 | },
|
140 | },
|
141 | dependencies: {
|
142 | head: 'dependencies',
|
143 | render: function (pkg) { return listDependences(pkg, modules); },
|
144 | },
|
145 | dependants: {
|
146 | head: 'dependants',
|
147 | render: function (pkg) { return listDependants(dependants || []); },
|
148 | },
|
149 | path: {
|
150 | head: 'path',
|
151 | render: function (pkg) {
|
152 | var path = basePath && pkg.dir.startsWith(basePath)
|
153 | ? pkg.dir.substring(basePath.length, pkg.dir.length)
|
154 | : pkg.dir;
|
155 | return common_1.log.gray(path);
|
156 | },
|
157 | },
|
158 | };
|
159 | var logModules = function (modules) {
|
160 | var cols = [];
|
161 | var addColumn = function (col, include) {
|
162 | if (include === void 0) { include = true; }
|
163 | if (include) {
|
164 | cols.push(col);
|
165 | }
|
166 | };
|
167 | addColumn(column.module);
|
168 | addColumn(column.version);
|
169 | addColumn(column.dependencies, dependencies !== 'none');
|
170 | addColumn(column.dependants, showDependants);
|
171 | addColumn(column.path, showPath);
|
172 | (columns || []).forEach(function (col) { return addColumn(col); });
|
173 | var head = cols.map(function (col) { return common_1.log.gray(col.head); });
|
174 | var builder = common_1.log.table({ head: head, border: false });
|
175 | modules.forEach(function (pkg) {
|
176 | var row = [];
|
177 | cols.forEach(function (col) { return row.push(col.render(pkg) + " "); });
|
178 | builder.add(row);
|
179 | });
|
180 | builder.log();
|
181 | };
|
182 | if (modules.length > 0) {
|
183 | logModules(modules);
|
184 | }
|
185 | }
|
186 | exports.printTable = printTable;
|