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

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

137 lines
3.6 KiB
JavaScript
Raw Normal View History

"use strict";
2020-06-26 20:30:21 +08:00
const path = require("path");
const fs = require("fs");
2018-05-29 14:59:57 -03:00
const chalk = require("chalk");
2018-06-18 15:16:40 -03:00
const execa = require("execa");
const minimist = require("minimist");
2018-05-29 14:59:57 -03:00
const stringWidth = require("string-width");
2018-06-18 15:16:40 -03:00
2018-05-29 14:59:57 -03:00
const bundler = require("./bundler");
const bundleConfigs = require("./config");
2018-05-24 15:30:45 -03:00
const util = require("./util");
2018-06-18 15:16:40 -03:00
const Cache = require("./cache");
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
});
const CACHE_VERSION = "v32"; // This need update when updating build scripts
2018-06-18 15:16:40 -03:00
const CACHED = chalk.bgYellow.black(" CACHED ");
const OK = chalk.bgGreen.black(" DONE ");
const FAIL = chalk.bgRed.black(" FAIL ");
2018-05-29 14:59:57 -03:00
function fitTerminal(input) {
2018-06-14 11:15:04 -03:00
const columns = Math.min(process.stdout.columns || 40, 80);
2018-05-29 14:59:57 -03:00
const WIDTH = columns - stringWidth(OK) + 1;
if (input.length < WIDTH) {
2020-01-19 06:48:04 +08:00
input += chalk.dim(".").repeat(WIDTH - input.length - 1);
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
return input;
}
2018-06-18 15:16:40 -03:00
async function createBundle(bundleConfig, cache) {
const { output, target } = bundleConfig;
2018-05-29 14:59:57 -03:00
process.stdout.write(fitTerminal(output));
try {
const { cached } = await bundler(bundleConfig, cache);
if (cached) {
console.log(CACHED);
return;
}
// 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") {
const file = path.join("dist", output);
const content = fs.readFileSync(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
}
}
console.log(OK);
} catch (error) {
console.log(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 (err) {
// Don't fail the build
}
}
2018-05-29 14:59:57 -03:00
async function preparePackage() {
2018-05-24 15:30:45 -03:00
const pkg = await util.readJson("package.json");
pkg.bin = "./bin-prettier.js";
delete pkg.dependencies;
delete pkg.devDependencies;
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"];
2018-05-24 15:30:45 -03:00
await util.writeJson("dist/package.json", pkg);
2018-05-29 14:59:57 -03:00
await util.copyFile("./README.md", "./dist/README.md");
2018-09-19 20:43:26 +08:00
await util.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-09-17 01:08:44 +09:00
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) {
2018-06-18 15:16:40 -03:00
await createBundle(bundleConfig, bundleCache);
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
2018-05-29 14:59:57 -03:00
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"],
})
);