-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathclean.ts
386 lines (332 loc) · 9.48 KB
/
clean.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { color } from "../color";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import * as constants from "../constants";
import {
IProjectCleanupResult,
IProjectCleanupService,
IProjectConfigService,
IProjectService,
} from "../definitions/project";
import type { PromptObject } from "prompts";
import { IOptions } from "../declarations";
import {
ITerminalSpinner,
ITerminalSpinnerService,
} from "../definitions/terminal-spinner-service";
import { IChildProcess } from "../common/declarations";
import * as os from "os";
import { resolve } from "path";
import { readdir } from "fs/promises";
import { isInteractive } from "../common/helpers";
const CLIPath = resolve(__dirname, "..", "..", "bin", "nativescript.js");
function bytesToHumanReadable(bytes: number): string {
const units = ["B", "KB", "MB", "GB", "TB"];
let unit = 0;
while (bytes >= 1024) {
bytes /= 1024;
unit++;
}
return `${bytes.toFixed(2)} ${units[unit]}`;
}
/**
* A helper function to map an array of values to promises with a concurrency limit.
* The mapper function should return a promise. It will be called for each value in the values array.
* The concurrency limit is the number of promises that can be running at the same time.
*
* This function will return a promise that resolves when all values have been mapped.
*
* @param values A static array of values to map to promises
* @param mapper A function that maps a value to a promise
* @param concurrency The number of promises that can be running at the same time
* @returns Promise<void>
*/
function promiseMap<T>(
values: T[],
mapper: (value: T) => Promise<void>,
concurrency = 10
) {
let index = 0;
let pending = 0;
let done = false;
return new Promise<void>((resolve, reject) => {
const next = () => {
done = index === values.length;
if (done && pending === 0) {
return resolve();
}
while (pending < concurrency && index < values.length) {
const value = values[index++];
pending++;
mapper(value)
.then(() => {
pending--;
next();
})
.catch();
}
};
next();
});
}
export class CleanCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(
private $projectCleanupService: IProjectCleanupService,
private $projectConfigService: IProjectConfigService,
private $terminalSpinnerService: ITerminalSpinnerService,
private $projectService: IProjectService,
private $prompter: IPrompter,
private $logger: ILogger,
private $options: IOptions,
private $childProcess: IChildProcess
) {}
public async execute(args: string[]): Promise<void> {
const isDryRun = this.$options.dryRun ?? false;
const isJSON = this.$options.json ?? false;
const spinner = this.$terminalSpinnerService.createSpinner({
isSilent: isJSON,
});
if (!this.$projectService.isValidNativeScriptProject()) {
return this.cleanMultipleProjects(spinner);
}
spinner.start("Cleaning project...\n");
let pathsToClean = [
constants.HOOKS_DIR_NAME,
constants.PLATFORMS_DIR_NAME,
constants.NODE_MODULES_FOLDER_NAME,
constants.PACKAGE_LOCK_JSON_FILE_NAME,
];
try {
const overridePathsToClean =
this.$projectConfigService.getValue("cli.pathsToClean");
const additionalPaths = this.$projectConfigService.getValue(
"cli.additionalPathsToClean"
);
// allow overriding default paths to clean
if (Array.isArray(overridePathsToClean)) {
pathsToClean = overridePathsToClean;
}
if (Array.isArray(additionalPaths)) {
pathsToClean.push(...additionalPaths);
}
} catch (err) {
// ignore
}
const res = await this.$projectCleanupService.clean(pathsToClean, {
dryRun: isDryRun,
silent: isJSON,
stats: isJSON,
});
if (res.stats && isJSON) {
console.log(
JSON.stringify(
{
ok: res.ok,
dryRun: isDryRun,
stats: Object.fromEntries(res.stats.entries()),
},
null,
2
)
);
return;
}
if (res.ok) {
spinner.succeed("Project successfully cleaned.");
} else {
spinner.fail(color.red("Project unsuccessfully cleaned."));
}
}
private async cleanMultipleProjects(spinner: ITerminalSpinner) {
if (!isInteractive() || this.$options.json) {
// interactive terminal is required, and we can't output json in an interactive command.
this.$logger.warn("No project found in the current directory.");
return;
}
const shouldScan = await this.$prompter.confirm(
"No project found in the current directory. Would you like to scan for all projects in sub-directories instead?"
);
if (!shouldScan) {
return;
}
spinner.start("Scanning for projects... Please wait.");
const paths = await this.getNSProjectPathsInDirectory();
spinner.succeed(`Found ${paths.length} projects.`);
let computed = 0;
const updateProgress = () => {
const current = color.grey(`${computed}/${paths.length}`);
spinner.start(
`Gathering cleanable sizes. This may take a while... ${current}`
);
};
// update the progress initially
updateProgress();
const projects = new Map<string, number>();
await promiseMap(
paths,
(p) => {
return this.$childProcess
.exec(`node ${CLIPath} clean --dry-run --json --disable-analytics`, {
cwd: p,
})
.then((res) => {
const paths: Record<string, number> = JSON.parse(res).stats;
return Object.values(paths).reduce((a, b) => a + b, 0);
})
.catch((err) => {
this.$logger.trace(
"Failed to get project size for %s, Error is:",
p,
err
);
return -1;
})
.then((size) => {
if (size > 0 || size === -1) {
// only store size if it's larger than 0 or -1 (error while getting size)
projects.set(p, size);
}
// update the progress after each processed project
computed++;
updateProgress();
});
},
os.cpus().length
);
spinner.clear();
spinner.stop();
this.$logger.clearScreen();
const totalSize = Array.from(projects.values())
.filter((s) => s > 0)
.reduce((a, b) => a + b, 0);
const pathsToClean = await this.$prompter.promptForChoice(
`Found ${
projects.size
} cleanable project(s) with a total size of: ${color.green(
bytesToHumanReadable(totalSize)
)}. Select projects to clean`,
Array.from(projects.keys()).map((p) => {
const size = projects.get(p);
let description;
if (size === -1) {
description = " - could not get size";
} else {
description = ` - ${bytesToHumanReadable(size)}`;
}
return {
title: `${p}${color.grey(description)}`,
value: p,
};
}),
true,
{
optionsPerPage: process.stdout.rows - 6, // 6 lines are taken up by the instructions
} as Partial<PromptObject>
);
this.$logger.clearScreen();
spinner.warn(
`This will run "${color.yellow(
`ns clean`
)}" in all the selected projects and ${color.red.bold(
"delete files from your system"
)}!`
);
spinner.warn(`This action cannot be undone!`);
let confirmed = await this.$prompter.confirm(
"Are you sure you want to clean the selected projects?"
);
if (!confirmed) {
return;
}
spinner.info("Cleaning... This might take a while...");
let totalSizeCleaned = 0;
for (let i = 0; i < pathsToClean.length; i++) {
const currentPath = pathsToClean[i];
spinner.start(
`Cleaning ${color.cyan(currentPath)}... ${i + 1}/${pathsToClean.length}`
);
const ok = await this.$childProcess
.exec(
`node ${CLIPath} clean ${
this.$options.dryRun ? "--dry-run" : ""
} --json --disable-analytics`,
{
cwd: currentPath,
}
)
.then((res) => {
const cleanupRes = JSON.parse(res) as IProjectCleanupResult;
return cleanupRes.ok;
})
.catch((err) => {
this.$logger.trace('Failed to clean project "%s"', currentPath, err);
return false;
});
if (ok) {
const cleanedSize = projects.get(currentPath);
const cleanedSizeStr = color.grey(
`- ${bytesToHumanReadable(cleanedSize)}`
);
spinner.succeed(`Cleaned ${color.cyan(currentPath)} ${cleanedSizeStr}`);
totalSizeCleaned += cleanedSize;
} else {
spinner.fail(`Failed to clean ${color.cyan(currentPath)} - skipped`);
}
}
spinner.clear();
spinner.stop();
spinner.succeed(
`Done! We've just freed up ${color.green(
bytesToHumanReadable(totalSizeCleaned)
)}! Woohoo! 🎉`
);
if (this.$options.dryRun) {
spinner.info(
'Note: the "--dry-run" flag was used, so no files were actually deleted.'
);
}
}
private async getNSProjectPathsInDirectory(
dir = process.cwd()
): Promise<string[]> {
let nsDirs: string[] = [];
const getFiles = async (dir: string) => {
if (dir.includes("node_modules")) {
// skip traversing node_modules
return;
}
const dirents = await readdir(dir, { withFileTypes: true }).catch(
(err) => {
this.$logger.trace(
'Failed to read directory "%s". Error is:',
dir,
err
);
return [];
}
);
const hasNSConfig = dirents.some(
(ent) =>
ent.name.includes("nativescript.config.ts") ||
ent.name.includes("nativescript.config.js")
);
if (hasNSConfig) {
nsDirs.push(dir);
// found a NativeScript project, stop traversing
return;
}
await Promise.all(
dirents.map((dirent: any) => {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
return getFiles(res);
}
})
);
};
await getFiles(dir);
return nsDirs;
}
}
injector.registerCommand("clean", CleanCommand);