Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor website build script (#11488)
- Loading branch information
Showing
10 changed files
with
137 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[build] | ||
command = "yarn build:website" | ||
|
||
[build.environment] | ||
NODE_VERSION = "14" | ||
YARN_VERSION = "1.22.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env node | ||
|
||
import path from "node:path"; | ||
import fs from "node:fs/promises"; | ||
import globby from "globby"; | ||
import prettier from "prettier"; | ||
import createEsmUtils from "esm-utils"; | ||
import execa from "execa"; | ||
import { | ||
PROJECT_ROOT, | ||
DIST_DIR, | ||
WEBSITE_DIR, | ||
readJson, | ||
writeJson, | ||
copyFile, | ||
writeFile, | ||
} from "./utils/index.mjs"; | ||
|
||
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 | ||
: path.dirname(require.resolve("prettier")); | ||
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 content = await fs.readFile(packageJsonFile); | ||
const packageJson = await readJson(packageJsonFile); | ||
await writeJson(packageJsonFile, { | ||
...packageJson, | ||
version: `999.999.999-pr.${process.env.REVIEW_ID}`, | ||
}); | ||
|
||
try { | ||
await runYarn("build", ["--playground"], { cwd: PROJECT_ROOT }); | ||
} finally { | ||
// restore | ||
await writeFile(packageJsonFile, content); | ||
} | ||
} | ||
|
||
async function buildPlaygroundFiles() { | ||
const files = await globby(["standalone.js", "parser-*.js"], { | ||
cwd: PRETTIER_DIR, | ||
}); | ||
const parsers = {}; | ||
for (const fileName of files) { | ||
const file = path.join(PRETTIER_DIR, fileName); | ||
await copyFile(file, path.join(PLAYGROUND_PRETTIER_DIR, fileName)); | ||
|
||
if (fileName === "standalone.js") { | ||
continue; | ||
} | ||
|
||
const plugin = require(file); | ||
// We add plugins to the global `prettierPlugins` object | ||
// the name after `parser-` is used as property | ||
// For example to get parsers in `parser-babel.js` via `prettierPlugins.babel` | ||
// See `scripts/build/config.mjs` | ||
const property = fileName.replace(/\.js$/, "").split("-")[1]; | ||
parsers[fileName] = { | ||
property, | ||
parsers: Object.keys(plugin.parsers), | ||
}; | ||
} | ||
|
||
await writeFile( | ||
path.join(PLAYGROUND_PRETTIER_DIR, "parsers-location.js"), | ||
prettier.format( | ||
` | ||
"use strict"; | ||
const parsersLocation = ${JSON.stringify(parsers)}; | ||
`, | ||
{ parser: "babel" } | ||
) | ||
); | ||
} | ||
|
||
(async () => { | ||
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 }); | ||
|
||
console.log("Building website..."); | ||
await runYarn("build", [], { cwd: WEBSITE_DIR }); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters