-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
53 lines (44 loc) · 1.37 KB
/
main.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
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { APIGroupVersion } from "../spec/mod.ts";
import { emitVersion } from "./mod.ts";
async function main(argv = Deno.args) {
const { _: args, write }: { _: string[]; write: boolean } = parse(argv, {
boolean: ["write"],
});
for (const source of args) {
await emit(source, write);
}
}
async function emit(source: string, write: boolean) {
const { isDirectory } = await Deno.stat(source);
if (isDirectory) {
for await (const entry of walk(source, {
includeDirs: false,
exts: [".json"],
})) {
await emitFile(entry.path, write);
}
} else {
await emitFile(source, write);
}
}
async function emitFile(source: string, write: boolean) {
const contents = JSON.parse(await Deno.readTextFile(source));
if (!isAPIVersion(contents)) return;
const generated = emitVersion(contents, "deno");
if (write) {
await Deno.writeTextFile(source.replace(/[.]json$/, ".ts"), generated);
} else {
console.log(generated);
}
}
function isAPIVersion(value: unknown): value is APIGroupVersion {
return (
value != null &&
typeof value === "object" &&
(value as APIGroupVersion).apiVersion == "spec.kure.sh/v1alpha1" &&
(value as APIGroupVersion).kind === "APIGroupVersion"
);
}
await main();