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.

195 lines
5.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";
2022-02-07 12:00:43 +08:00
import createEsmUtils from "esm-utils";
import styleText from "node-style-text";
import prettyBytes from "pretty-bytes";
2025-05-27 09:35:31 +08:00
import prettyMilliseconds from "pretty-ms";
2025-12-01 07:25:25 +08:00
import { DIST_DIR } from "../utilities/index.js";
2025-05-27 09:35:31 +08:00
import packageConfigs 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] = styleText[color].black(padStatusText(text));
2020-10-26 09:44:09 +08:00
}
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 += styleText.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);
};
2025-04-09 08:38:46 +08:00
async function buildFile({ packageConfig, file, cliOptions, results }) {
const { distDirectory } = packageConfig;
2022-09-25 11:50:59 +08:00
let displayName = file.output.file;
if (
(file.platform === "universal" && file.output.format !== "esm") ||
(file.output.file.startsWith("index.") && file.output.format !== "esm") ||
file.output.file.endsWith(".browser.mjs") ||
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)) ||
2025-05-29 15:30:45 +08:00
(cliOptions.playground && !file.playground)
2022-09-25 11:50:59 +08:00
) {
console.log(status.SKIPPED);
return;
}
let result;
try {
2025-04-09 08:38:46 +08:00
result = await file.build({ packageConfig, file, cliOptions, results });
} 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
2024-07-02 10:01:15 +08:00
result ??= {};
2022-09-25 11:50:59 +08:00
if (result.skipped) {
console.log(status.SKIPPED);
return;
}
2024-07-02 10:01:15 +08:00
const outputFile = cliOptions.saveAs ?? file.output.file;
2022-09-25 11:50:59 +08:00
const sizeMessages = [];
if (cliOptions.printSize) {
2025-04-09 08:38:46 +08:00
const { size } = await fs.stat(path.join(distDirectory, outputFile));
2022-09-25 11:50:59 +08:00
sizeMessages.push(prettyBytes(size));
}
if (cliOptions.compareSize) {
// TODO: Use `import.meta.resolve` when Node.js support
const stablePrettierDirectory = path.dirname(require.resolve("prettier"));
2024-07-02 10:01:15 +08:00
const stableVersionFile = path.join(stablePrettierDirectory, outputFile);
2022-09-25 11:50:59 +08:00
let stableSize;
try {
({ size: stableSize } = await fs.stat(stableVersionFile));
} catch {
// No op
}
if (stableSize) {
2025-04-09 08:38:46 +08:00
const { size } = await fs.stat(path.join(distDirectory, outputFile));
2022-09-25 11:50:59 +08:00
const sizeDiff = size - stableSize;
const message = styleText[sizeDiff > 0 ? "yellow" : "green"](
2022-09-25 11:50:59 +08:00
prettyBytes(sizeDiff),
);
sizeMessages.push(`${message}`);
} else {
sizeMessages.push(styleText.blue("[NEW FILE]"));
2022-09-25 11:50:59 +08:00
}
}
if (sizeMessages.length > 0) {
// Clear previous line
clear();
process.stdout.write(
fitTerminal(displayName, `${sizeMessages.join(", ")} `),
);
}
console.log(status.DONE);
2024-07-02 10:01:15 +08:00
return result;
2018-05-24 15:30:45 -03:00
}
async function run() {
const cliOptions = parseArguments();
2022-01-27 09:37:55 +08:00
2025-05-27 09:35:31 +08:00
let packagesToBuild = [];
if (cliOptions.packages) {
for (const packageName of cliOptions.packages) {
const packageConfig = packageConfigs.find(
(packageConfig) => packageConfig.packageName === packageName,
);
if (!packageConfig) {
throw new Error(`Unknown package "${packageName}"`);
}
packagesToBuild.push(packageConfig);
}
} else {
packagesToBuild = packageConfigs;
}
// TODO: Clear package dist directory instead
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`);
}
}
}
2025-05-27 09:35:31 +08:00
for (const [index, packageConfig] of packagesToBuild.entries()) {
if (index > 0) {
console.log();
}
2021-09-09 12:23:53 +08:00
2025-05-27 09:35:31 +08:00
console.log(
2025-10-14 19:30:07 +08:00
styleText.inverse`[${index + 1}/${packagesToBuild.length}] Building package '${packageConfig.packageName}'`,
2025-05-27 09:35:31 +08:00
);
const startTime = performance.now();
const results = [];
for (const file of packageConfig.files) {
const result = await buildFile({
packageConfig,
file,
cliOptions,
results,
});
results.push(result);
}
console.log(
`Build package '${packageConfig.packageName}' success in ${prettyMilliseconds(performance.now() - startTime)}`,
);
}
2018-05-24 15:30:45 -03:00
}
await run();