-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathclean.ts
More file actions
67 lines (58 loc) · 2.84 KB
/
clean.ts
File metadata and controls
67 lines (58 loc) · 2.84 KB
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { error } from 'node:console';
import { resolve, sep } from 'node:path';
import { filepath } from '../src/Utility/Filesystem/filepath';
import { verbose } from '../src/Utility/Text/streams';
import { $root, Git, brightGreen, cyan, getModifiedIgnoredFiles, rimraf } from './common';
// notes:
// list all gitignore'd files that are modified: `git clean -Xd -n`
// list all untracked and ignored files that are modified/created: `git clean -Xd -n`
export async function main() {
await rimraf(resolve($root, 'dist'));
}
export async function all() {
await rimraf(...(await getModifiedIgnoredFiles()).filter((each): each is string => each !== undefined && !each.includes('node_modules')));
}
export async function reset() {
verbose(`Resetting all .gitignored files in extension`);
await rimraf(...(await getModifiedIgnoredFiles()).filter((each): each is string => each !== undefined));
}
async function details(files: string[]) {
const results = await Promise.all(files.filter(each => each).map(async (each) => {
const [, stats] = await filepath.stats(each);
if (!stats) {
return null;
}
return {
filename: stats.isDirectory() ? cyan(`${each}${sep}**`) : brightGreen(`${each}`),
date: stats.mtime.toLocaleDateString().replace(/\b(\d)\//g, '0$1\/'),
time: stats.mtime.toLocaleTimeString().replace(/^(\d)\:/g, '0$1:'),
modified: stats.mtime
};
}));
let all = results.filter((each): each is NonNullable<typeof each> => each !== null);
all = all.sort((a, b) => a.modified.getTime() - b.modified.getTime());
// print a formatted table so the date and time are aligned
const max = all.reduce((max, each) => Math.max(max, each.filename.length), 0);
all.forEach(each => console.log(` ${each.filename.padEnd(max)} [${each.date} ${each.time}]`));
console.log('');
}
export async function show(opt?: string) {
switch (opt?.toLowerCase()) {
case 'new':
console.log(cyan('\n\nNew files:'));
const r = await Git('ls-files', '--others', '--exclude-standard', '-z');
return details(r.stdio.all().map(each => resolve(each.trim().replace(/\0/g, ''))));
case undefined:
case '':
case 'ignored':
case 'untracked':
console.log(cyan('\n\nUntracked+Ignored files:'));
return details((await getModifiedIgnoredFiles()).filter((each): each is string => each !== undefined));
default:
return error(`Unknown option '${opt}'`);
}
}