Repository navigation
Expand file tree
/
Copy pathbuild-output.mjs
More file actions
63 lines (58 loc) · 2.37 KB
/
Copy pathbuild-output.mjs
File metadata and controls
63 lines (58 loc) · 2.37 KB
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
// Packages the built site (apps/cli-docs/dist) as Vercel Build Output API v3:
// the static files, the routes below, and the /install/<handoff code> Edge
// Function. Usage: node build-output.mjs <output dir, e.g. .vercel/output>
import { cpSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { HANDOFF_CODE } from "./install-handoff.mjs";
const HANDOFF_FUNCTION = "install-handoff";
const routes = [
// Browsers that open /install or /install.ps1 get the highlighted source
// page; curl, wget, and irm still get the raw script.
{ src: "^/install(\\.ps1)?$", headers: { Vary: "Accept" }, continue: true },
{
src: "^/install(\\.ps1)?$",
has: [{ type: "header", key: "accept", value: { re: ".*text/html.*" } }],
dest: "/install-source/install$1/index.html",
check: true,
},
// Only the raw script is plain text; the browser page above stays HTML.
{
src: "^/install$",
missing: [{ type: "header", key: "accept", value: { re: ".*text/html.*" } }],
headers: { "Content-Type": "text/plain; charset=utf-8" },
continue: true,
},
{
src: `^/install/(${HANDOFF_CODE.source.slice(1, -1)})$`,
dest: `/${HANDOFF_FUNCTION}?code=$1`,
},
];
export function buildOutput(distDir, outputDir) {
rmSync(outputDir, { recursive: true, force: true });
cpSync(distDir, join(outputDir, "static"), { recursive: true });
writeFileSync(
join(outputDir, "config.json"),
`${JSON.stringify({ version: 3, routes })}\n`,
);
const functionDir = join(outputDir, "functions", `${HANDOFF_FUNCTION}.func`);
mkdirSync(functionDir, { recursive: true });
writeFileSync(
join(functionDir, ".vc-config.json"),
`${JSON.stringify({ runtime: "edge", entrypoint: "index.js" })}\n`,
);
const handler = readFileSync(new URL("./install-handoff.mjs", import.meta.url), "utf8");
const installScript = readFileSync(join(distDir, "install"), "utf8");
writeFileSync(
join(functionDir, "index.js"),
`${handler}\nexport default createHandler(${JSON.stringify(installScript)});\n`,
);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const outputDir = process.argv[2];
if (!outputDir) {
console.error("usage: node build-output.mjs <output dir>");
process.exit(2);
}
buildOutput(fileURLToPath(new URL("../dist", import.meta.url)), outputDir);
}