Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/webui/quarto-preview/build.ts
6456 views
1
import { dirname } from "node:path";
2
3
// ensure this is treated as a module
4
export {};
5
6
const args = Deno.args;
7
8
// establish target js build time
9
const kQuartoPreviewJs = "../../resources/preview/quarto-preview.js";
10
let jsBuildTime: number;
11
try {
12
jsBuildTime = Deno.statSync(kQuartoPreviewJs).mtime?.valueOf() || 0;
13
} catch {
14
jsBuildTime = 0;
15
}
16
17
const buildFromArgs = () => {
18
return args.includes("--force");
19
};
20
21
const run = async (args: string[], quiet = true) => {
22
console.log(`Running: npm ${args.join(" ")}`);
23
const command = new Deno.Command("npm", {
24
args,
25
});
26
const output = await command.output();
27
if (output.success || quiet) {
28
return output;
29
}
30
console.error("Command failed");
31
console.log(new TextDecoder().decode(output.stderr));
32
Deno.exit(output.code);
33
};
34
35
const buildFromGit = async () => {
36
let output: Deno.CommandOutput;
37
try {
38
const command = new Deno.Command("git", { args: ["ls-files"] });
39
output = await command.output();
40
} catch {
41
// git not installed, rebuild
42
return true;
43
}
44
if (!output.success) {
45
return true;
46
}
47
const stdout = new TextDecoder().decode(output.stdout);
48
const files = stdout.split("\n").filter((line) => line.length > 0);
49
return files.some((file) =>
50
Deno.statSync(file).mtime!.valueOf() > jsBuildTime
51
);
52
};
53
54
// check if any of our repo files have a later time
55
const build = buildFromArgs() || await buildFromGit();
56
57
if (!build) {
58
console.log("No changes to quarto-preview.js, skipping build");
59
Deno.exit(0);
60
}
61
62
console.log("Building quarto-preview.js");
63
console.log("Installing...");
64
await run(["install"], false);
65
console.log("Building...");
66
await run(["run", "build"], false);
67
68