This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
88 lines (73 loc) · 2.53 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
import { cleanManifest, createManifest } from "./manifest.ts";
import { validateAndCreateFunctions } from "./functions.ts";
import { Options } from "./types.ts";
import { parse, path } from "./deps.ts";
const run = async () => {
const start = Date.now();
// We could add additional arguments to indicate things like only generate the manifest, or only functions
let { source, output, manifest: manifestOnly = false } = parse(Deno.args);
// If we're generating functions, default output to a relative dist folder
if (!output && !manifestOnly) {
output = "dist";
}
const outputDirectory = output
? (path.isAbsolute(output) ? output : path.join(Deno.cwd(), output || ""))
: undefined;
const workingDirectory = path.isAbsolute(source || "")
? source
: path.join(Deno.cwd(), source || "");
const options: Options = {
manifestOnly,
workingDirectory,
outputDirectory,
// deno-lint-ignore no-explicit-any
log: (...args: any) => console.log(...args),
};
// Disable logging to stdout if we're outputing a manifest.json file to stdout
if (options.manifestOnly) {
options.log = () => {};
}
options.log(options);
// Clean output directory
if (options.outputDirectory) {
const removedDirectory = await removeDirectory(options.outputDirectory);
if (removedDirectory) {
options.log(`remove directory: ${options.outputDirectory}`);
}
}
// Generate Manifest
const generatedManifest = await createManifest(options);
await validateAndCreateFunctions(options, generatedManifest);
const prunedManifest = cleanManifest(generatedManifest);
// If no output was provided, print to stdout
if (!options.outputDirectory) {
// We explicitly are writing this to stdout here, not using log()
console.log(JSON.stringify(prunedManifest, null, 2));
} else {
await Deno.writeTextFile(
path.join(options.outputDirectory, "manifest.json"),
JSON.stringify(prunedManifest, null, 2),
);
options.log(`wrote manifest.json`);
}
const duration = Date.now() - start;
options.log(`duration: ${duration}ms`);
};
/**
* Recursively deletes the specified directory.
*
* @param directoryPath the directory to delete
* @returns true when the directory is deleted or throws unexpected errors
*/
async function removeDirectory(directoryPath: string): Promise<boolean> {
try {
await Deno.remove(directoryPath, { recursive: true });
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
run();