Files
test-repo/scripts/build-website.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-09-11 09:55:46 +08:00
#!/usr/bin/env node
2026-02-05 22:29:59 +08:00
import assert from "node:assert/strict";
import path from "node:path";
import url from "node:url";
2025-02-25 06:27:02 +08:00
import esbuild from "esbuild";
import fastGlob from "fast-glob";
2025-02-25 08:07:03 +08:00
import spawn from "nano-spawn";
2025-02-25 06:27:02 +08:00
import serialize from "serialize-javascript";
2021-09-11 09:55:46 +08:00
import {
copyFile,
DIST_DIR,
PROJECT_ROOT,
WEBSITE_DIR,
2021-09-11 09:55:46 +08:00
writeFile,
2025-12-01 07:25:25 +08:00
} from "./utilities/index.js";
2021-09-11 09:55:46 +08:00
const runYarn = (command, args, options) =>
2025-02-25 08:07:03 +08:00
spawn("yarn", [command, ...args], { stdio: "inherit", ...options });
2026-02-05 22:29:59 +08:00
2021-09-11 09:55:46 +08:00
const IS_PULL_REQUEST = process.env.PULL_REQUEST === "true";
2026-02-05 22:29:59 +08:00
const NODE_MODULES_DIR = url.fileURLToPath(
new URL("../node_modules", import.meta.url),
);
2021-09-11 09:55:46 +08:00
2025-02-25 08:29:09 +08:00
async function writeScript(file, code) {
const { code: minified } = await esbuild.transform(code, {
loader: "js",
minify: true,
});
2026-02-05 22:29:59 +08:00
await writeFile(file, minified.trim());
2025-02-25 08:29:09 +08:00
}
2026-02-05 22:29:59 +08:00
/** @param {"stable" | "next"} version */
async function buildPlaygroundFiles(version) {
assert.ok(version === "stable" || version === "next");
2021-09-11 09:55:46 +08:00
2026-02-05 22:29:59 +08:00
let packagesDirectory;
const versionData = { name: version };
const libDirectory = path.join(WEBSITE_DIR, `static/lib/${version}/`);
if (version === "stable") {
packagesDirectory = NODE_MODULES_DIR;
const { version: prettierVersion } = await import(
url.pathToFileURL(path.join(packagesDirectory, "prettier/standalone.mjs"))
);
versionData.version = prettierVersion;
} else if (version === "next") {
2026-02-05 22:29:59 +08:00
packagesDirectory = DIST_DIR;
versionData.gitTree = await getGitTreeInformation();
if (IS_PULL_REQUEST) {
versionData.pr = process.env.REVIEW_ID;
}
2021-09-11 09:55:46 +08:00
}
2025-06-08 16:52:42 +08:00
const pluginFiles = [];
2025-06-08 16:52:42 +08:00
// Builtin plugins
for (const fileName of await fastGlob(["plugins/*.mjs"], {
2026-02-05 22:29:59 +08:00
cwd: path.join(packagesDirectory, "prettier"),
2025-06-08 16:52:42 +08:00
})) {
pluginFiles.push(`prettier/${fileName}`);
}
2025-05-29 15:30:45 +08:00
// TODO: Support stable version
2025-06-08 16:52:42 +08:00
// External plugins
2026-02-05 22:29:59 +08:00
if (version === "next") {
2025-06-08 16:52:42 +08:00
for (const pluginName of ["plugin-hermes"]) {
pluginFiles.push(`${pluginName}/index.mjs`);
}
for (const pluginName of ["plugin-oxc"]) {
pluginFiles.push(`${pluginName}/index.browser.mjs`);
}
2025-05-29 15:30:45 +08:00
}
2023-03-28 08:55:54 +08:00
const packageManifest = {
2026-02-05 22:29:59 +08:00
version: versionData,
2025-06-08 16:52:42 +08:00
prettier: {
file: "prettier/standalone.mjs",
},
plugins: await Promise.all(
pluginFiles.map(async (file) => {
const plugin = { file };
const pluginModule = await import(
2026-02-05 22:29:59 +08:00
url.pathToFileURL(path.join(packagesDirectory, file))
);
for (const property of ["languages", "options", "defaultOptions"]) {
const value = pluginModule[property];
if (value !== undefined) {
plugin[property] = value;
}
}
for (const property of ["parsers", "printers"]) {
const value = pluginModule[property];
if (value !== undefined) {
plugin[property] = Object.keys(value);
}
}
return plugin;
}),
),
2023-03-28 08:55:54 +08:00
};
2021-09-11 09:55:46 +08:00
2025-02-25 08:29:09 +08:00
await Promise.all([
2025-06-08 16:52:42 +08:00
...[packageManifest.prettier, ...packageManifest.plugins].map(({ file }) =>
copyFile(
2026-02-05 22:29:59 +08:00
path.join(packagesDirectory, file),
path.join(libDirectory, file),
2025-06-08 16:52:42 +08:00
),
),
2025-02-25 08:29:09 +08:00
writeScript(
2026-02-05 22:29:59 +08:00
path.join(libDirectory, "package-manifest.mjs"),
2025-06-08 16:52:42 +08:00
`export default ${serialize(packageManifest, { space: 2 })};`,
2025-02-25 08:29:09 +08:00
),
]);
2021-09-11 09:55:46 +08:00
}
2026-02-05 22:29:59 +08:00
async function getGitTreeInformation() {
const { stdout: branch } = await spawn("git", [
"rev-parse",
"--abbrev-ref",
"HEAD",
]);
const { stdout: diff } = await spawn("git", ["diff", "--name-only"]);
if (diff) {
return { branch };
}
const { stdout: commit } = await spawn("git", [
"rev-parse",
"--short",
"HEAD",
]);
return { branch, commit };
}
2021-09-11 09:55:46 +08:00
2026-02-05 22:29:59 +08:00
console.log("Building prettier...");
await runYarn("build", ["--clean", "--playground"], { cwd: PROJECT_ROOT });
console.log("Preparing files for playground (stable)...");
await buildPlaygroundFiles("stable");
console.log("Preparing files for playground (next)...");
await buildPlaygroundFiles("next");
2021-09-11 09:55:46 +08:00
// --- Site ---
console.log("Installing website dependencies...");
await runYarn("install", [], { cwd: WEBSITE_DIR });
2021-09-11 09:55:46 +08:00
console.log("Building website...");
await runYarn("build", [], { cwd: WEBSITE_DIR });