-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
103 lines (94 loc) · 2.58 KB
/
cli.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
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
import { basename, join } from "https://deno.land/[email protected]/path/mod.ts";
import { findAndReplace } from "./change.ts";
/**
* Recursively find all files in a directory
* @param path The starting parent path
* @param ignore The files to ignore
*/
export async function* recursiveReaddir(
path = Deno.cwd(),
ignore: string[] = [],
): AsyncGenerator<string, void> {
for await (const dirEntry of Deno.readDir(path)) {
if (ignore.includes(dirEntry.name)) continue;
if (dirEntry.isDirectory) {
yield* recursiveReaddir(join(path, dirEntry.name), ignore);
} else if (dirEntry.isFile) {
yield join(path, dirEntry.name);
}
}
}
async function update(
quiet = false,
check = false,
ignore: string[] = [],
lineIgnore = "i-deno-outdated",
debug = false,
) {
let count = 0;
// TODO .gitignore
for await (
const file of recursiveReaddir(Deno.cwd(), [...ignore, ".git", "deno.lock"])
) {
if (ignore.includes(basename(file))) {
if (debug) console.log(`Ignoring ${file}`);
continue;
}
if (debug) console.log(`Attempting to update ${file}`);
const originalSource = await Deno.readTextFile(file);
const newSource = await findAndReplace(originalSource, lineIgnore);
if (newSource !== originalSource) {
if (check) {
console.log(`${file} needs updating`);
} else {
await Deno.writeTextFile(
file,
newSource,
);
}
count++;
if (!quiet) console.log(file);
}
}
return count;
}
await new Command()
.name("deno-outdated")
.version("0.3.0")
.option("-d, --debug", "Show all scanned files")
.option("-q, --quiet", "Silence any output")
.option(
"-c, --check",
"Check files without updating them",
)
.option(
"-l --line-ignore [line-ignore:string]",
"The text of the comment to ignore",
{
default: "i-deno-outdated",
},
)
.option("-i --ignore [ignore...:string]]", "list of files to ignore", {
separator: " ",
})
.description(
"Check and update outdated dependencies for various 3rd party vendors",
)
.action(async ({ quiet, ignore, lineIgnore, debug, check }) => {
const count = await update(
quiet,
check,
Array.isArray(ignore) ? ignore : [],
typeof lineIgnore === "string" ? lineIgnore : "i-deno-outdated",
debug,
);
if (!quiet) {
console.log(
`${check ? "Checked" : "Updated"} ${count} file${
count === 1 ? "" : "s"
}`,
);
}
})
.parse(Deno.args);