-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
101 lines (81 loc) · 2.31 KB
/
mod.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
#!/usr/bin/env deno run --allow-read --allow-write
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { walk } from "https://deno.land/[email protected]/fs/mod.ts";
import { encode } from "https://deno.land/[email protected]/encoding/utf8.ts";
import { yellow, cyan } from "https://deno.land/[email protected]/fmt/colors.ts";
import ProgressBar from "https://deno.land/x/[email protected]/mod.ts";
import shuffle from "https://deno.land/x/shuffle/mod.ts";
const args = parse(Deno.args, {
boolean: true,
alias: {
force: "f",
help: "h",
},
});
const helpMsg = `This command could delete list half your files randomly.
USAGE:
thanos [FLAGS]
FLAGS:
-h, --help Prints help information
-f, --force Force delete
`;
if (args.help) {
print(helpMsg);
Deno.exit(0);
}
print("Thanos is comming");
await printDots(4, 2000);
print(`Thanos is scanning your directory ${cyan(Deno.cwd())} `);
await printDots(6, 3000);
async function getFilesNames(): Promise<string[]> {
const filePaths: string[] = [];
for await (const entry of walk(".")) {
if (entry.isFile) {
filePaths.push(entry.path);
}
}
return filePaths;
}
const files = await getFilesNames();
const deleteCount = Math.floor(files.length / 2);
console.log(`Thanos has found ${yellow(String(files.length))} files`);
await delay(300);
console.log(`Thanos will delete ${yellow(String(deleteCount))} files`);
await delay(300);
if (!args.force) {
console.log(`Good luck! Thanos is not run with flag '${cyan("--force")}'.`);
console.log(
`If you really want Thanos to delete half of the files, please run '${
cyan("thanos --force")
}'`,
);
Deno.exit(0);
}
const progress = new ProgressBar({
title: "deleting:",
total: deleteCount,
});
const filesWillDeleted = shuffle(files).slice(0, deleteCount);
for (let i = 0; i < deleteCount; i++) {
await Deno.remove(filesWillDeleted[i]);
progress.render(i);
}
progress.render(deleteCount);
print("\n");
function print(str: string): void {
Deno.stdout.writeSync(encode(str));
}
async function printDots(dots: number, ms = 1000): Promise<void> {
for (let i = 0; i < dots; i++) {
print(".");
await delay(ms / dots);
}
print("\n");
}
function delay(ms: number): Promise<void> {
return new Promise((r) => {
setTimeout(() => {
r();
}, ms);
});
}