UNPKG

7.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.cmd = exports.args = exports.description = exports.alias = exports.name = void 0;
4var tslib_1 = require("tslib");
5var path_1 = require("path");
6var common_1 = require("../common");
7exports.name = 'delete';
8exports.alias = 'del';
9exports.description = "Deletes transient files across projects (eg. logs, node_modules etc).";
10exports.args = {
11 '-i': 'Include ignored modules.',
12};
13function cmd(args) {
14 return tslib_1.__awaiter(this, void 0, void 0, function () {
15 var options, includeIgnored, settings, response, modules, _a;
16 return tslib_1.__generator(this, function (_b) {
17 switch (_b.label) {
18 case 0:
19 options = (args && args.options) || {};
20 includeIgnored = options.i || false;
21 return [4, common_1.loadSettings()];
22 case 1:
23 settings = _b.sent();
24 if (!settings) {
25 common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR);
26 return [2];
27 }
28 return [4, common_1.inquirer.prompt([
29 {
30 type: 'list',
31 name: 'type',
32 message: 'Delete?',
33 choices: [
34 'logs (error)',
35 '.tslint.json.OLD',
36 'yarn.lock',
37 'package-lock.json',
38 'node_modules',
39 ],
40 },
41 ])];
42 case 2:
43 response = (_b.sent());
44 common_1.log.info();
45 modules = settings.modules
46 .filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); })
47 .map(function (m) { return m.dir; });
48 _a = response.type;
49 switch (_a) {
50 case 'yarn.lock': return [3, 3];
51 case 'package-lock.json': return [3, 5];
52 case '.tslint.json.OLD': return [3, 7];
53 case 'logs (error)': return [3, 9];
54 case 'node_modules': return [3, 11];
55 }
56 return [3, 13];
57 case 3: return [4, deleteAfterPrompt(modules.map(function (dir) { return dir + "/yarn.lock"; }))];
58 case 4:
59 _b.sent();
60 return [3, 14];
61 case 5: return [4, deleteAfterPrompt(modules.map(function (dir) { return dir + "/package-lock.json"; }))];
62 case 6:
63 _b.sent();
64 return [3, 14];
65 case 7: return [4, deleteAfterPrompt(modules.map(function (dir) { return dir + "/.tslint.json.OLD"; }))];
66 case 8:
67 _b.sent();
68 return [3, 14];
69 case 9: return [4, deleteAfterPrompt(common_1.flatten([
70 modules.map(function (dir) { return dir + "/yarn-error.log"; }),
71 modules.map(function (dir) { return dir + "/npm-debug.log"; }),
72 ]))];
73 case 10:
74 _b.sent();
75 return [3, 14];
76 case 11: return [4, deleteAfterPrompt(modules.map(function (dir) { return dir + "/node_modules"; }))];
77 case 12:
78 _b.sent();
79 return [3, 14];
80 case 13:
81 common_1.log.error("'" + response.type + "' not supported.");
82 return [3, 14];
83 case 14:
84 common_1.log.info();
85 return [2];
86 }
87 });
88 });
89}
90exports.cmd = cmd;
91function deleteAfterPrompt(paths) {
92 return tslib_1.__awaiter(this, void 0, void 0, function () {
93 var response, _a;
94 return tslib_1.__generator(this, function (_b) {
95 switch (_b.label) {
96 case 0: return [4, common_1.filter.fileExists(paths)];
97 case 1:
98 paths = _b.sent();
99 if (paths.length === 0) {
100 common_1.log.info.gray('No files to delete.');
101 return [2, false];
102 }
103 common_1.log.info.cyan("\nDelete files:");
104 paths.forEach(function (path) {
105 common_1.log.info(" " + toDisplayPath(path));
106 });
107 common_1.log.info();
108 return [4, common_1.inquirer.prompt([
109 {
110 type: 'list',
111 name: 'confirm',
112 message: 'Are you sure?',
113 choices: ['No', 'Yes'],
114 },
115 ])];
116 case 2:
117 response = (_b.sent());
118 _a = response.confirm;
119 switch (_a) {
120 case 'No': return [3, 3];
121 case 'Yes': return [3, 4];
122 }
123 return [3, 6];
124 case 3:
125 common_1.log.info.gray("Nothing deleted.");
126 return [2, false];
127 case 4:
128 common_1.log.info();
129 return [4, deleteFiles(paths)];
130 case 5:
131 _b.sent();
132 return [2, true];
133 case 6: return [2, false];
134 }
135 });
136 });
137}
138function toDisplayPath(path) {
139 var root = path_1.parse(path);
140 var dir = path_1.parse(root.dir);
141 return common_1.log.gray(dir.dir + "/" + common_1.log.magenta(dir.base) + "/" + common_1.log.cyan(root.base));
142}
143function deleteFiles(paths) {
144 return tslib_1.__awaiter(this, void 0, void 0, function () {
145 var tasks, error_1;
146 var _this = this;
147 return tslib_1.__generator(this, function (_a) {
148 switch (_a.label) {
149 case 0:
150 tasks = paths.map(function (path) {
151 return {
152 title: common_1.log.cyan('Delete') + " " + toDisplayPath(path),
153 task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
154 return [2, common_1.tryDelete(path, { retry: 3 })];
155 }); }); },
156 };
157 });
158 _a.label = 1;
159 case 1:
160 _a.trys.push([1, 3, , 4]);
161 return [4, common_1.listr(tasks, { concurrent: true, exitOnError: false }).run()];
162 case 2:
163 _a.sent();
164 return [3, 4];
165 case 3:
166 error_1 = _a.sent();
167 return [3, 4];
168 case 4: return [2];
169 }
170 });
171 });
172}