prettier/scripts/build/build.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

"use strict";
2018-05-29 20:59:57 +03:00
const chalk = require("chalk");
const stringWidth = require("string-width");
const bundler = require("./bundler");
const bundleConfigs = require("./config");
2018-05-24 21:30:45 +03:00
const util = require("./util");
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-05-29 20:59:57 +03:00
const OK = chalk.reset.inverse.bold.green(" DONE ");
const FAIL = chalk.reset.inverse.bold.red(" FAIL ");
2018-05-24 21:30:45 +03:00
2018-05-29 20:59:57 +03:00
function fitTerminal(input) {
2018-06-05 22:37:52 +03:00
const columns = Math.min(process.stdout.columns, 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-05-29 20:59:57 +03:00
async function createBundle(bundleConfig) {
const { output } = bundleConfig;
process.stdout.write(fitTerminal(output));
2018-05-29 20:59:57 +03:00
try {
await bundler(bundleConfig, output);
} catch (error) {
process.stdout.write(`${FAIL}\n\n`);
handleError(error);
2018-05-24 21:30:45 +03:00
}
2018-05-29 20:59:57 +03:00
process.stdout.write(`${OK}\n`);
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-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-05-24 21:30:45 +03:00
}
2018-05-24 21:30:45 +03:00
async function run() {
await util.asyncRimRaf("dist");
2018-05-29 20:59:57 +03:00
console.log(chalk.inverse(" Building packages "));
for (const bundleConfig of bundleConfigs) {
await createBundle(bundleConfig);
}
2018-05-24 21:30:45 +03:00
2018-05-29 20:59:57 +03:00
await preparePackage();
}
2018-05-24 21:30:45 +03:00
run();