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.

166 lines
4.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 fs from "node:fs/promises";
import path from "node:path";
2021-05-15 20:09:52 +08:00
import readline from "node:readline";
2021-05-15 20:09:52 +08:00
import chalk from "chalk";
2022-02-07 12:00:43 +08:00
import createEsmUtils from "esm-utils";
import prettyBytes from "pretty-bytes";
2023-03-28 16:18:53 +08:00
import { DIST_DIR } from "../utils/index.js";
import files from "./config.js";
import parseArguments from "./parse-arguments.js";
2022-02-07 12:00:43 +08:00
const { require } = createEsmUtils(import.meta);
2020-10-26 09:44:09 +08:00
const statusConfig = [
{ 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) {
2022-02-07 12:00:43 +08:00
const repeatCount = Math.max(WIDTH - input.length - 1 - suffix.length, 0);
input += chalk.dim(".").repeat(repeatCount) + suffix;
2018-05-24 15:30:45 -03:00
}
2018-05-29 14:59:57 -03:00
return input;
}
2022-01-27 09:37:55 +08:00
const clear = () => {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
};
2022-09-25 11:50:59 +08:00
async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
let displayName = file.output.file;
if (
(file.platform === "universal" && file.output.format !== "esm") ||
(file.output.file.startsWith("index.") && file.output.format !== "esm") ||
file.kind === "types"
2022-09-25 11:50:59 +08:00
) {
displayName = ` ${displayName}`;
}
process.stdout.write(fitTerminal(displayName));
if (
(cliOptions.files && !cliOptions.files.has(file.output.file)) ||
(cliOptions.playground &&
(file.output.format !== "umd" || file.output.file === "doc.js"))
2022-09-25 11:50:59 +08:00
) {
console.log(status.SKIPPED);
return;
}
let result;
try {
result = await file.build({
2022-09-25 11:50:59 +08:00
file,
files,
shouldCollectLicenses,
cliOptions,
});
} catch (error) {
2020-10-26 09:44:09 +08:00
console.log(status.FAIL + "\n");
console.error(error);
throw error;
}
2022-09-25 11:50:59 +08:00
result ??= {
file: cliOptions.saveAs ?? file.output.file,
};
2022-09-25 11:50:59 +08:00
if (result.skipped) {
console.log(status.SKIPPED);
return;
}
const sizeMessages = [];
if (cliOptions.printSize) {
const { size } = await fs.stat(path.join(DIST_DIR, result.file));
sizeMessages.push(prettyBytes(size));
}
if (cliOptions.compareSize) {
// TODO: Use `import.meta.resolve` when Node.js support
const stablePrettierDirectory = path.dirname(require.resolve("prettier"));
const stableVersionFile = path.join(stablePrettierDirectory, result.file);
let stableSize;
try {
({ size: stableSize } = await fs.stat(stableVersionFile));
} catch {
// No op
}
if (stableSize) {
const { size } = await fs.stat(path.join(DIST_DIR, result.file));
const sizeDiff = size - stableSize;
const message = chalk[sizeDiff > 0 ? "yellow" : "green"](
prettyBytes(sizeDiff),
);
sizeMessages.push(`${message}`);
} else {
sizeMessages.push(chalk.blue("[NEW FILE]"));
}
}
if (sizeMessages.length > 0) {
// Clear previous line
clear();
process.stdout.write(
fitTerminal(displayName, `${sizeMessages.join(", ")} `),
);
}
console.log(status.DONE);
2018-05-24 15:30:45 -03:00
}
async function run() {
const cliOptions = parseArguments();
2022-01-27 09:37:55 +08:00
2022-09-25 11:50:59 +08:00
if (cliOptions.clean) {
2022-02-02 10:23:51 +08:00
let stat;
try {
stat = await fs.stat(DIST_DIR);
} catch {
// No op
}
if (stat) {
if (stat.isDirectory()) {
await fs.rm(DIST_DIR, { recursive: true, force: true });
} else {
throw new Error(`"${DIST_DIR}" is not a directory`);
}
}
}
2022-09-25 11:50:59 +08:00
const shouldCollectLicenses =
!cliOptions.playground &&
!cliOptions.files &&
typeof cliOptions.minify !== "boolean";
2018-05-29 14:59:57 -03:00
console.log(chalk.inverse(" Building packages "));
2021-09-09 12:23:53 +08:00
2022-09-25 11:50:59 +08:00
for (const file of files) {
await buildFile({ file, files, shouldCollectLicenses, cliOptions });
}
2018-05-24 15:30:45 -03:00
}
await run();