prettier/scripts/build/build.js

121 lines
2.9 KiB
JavaScript
Raw Normal View History

"use strict";
2018-05-29 20:59:57 +03:00
const chalk = require("chalk");
2018-06-18 21:16:40 +03:00
const execa = require("execa");
const minimist = require("minimist");
const path = require("path");
2018-05-29 20:59:57 +03:00
const stringWidth = require("string-width");
2018-06-18 21:16:40 +03:00
2018-05-29 20:59:57 +03:00
const bundler = require("./bundler");
const bundleConfigs = require("./config");
2018-05-24 21:30:45 +03:00
const util = require("./util");
2018-06-18 21:16:40 +03:00
const Cache = require("./cache");
2018-05-24 21:30:45 +03:00
2018-05-29 20:59:57 +03:00
// Errors in promises should be fatal.
const loggedErrors = new Set();
process.on("unhandledRejection", err => {
2018-06-05 22:37:52 +03:00
// No need to print it twice.
if (!loggedErrors.has(err)) {
console.error(err);
2018-05-24 21:30:45 +03:00
}
2018-06-05 22:37:52 +03:00
process.exit(1);
2018-05-29 20:59:57 +03:00
});
2018-05-24 21:30:45 +03:00
2018-06-18 21:16:40 +03:00
const CACHED = chalk.bgYellow.black(" CACHED ");
const OK = chalk.bgGreen.black(" DONE ");
const FAIL = chalk.bgRed.black(" FAIL ");
2018-05-24 21:30:45 +03:00
2018-05-29 20:59:57 +03:00
function fitTerminal(input) {
2018-06-14 17:15:04 +03:00
const columns = Math.min(process.stdout.columns || 40, 80);
2018-05-29 20:59:57 +03:00
const WIDTH = columns - stringWidth(OK) + 1;
if (input.length < WIDTH) {
input += Array(WIDTH - input.length).join(chalk.dim("."));
2018-05-24 21:30:45 +03:00
}
2018-05-29 20:59:57 +03:00
return input;
}
2018-06-18 21:16:40 +03:00
async function createBundle(bundleConfig, cache) {
2018-05-29 20:59:57 +03:00
const { output } = bundleConfig;
process.stdout.write(fitTerminal(output));
2018-06-18 21:16:40 +03:00
return bundler(bundleConfig, cache)
.catch(error => {
console.log(FAIL + "\n");
handleError(error);
})
.then(result => {
if (result.cached) {
console.log(CACHED);
} else {
console.log(OK);
}
});
2018-05-24 21:30:45 +03:00
}
2018-05-29 20:59:57 +03:00
function handleError(error) {
loggedErrors.add(error);
console.error(error);
throw error;
2018-05-24 21:30:45 +03:00
}
2018-06-18 21: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),
path.join(".cache", "files")
]);
}
} catch (err) {
// Don't fail the build
}
}
2018-05-29 20:59:57 +03:00
async function preparePackage() {
2018-05-24 21:30:45 +03:00
const pkg = await util.readJson("package.json");
pkg.bin = "./bin-prettier.js";
pkg.engines.node = ">=4";
2018-05-24 21:30:45 +03:00
delete pkg.dependencies;
delete pkg.devDependencies;
pkg.scripts = {
prepublishOnly:
"node -e \"assert.equal(require('.').version, require('..').version)\""
2018-05-24 21:30:45 +03:00
};
pkg.files = ["*.js"];
2018-05-24 21:30:45 +03:00
await util.writeJson("dist/package.json", pkg);
2018-05-29 20:59:57 +03:00
await util.copyFile("./README.md", "./dist/README.md");
2018-09-19 15:43:26 +03:00
await util.copyFile("./LICENSE", "./dist/LICENSE");
2018-05-24 21:30:45 +03:00
}
2018-06-18 21: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/", "v16");
2018-06-18 21:16:40 +03:00
await bundleCache.load();
2018-05-24 21:30:45 +03:00
2018-05-29 20:59:57 +03:00
console.log(chalk.inverse(" Building packages "));
for (const bundleConfig of bundleConfigs) {
2018-06-18 21:16:40 +03:00
await createBundle(bundleConfig, bundleCache);
}
2018-05-24 21:30:45 +03:00
2018-06-18 21:16:40 +03:00
await bundleCache.save();
await cacheFiles();
2018-05-29 20:59:57 +03:00
await preparePackage();
}
2018-06-18 21:16:40 +03:00
run(
minimist(process.argv.slice(2), {
boolean: ["purge-cache"]
})
);