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

120 lines
3.0 KiB
JavaScript
Raw Normal View History

"use strict";
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");
const path = require("path");
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();
2020-03-21 22:07:52 +02:00
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 = "v24"; // 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) {
2018-05-29 14:59:57 -03:00
const { output } = bundleConfig;
process.stdout.write(fitTerminal(output));
2018-06-18 15:16:40 -03:00
return bundler(bundleConfig, cache)
2020-03-21 22:07:52 +02:00
.catch((error) => {
2018-06-18 15:16:40 -03:00
console.log(FAIL + "\n");
handleError(error);
})
2020-03-21 22:07:52 +02:00
.then((result) => {
2018-06-18 15:16:40 -03:00
if (result.cached) {
console.log(CACHED);
} else {
console.log(OK);
}
});
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
}
2018-06-18 15:16:40 -03:00
async function cacheFiles() {
// Copy built files to .cache
try {
await execa("rm", ["-rf", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files")]);
for (const bundleConfig of bundleConfigs) {
await execa("cp", [
path.join("dist", bundleConfig.output),
2020-03-21 22:07:52 +02:00
path.join(".cache", "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:
2020-03-21 22:07:52 +02:00
"node -e \"assert.equal(require('.').version, require('..').version)\"",
2018-05-24 15:30:45 -03:00
};
pkg.files = ["*.js"];
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"]);
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
}
2018-06-18 15:16:40 -03:00
await cacheFiles();
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), {
2020-03-21 22:07:52 +02:00
boolean: ["purge-cache"],
2018-06-18 15:16:40 -03:00
})
);