-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm.ts
73 lines (65 loc) · 1.71 KB
/
rm.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
import {
exists,
Command,
Logger,
} from "../deps.ts";
import { createFsCliLogger, displayResult } from "./tools/logger.ts";
import {
GlobalOptions,
assertValidCliOptions,
parseGlobalOptions,
} from "./tools/options.ts";
import {
search,
SearchOptions,
searchOptionsName,
parseSearchOptions,
addSearchOptions,
} from "./tools/search.ts";
export function addRmCommand(command: Command<any, any>) {
command
.command("rm <paths...:string>")
.description("rm -rf on each file and directory");
addSearchOptions(command);
command.action(rmCommand);
}
type RmOptions = GlobalOptions & SearchOptions;
async function rmCommand(options: any, inputs: string[]) {
const rmOptions = parseCliOptions(options);
const logger = await createFsCliLogger(rmOptions);
const remove = removeHOF(logger, rmOptions);
const paths = await search(inputs, rmOptions);
let actualyRemoved = 0;
for await (const path of paths) {
if (await remove(path)) {
actualyRemoved++;
}
}
displayResult("Removed", actualyRemoved, rmOptions);
}
const removeHOF = (logger: Logger, { dry }: GlobalOptions) => {
return async (path: string) => {
if (!(await exists(path))) {
return false
}
logger.info(`Deleting ${path}`);
if (!dry) {
await Deno.remove(path, { recursive: true });
}
return true;
};
};
const parseCliOptions = (options: any): RmOptions => {
assertValidCliOptions(
options,
...searchOptionsName,
);
const rmOptions = {
...parseGlobalOptions(options),
...parseSearchOptions(options),
};
if (!rmOptions.includeFiles && !rmOptions.includeDirs) {
throw new Error("Without files and dirs, won't find much to delete !");
}
return rmOptions;
};