forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-website.js
More file actions
115 lines (97 loc) · 2.96 KB
/
Copy pathbuild-website.js
File metadata and controls
115 lines (97 loc) · 2.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
import fs from "node:fs/promises";
import path from "node:path";
import url from "node:url";
import createEsmUtils from "esm-utils";
import { execa } from "execa";
import fastGlob from "fast-glob";
import { format } from "../src/index.js";
import {
copyFile,
DIST_DIR,
PROJECT_ROOT,
WEBSITE_DIR,
writeFile,
writeJson,
} from "./utils/index.js";
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));
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");
const packageJsonContent = await fs.readFile(packageJsonFile);
const packageJson = JSON.parse(packageJsonContent);
await writeJson(packageJsonFile, {
...packageJson,
version: `999.999.999-pr.${process.env.REVIEW_ID}`,
});
try {
await runYarn("build", ["--playground", "--clean"], {
cwd: PROJECT_ROOT,
});
} finally {
// restore
await writeFile(packageJsonFile, packageJsonContent);
}
}
async function buildPlaygroundFiles() {
const patterns = ["standalone.js", "plugins/*.js"];
const files = await fastGlob(patterns, {
cwd: PRETTIER_DIR,
});
const packageManifest = {
builtinPlugins: [],
};
for (const fileName of files) {
const file = path.join(PRETTIER_DIR, fileName);
const dist = path.join(PLAYGROUND_PRETTIER_DIR, fileName);
await copyFile(file, dist);
if (fileName === "standalone.js") {
continue;
}
const pluginModule = require(dist);
const plugin = pluginModule.default ?? pluginModule;
const { parsers = {}, printers = {} } = plugin;
packageManifest.builtinPlugins.push({
file: fileName,
parsers: Object.keys(parsers),
printers: Object.keys(printers),
});
}
await writeFile(
path.join(PLAYGROUND_PRETTIER_DIR, "package-manifest.js"),
await format(
/* Indent */ `
"use strict";
const prettierPackageManifest = ${JSON.stringify(packageManifest)};
`,
{ parser: "meriyah" },
),
);
}
if (IS_PULL_REQUEST) {
console.log("Building prettier...");
await buildPrettier();
}
console.log("Preparing files for playground...");
await buildPlaygroundFiles();
// --- Site ---
console.log("Installing website dependencies...");
await runYarn("install", [], { cwd: WEBSITE_DIR });
if (IS_PULL_REQUEST) {
console.log("Synchronizing docs...");
process.env.PRETTIER_VERSION = `999.999.999-pr.${process.env.REVIEW_ID}`;
await runYarn("update-stable-docs", [], { cwd: WEBSITE_DIR });
}
console.log("Building website...");
await runYarn("build", [], { cwd: WEBSITE_DIR });