2021-09-11 09:55:46 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
import fs from "node:fs/promises";
|
2023-12-17 19:34:12 +00:00
|
|
|
import path from "node:path";
|
2022-10-06 15:20:19 +08:00
|
|
|
import url from "node:url";
|
2025-02-25 06:27:02 +08:00
|
|
|
import esbuild from "esbuild";
|
2023-12-17 19:34:12 +00:00
|
|
|
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,
|
2023-12-17 19:34:12 +00:00
|
|
|
DIST_DIR,
|
|
|
|
|
PROJECT_ROOT,
|
|
|
|
|
WEBSITE_DIR,
|
2021-09-11 09:55:46 +08:00
|
|
|
writeFile,
|
2023-12-17 19:34:12 +00:00
|
|
|
writeJson,
|
2023-03-28 16:18:53 +08:00
|
|
|
} from "./utils/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 });
|
2021-09-11 09:55:46 +08:00
|
|
|
const IS_PULL_REQUEST = process.env.PULL_REQUEST === "true";
|
2025-06-08 16:52:42 +08:00
|
|
|
const PACKAGES_DIRECTORY = IS_PULL_REQUEST
|
|
|
|
|
? DIST_DIR
|
|
|
|
|
: url.fileURLToPath(new URL("../node_modules", import.meta.url));
|
|
|
|
|
const PLAYGROUND_LIB_DIRECTORY = path.join(WEBSITE_DIR, "static/lib");
|
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,
|
|
|
|
|
});
|
2025-06-08 16:52:42 +08:00
|
|
|
await writeFile(path.join(PLAYGROUND_LIB_DIRECTORY, file), minified.trim());
|
2025-02-25 08:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-11 09:55:46 +08:00
|
|
|
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 {
|
2025-05-29 15:30:45 +08:00
|
|
|
await runYarn("build", ["--clean", "--playground"], {
|
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() {
|
2025-06-08 16:52:42 +08:00
|
|
|
const pluginFiles = [];
|
2022-10-06 15:20:19 +08:00
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
// Builtin plugins
|
|
|
|
|
for (const fileName of await fastGlob(["plugins/*.mjs"], {
|
|
|
|
|
cwd: path.join(PACKAGES_DIRECTORY, "prettier"),
|
|
|
|
|
})) {
|
|
|
|
|
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
|
2025-05-29 15:30:45 +08:00
|
|
|
if (IS_PULL_REQUEST) {
|
2025-06-08 16:52:42 +08:00
|
|
|
for (const pluginName of ["plugin-hermes"]) {
|
|
|
|
|
pluginFiles.push(`${pluginName}/index.mjs`);
|
|
|
|
|
}
|
2025-05-29 15:30:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-28 08:55:54 +08:00
|
|
|
const packageManifest = {
|
2025-06-08 16:52:42 +08:00
|
|
|
prettier: {
|
|
|
|
|
file: "prettier/standalone.mjs",
|
|
|
|
|
},
|
2023-03-28 08:55:54 +08:00
|
|
|
};
|
2021-09-11 09:55:46 +08:00
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
packageManifest.plugins = await Promise.all(
|
|
|
|
|
pluginFiles.map(async (file) => {
|
|
|
|
|
const plugin = { file };
|
2021-09-11 09:55:46 +08:00
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
const pluginModule = await import(
|
|
|
|
|
url.pathToFileURL(path.join(PACKAGES_DIRECTORY, file))
|
|
|
|
|
);
|
2025-02-25 06:27:02 +08:00
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
for (const property of ["languages", "options", "defaultOptions"]) {
|
|
|
|
|
const value = pluginModule[property];
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
plugin[property] = value;
|
|
|
|
|
}
|
2025-02-25 06:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
for (const property of ["parsers", "printers"]) {
|
|
|
|
|
const value = pluginModule[property];
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
plugin[property] = Object.keys(value);
|
|
|
|
|
}
|
2025-02-25 06:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-08 16:52:42 +08:00
|
|
|
return plugin;
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-02-25 06:27:02 +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(
|
|
|
|
|
path.join(PACKAGES_DIRECTORY, file),
|
|
|
|
|
path.join(PLAYGROUND_LIB_DIRECTORY, file),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-02-25 08:29:09 +08:00
|
|
|
writeScript(
|
2025-06-08 16:52:42 +08:00
|
|
|
"package-manifest.mjs",
|
|
|
|
|
`export default ${serialize(packageManifest, { space: 2 })};`,
|
2025-02-25 08:29:09 +08:00
|
|
|
),
|
|
|
|
|
]);
|
2021-09-11 09:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
2022-04-03 01:42:20 +08:00
|
|
|
if (IS_PULL_REQUEST) {
|
|
|
|
|
console.log("Building prettier...");
|
|
|
|
|
await buildPrettier();
|
|
|
|
|
}
|
2021-09-11 09:55:46 +08:00
|
|
|
|
2022-04-03 01:42:20 +08:00
|
|
|
console.log("Preparing files for playground...");
|
|
|
|
|
await buildPlaygroundFiles();
|
2021-09-11 09:55:46 +08:00
|
|
|
|
2022-04-03 01:42:20 +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...");
|
2024-06-11 09:18:16 +08:00
|
|
|
process.env.PRETTIER_VERSION = `999.999.999-pr.${process.env.REVIEW_ID}`;
|
2023-04-27 04:26:06 +02:00
|
|
|
await runYarn("update-stable-docs", [], { cwd: WEBSITE_DIR });
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 01:42:20 +08:00
|
|
|
console.log("Building website...");
|
|
|
|
|
await runYarn("build", [], { cwd: WEBSITE_DIR });
|