Files
test-repo/scripts/build/build.mjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

183 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-05-15 20:09:52 +08:00
#!/usr/bin/env node
2021-05-15 20:09:52 +08:00
import path from "node:path";
import fs from "node:fs/promises";
import readline from "node:readline";
import chalk from "chalk";
import execa from "execa";
import minimist from "minimist";
import prettyBytes from "pretty-bytes";
import bundler from "./bundler.mjs";
import bundleConfigs from "./config.mjs";
import * as utils from "./utils.mjs";
import Cache from "./cache.mjs";
2018-05-29 14:59:57 -03:00
// Errors in promises should be fatal.
const loggedErrors = new Set();
process.on("unhandledRejection", (err) => {
2018-06-05 16:37:52 -03:00
// No need to print it twice.
if (!loggedErrors.has(err)) {
console.error(err);
2017-12-29 12:14:31 -05:00
}
2018-06-05 16:37:52 -03:00
process.exit(1);
2018-05-29 14:59:57 -03:00
});
2021-07-21 09:10:34 +08:00
const CACHE_VERSION = "v38"; // This need update when updating build scripts
2020-10-26 09:44:09 +08:00
const statusConfig = [
{ color: "bgYellow", text: "CACHED" },
{ color: "bgGreen", text: "DONE" },
{ color: "bgRed", text: "FAIL" },
{ color: "bgGray", text: "SKIPPED" },
];
const maxLength = Math.max(...statusConfig.map(({ text }) => text.length)) + 2;
const padStatusText = (text) => {
while (text.length < maxLength) {
text = text.length % 2 ? `${text} ` : ` ${text}`;
}
return text;
};
const status = {};
for (const { color, text } of statusConfig) {
status[text] = chalk[color].black(padStatusText(text));
}
2018-05-29 14:59:57 -03:00
function fitTerminal(input, suffix = "") {
2018-06-14 11:15:04 -03:00
const columns = Math.min(process.stdout.columns || 40, 80);
2020-10-26 09:44:09 +08:00
const WIDTH = columns - maxLength + 1;
2018-05-29 14:59:57 -03:00
if (input.length < WIDTH) {
const repeatCount = WIDTH - input.length - 1 - suffix.length;
input += chalk.dim(".").repeat(repeatCount) + suffix;
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
return input;
}
2020-10-26 09:44:09 +08:00
async function createBundle(bundleConfig, cache, options) {
const { output, target, format, type } = bundleConfig;
2018-05-29 14:59:57 -03:00
process.stdout.write(fitTerminal(output));
try {
2020-10-26 09:44:09 +08:00
const { cached, skipped } = await bundler(bundleConfig, cache, options);
if (skipped) {
console.log(status.SKIPPED);
return;
}
if (cached) {
2020-10-26 09:44:09 +08:00
console.log(status.CACHED);
return;
}
const file = path.join("dist", output);
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
if (target === "universal") {
2021-01-09 02:09:40 +09:00
const content = await fs.readFile(file, "utf8");
if (content.includes("\ufffe")) {
throw new Error("Bundled umd file should not have U+FFFE character.");
2018-06-18 15:16:40 -03:00
}
}
if (options["print-size"]) {
// Clear previous line
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
2021-01-09 02:09:40 +09:00
const getSizeText = async (file) =>
prettyBytes((await fs.stat(file)).size);
const sizeTexts = [await getSizeText(file)];
if (
type !== "core" &&
format !== "esm" &&
bundleConfig.bundler !== "webpack" &&
target === "universal"
) {
const esmFile = path.join("dist/esm", output.replace(".js", ".mjs"));
2021-01-09 02:09:40 +09:00
sizeTexts.push(`esm ${await getSizeText(esmFile)}`);
}
2021-01-26 09:46:14 +08:00
process.stdout.write(fitTerminal(output, `${sizeTexts.join(", ")} `));
}
2020-10-26 09:44:09 +08:00
console.log(status.DONE);
} catch (error) {
2020-10-26 09:44:09 +08:00
console.log(status.FAIL + "\n");
handleError(error);
}
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
function handleError(error) {
loggedErrors.add(error);
console.error(error);
throw error;
2018-05-24 15:30:45 -03:00
}
2020-09-17 01:08:44 +09:00
async function cacheFiles(cache) {
2018-06-18 15:16:40 -03:00
// Copy built files to .cache
try {
await execa("rm", ["-rf", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files")]);
2020-09-17 01:08:44 +09:00
await execa("mkdir", ["-p", path.join(".cache", "files", "esm")]);
const manifest = cache.updated;
for (const file of Object.keys(manifest.files)) {
2018-06-18 15:16:40 -03:00
await execa("cp", [
2020-09-17 01:08:44 +09:00
file,
path.join(".cache", file.replace("dist", "files")),
2018-06-18 15:16:40 -03:00
]);
}
} catch {
2018-06-18 15:16:40 -03:00
// Don't fail the build
}
}
2018-05-29 14:59:57 -03:00
async function preparePackage() {
2021-05-15 20:09:52 +08:00
const pkg = await utils.readJson("package.json");
2018-05-24 15:30:45 -03:00
pkg.bin = "./bin-prettier.js";
pkg.engines.node = ">=10.13.0";
2018-05-24 15:30:45 -03:00
delete pkg.dependencies;
delete pkg.devDependencies;
delete pkg.browserslist;
2018-05-24 15:30:45 -03:00
pkg.scripts = {
prepublishOnly:
2018-05-29 00:22:59 -06:00
"node -e \"assert.equal(require('.').version, require('..').version)\"",
2018-05-24 15:30:45 -03:00
};
pkg.files = ["*.js", "esm/*.mjs"];
2021-05-15 20:09:52 +08:00
await utils.writeJson("dist/package.json", pkg);
2018-05-29 14:59:57 -03:00
2021-05-15 20:09:52 +08:00
await utils.copyFile("./README.md", "./dist/README.md");
await utils.copyFile("./LICENSE", "./dist/LICENSE");
2018-05-24 15:30:45 -03:00
}
2018-06-18 15:16:40 -03:00
async function run(params) {
await execa("rm", ["-rf", "dist"]);
await execa("mkdir", ["-p", "dist"]);
2020-10-26 09:44:09 +08:00
if (!params.playground) {
await execa("mkdir", ["-p", "dist/esm"]);
}
2018-06-18 15:16:40 -03:00
if (params["purge-cache"]) {
await execa("rm", ["-rf", ".cache"]);
}
const bundleCache = new Cache(".cache/", CACHE_VERSION);
2018-06-18 15:16:40 -03:00
await bundleCache.load();
2018-05-24 15:30:45 -03:00
2018-05-29 14:59:57 -03:00
console.log(chalk.inverse(" Building packages "));
for (const bundleConfig of bundleConfigs) {
2020-10-26 09:44:09 +08:00
await createBundle(bundleConfig, bundleCache, params);
2018-05-24 15:30:45 -03:00
}
2020-09-17 01:08:44 +09:00
await cacheFiles(bundleCache);
2020-04-24 11:00:06 +08:00
await bundleCache.save();
2018-06-18 15:16:40 -03:00
2020-10-26 09:44:09 +08:00
if (!params.playground) {
await preparePackage();
}
2018-05-24 15:30:45 -03:00
}
2018-06-18 15:16:40 -03:00
run(
minimist(process.argv.slice(2), {
boolean: ["purge-cache", "playground", "print-size"],
2018-06-18 15:16:40 -03:00
})
);