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.

112 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-09-11 09:55:46 +08:00
#!/usr/bin/env node
import path from "node:path";
import fs from "node:fs/promises";
import url from "node:url";
2022-02-05 16:24:10 +03:00
import fastGlob from "fast-glob";
2021-09-11 09:55:46 +08:00
import createEsmUtils from "esm-utils";
2021-11-29 10:07:11 +08:00
import { execa } from "execa";
import { format } from "../src/index.js";
2021-09-11 09:55:46 +08:00
import {
PROJECT_ROOT,
DIST_DIR,
WEBSITE_DIR,
writeJson,
copyFile,
writeFile,
2023-03-28 16:18:53 +08:00
} from "./utils/index.js";
2021-09-11 09:55:46 +08:00
const { require } = createEsmUtils(import.meta);
const runYarn = (command, args, options) =>
execa("yarn", [command, ...args], {
stdout: "inherit",
stderr: "inherit",
...options,
});
const IS_PULL_REQUEST = process.env.PULL_REQUEST === "true";
const PRETTIER_DIR = IS_PULL_REQUEST
? DIST_DIR
: url.fileURLToPath(new URL("../node_modules/prettier", import.meta.url));
2021-09-11 09:55:46 +08:00
const PLAYGROUND_PRETTIER_DIR = path.join(WEBSITE_DIR, "static/lib");
async function buildPrettier() {
// --- Build prettier for PR ---
const packageJsonFile = path.join(PROJECT_ROOT, "package.json");
2022-01-26 20:33:15 +08:00
const packageJsonContent = await fs.readFile(packageJsonFile);
const packageJson = JSON.parse(packageJsonContent);
2021-09-11 09:55:46 +08:00
await writeJson(packageJsonFile, {
...packageJson,
version: `999.999.999-pr.${process.env.REVIEW_ID}`,
});
try {
2022-02-09 09:31:03 +08:00
await runYarn("build", ["--playground", "--clean"], {
2022-01-26 20:33:15 +08:00
cwd: PROJECT_ROOT,
});
2021-09-11 09:55:46 +08:00
} finally {
// restore
2022-01-26 20:33:15 +08:00
await writeFile(packageJsonFile, packageJsonContent);
2021-09-11 09:55:46 +08:00
}
}
async function buildPlaygroundFiles() {
2023-07-11 14:50:00 +08:00
const patterns = ["standalone.js", "plugins/*.js"];
const files = await fastGlob(patterns, {
2021-09-11 09:55:46 +08:00
cwd: PRETTIER_DIR,
});
2023-03-28 08:55:54 +08:00
const packageManifest = {
builtinPlugins: [],
};
2021-09-11 09:55:46 +08:00
for (const fileName of files) {
const file = path.join(PRETTIER_DIR, fileName);
2022-02-12 15:39:16 +08:00
const dist = path.join(PLAYGROUND_PRETTIER_DIR, fileName);
await copyFile(file, dist);
2021-09-11 09:55:46 +08:00
if (fileName === "standalone.js") {
continue;
}
const pluginModule = require(dist);
const plugin = pluginModule.default ?? pluginModule;
2023-03-28 08:55:54 +08:00
const { parsers = {}, printers = {} } = plugin;
packageManifest.builtinPlugins.push({
file: fileName,
parsers: Object.keys(parsers),
printers: Object.keys(printers),
});
2021-09-11 09:55:46 +08:00
}
await writeFile(
2023-03-28 08:55:54 +08:00
path.join(PLAYGROUND_PRETTIER_DIR, "package-manifest.js"),
await format(
2023-03-28 08:55:54 +08:00
/* Indent */ `
2021-09-11 09:55:46 +08:00
"use strict";
2023-03-28 08:55:54 +08:00
const prettierPackageManifest = ${JSON.stringify(packageManifest)};
2021-09-11 09:55:46 +08:00
`,
{ parser: "meriyah" },
2021-09-11 09:55:46 +08:00
),
);
}
if (IS_PULL_REQUEST) {
console.log("Building prettier...");
await buildPrettier();
}
2021-09-11 09:55:46 +08:00
console.log("Preparing files for playground...");
await buildPlaygroundFiles();
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
2023-04-27 04:26:06 +02:00
if (IS_PULL_REQUEST) {
console.log("Synchronizing docs...");
await runYarn("update-stable-docs", [], { cwd: WEBSITE_DIR });
}
console.log("Building website...");
await runYarn("build", [], { cwd: WEBSITE_DIR });