UNPKG

11.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.outdated = exports.cmd = exports.description = exports.alias = exports.name = void 0;
4var tslib_1 = require("tslib");
5var common_1 = require("../common");
6var run_cmd_1 = require("./run.cmd");
7exports.name = 'outdated';
8exports.alias = 'o';
9exports.description = 'Checks all modules for outdated references on NPM.';
10function cmd(args) {
11 return tslib_1.__awaiter(this, void 0, void 0, function () {
12 return tslib_1.__generator(this, function (_a) {
13 outdated({});
14 return [2];
15 });
16 });
17}
18exports.cmd = cmd;
19function outdated(options) {
20 return tslib_1.__awaiter(this, void 0, void 0, function () {
21 var _a, includeIgnored, settings, modules, results, tasks, runner, error_1, updated, _b, _c;
22 var _this = this;
23 return tslib_1.__generator(this, function (_d) {
24 switch (_d.label) {
25 case 0:
26 _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a;
27 return [4, common_1.loadSettings()];
28 case 1:
29 settings = _d.sent();
30 if (!settings) {
31 common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR);
32 return [2];
33 }
34 modules = settings.modules.filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); });
35 common_1.log.info.gray("\nChecking for outdated modules:");
36 results = [];
37 tasks = modules.map(function (pkg) {
38 return {
39 title: "" + common_1.formatModuleName(pkg.name),
40 task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
41 var result;
42 return tslib_1.__generator(this, function (_a) {
43 switch (_a.label) {
44 case 0: return [4, getOutdated(pkg)];
45 case 1:
46 result = _a.sent();
47 if (result.modules.length > 0 || result.error) {
48 results.push(result);
49 }
50 return [2];
51 }
52 });
53 }); },
54 };
55 });
56 runner = common_1.listr(tasks, { concurrent: true, exitOnError: false });
57 _d.label = 2;
58 case 2:
59 _d.trys.push([2, 4, , 5]);
60 return [4, runner.run()];
61 case 3:
62 _d.sent();
63 return [3, 5];
64 case 4:
65 error_1 = _d.sent();
66 return [3, 5];
67 case 5:
68 if (!(results.length > 0)) return [3, 10];
69 common_1.log.info();
70 results.forEach(function (item) { return printOutdatedModule(item); });
71 _b = updatePackageJsonRefs;
72 _c = [modules];
73 return [4, promptToUpdate(results)];
74 case 6: return [4, _b.apply(void 0, _c.concat([_d.sent()]))];
75 case 7:
76 updated = _d.sent();
77 if (!(updated.length > 0)) return [3, 9];
78 return [4, run_cmd_1.run('yarn install', { concurrent: true, modules: updated, printStatus: false })];
79 case 8:
80 _d.sent();
81 _d.label = 9;
82 case 9: return [3, 11];
83 case 10:
84 common_1.log.info();
85 common_1.log.info.gray("All modules up-to-date.");
86 _d.label = 11;
87 case 11:
88 common_1.log.info();
89 return [2];
90 }
91 });
92 });
93}
94exports.outdated = outdated;
95function promptToUpdate(outdated) {
96 return tslib_1.__awaiter(this, void 0, void 0, function () {
97 var updates, choices, answer;
98 return tslib_1.__generator(this, function (_a) {
99 switch (_a.label) {
100 case 0:
101 if (outdated.length === 0) {
102 return [2, []];
103 }
104 updates = {};
105 outdated.forEach(function (outdated) {
106 outdated.modules.forEach(function (m) {
107 var name = m.name, latest = m.latest;
108 var current = updates[name] ? updates[name].latest : undefined;
109 if (!current || common_1.semver.gt(latest, current)) {
110 updates[name] = { name: name, latest: latest };
111 }
112 });
113 });
114 choices = Object.keys(updates).map(function (key) {
115 var update = updates[key];
116 var name = key + " \u279C " + update.latest;
117 return { name: name, value: update.name };
118 });
119 if (choices.length === 0) {
120 return [2, []];
121 }
122 return [4, common_1.inquirer.prompt({
123 name: 'update',
124 type: 'checkbox',
125 choices: choices,
126 pageSize: 30,
127 })];
128 case 1:
129 answer = _a.sent();
130 return [2, Object.keys(updates)
131 .map(function (key) { return updates[key]; })
132 .filter(function (update) { return answer.update.includes(update.name); })];
133 }
134 });
135 });
136}
137function updatePackageJsonRefs(modules, updates) {
138 return tslib_1.__awaiter(this, void 0, void 0, function () {
139 var updated, _loop_1, _i, updates_1, update;
140 var _this = this;
141 return tslib_1.__generator(this, function (_a) {
142 switch (_a.label) {
143 case 0:
144 if (updates.length === 0) {
145 return [2, []];
146 }
147 updated = [];
148 _loop_1 = function (update) {
149 return tslib_1.__generator(this, function (_a) {
150 switch (_a.label) {
151 case 0: return [4, Promise.all(modules.map(function (pkg) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
152 var changed;
153 return tslib_1.__generator(this, function (_a) {
154 switch (_a.label) {
155 case 0: return [4, common_1.updatePackageRef(pkg, update.name, update.latest, { save: true })];
156 case 1:
157 changed = _a.sent();
158 if (changed && !updated.some(function (m) { return m.name === pkg.name; })) {
159 updated = tslib_1.__spreadArrays(updated, [pkg]);
160 }
161 return [2];
162 }
163 });
164 }); }))];
165 case 1:
166 _a.sent();
167 return [2];
168 }
169 });
170 };
171 _i = 0, updates_1 = updates;
172 _a.label = 1;
173 case 1:
174 if (!(_i < updates_1.length)) return [3, 4];
175 update = updates_1[_i];
176 return [5, _loop_1(update)];
177 case 2:
178 _a.sent();
179 _a.label = 3;
180 case 3:
181 _i++;
182 return [3, 1];
183 case 4:
184 if (updated.length > 0) {
185 common_1.log.info.gray("\nUpdated:");
186 updated.forEach(function (pkg) {
187 common_1.log.info.gray(" - " + common_1.log.cyan(pkg.name));
188 });
189 common_1.log.info();
190 }
191 return [2, updated];
192 }
193 });
194 });
195}
196function getOutdated(pkg) {
197 return tslib_1.__awaiter(this, void 0, void 0, function () {
198 var result, cmd, res, _a, outdated_1, error, error_2;
199 return tslib_1.__generator(this, function (_b) {
200 switch (_b.label) {
201 case 0:
202 result = { name: pkg.name, modules: [] };
203 cmd = "cd " + pkg.dir + " && npm outdated --json";
204 _b.label = 1;
205 case 1:
206 _b.trys.push([1, 3, , 4]);
207 return [4, common_1.exec.cmd.run(cmd, { silent: true })];
208 case 2:
209 res = _b.sent();
210 _a = parseOutdated(res.info), outdated_1 = _a.outdated, error = _a.error;
211 result.modules = outdated_1;
212 result.error = error;
213 return [3, 4];
214 case 3:
215 error_2 = _b.sent();
216 result.error = error_2.message;
217 return [3, 4];
218 case 4: return [2, result];
219 }
220 });
221 });
222}
223function parseOutdated(stdout) {
224 if (!stdout || stdout.length === 0) {
225 return { outdated: [] };
226 }
227 var json = JSON.parse(stdout.join('\n'));
228 var error = json.error;
229 if (error) {
230 return { error: error.summary, outdated: [] };
231 }
232 var outdated = Object.keys(json).map(function (name) {
233 var _a = json[name], current = _a.current, wanted = _a.wanted, latest = _a.latest, location = _a.location;
234 var outdated = { name: name, current: current, wanted: wanted, latest: latest, location: location };
235 return outdated;
236 });
237 return { outdated: outdated };
238}
239function printOutdatedModule(outdated) {
240 common_1.log.info.yellow("" + outdated.name);
241 if (outdated.error) {
242 common_1.log.info.red(outdated.error);
243 }
244 var table = common_1.log.table({
245 head: [' dependency', 'current ', 'wanted ', 'latest'].map(function (label) { return common_1.log.gray(label); }),
246 border: false,
247 });
248 outdated.modules.forEach(function (item) {
249 var name = item.name, current = item.current, latest = item.latest;
250 var wanted = item.wanted === latest ? common_1.log.green(item.wanted) : common_1.log.magenta(item.wanted);
251 table.add([
252 common_1.log.gray(" \u2022 " + common_1.formatModuleName(name) + " "),
253 common_1.log.gray(current) + " ",
254 wanted + " ",
255 "" + common_1.log.green(latest),
256 ]);
257 });
258 if (outdated.modules.length > 0) {
259 table.log();
260 }
261 common_1.log.info();
262}