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";
|
|
|
|
|
const PRETTIER_DIR = IS_PULL_REQUEST
|
2025-04-09 08:38:46 +08:00
|
|
|
? path.join(DIST_DIR, "prettier")
|
2022-10-06 15:20:19 +08:00
|
|
|
: 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");
|
|
|
|
|
|
2025-02-25 08:29:09 +08:00
|
|
|
async function writeScript(file, code) {
|
|
|
|
|
const { code: minified } = await esbuild.transform(code, {
|
|
|
|
|
loader: "js",
|
|
|
|
|
minify: true,
|
|
|
|
|
});
|
|
|
|
|
await writeFile(path.join(PLAYGROUND_PRETTIER_DIR, file), minified.trim());
|
|
|
|
|
}
|
|
|
|
|
|
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-27 09:35:31 +08:00
|
|
|
await runYarn("build", ["--package=prettier", "--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-02-25 08:29:09 +08:00
|
|
|
const patterns = ["standalone.mjs", "plugins/*.mjs"];
|
2022-10-06 15:20:19 +08:00
|
|
|
|
|
|
|
|
const files = await fastGlob(patterns, {
|
2021-09-11 09:55:46 +08:00
|
|
|
cwd: PRETTIER_DIR,
|
|
|
|
|
});
|
2022-10-06 15:20:19 +08:00
|
|
|
|
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
|
|
|
|
2025-02-25 08:29:09 +08:00
|
|
|
if (fileName === "standalone.mjs") {
|
2021-09-11 09:55:46 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 22:11:19 +08:00
|
|
|
const pluginModule = await import(url.pathToFileURL(dist));
|
2025-02-25 06:27:02 +08:00
|
|
|
|
|
|
|
|
const plugin = {
|
|
|
|
|
name: path.basename(fileName, ".js"),
|
2023-03-28 08:55:54 +08:00
|
|
|
file: fileName,
|
2025-02-25 06:27:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
packageManifest.builtinPlugins.push(plugin);
|
2021-09-11 09:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 08:29:09 +08:00
|
|
|
const serialized = serialize(packageManifest, { space: 2 });
|
2025-02-25 06:27:02 +08:00
|
|
|
|
2025-02-25 08:29:09 +08:00
|
|
|
await Promise.all([
|
|
|
|
|
writeScript("package-manifest.mjs", `export default ${serialized};`),
|
|
|
|
|
writeScript(
|
|
|
|
|
"package-manifest.js",
|
|
|
|
|
/* Indent */ `
|
|
|
|
|
"use strict";
|
2025-02-25 06:27:02 +08:00
|
|
|
|
2025-02-25 08:29:09 +08:00
|
|
|
self.prettierPackageManifest = ${serialized};
|
|
|
|
|
`,
|
|
|
|
|
),
|
|
|
|
|
]);
|
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 });
|