2021-05-15 20:09:52 +08:00
|
|
|
#!/usr/bin/env node
|
2017-06-28 22:34:59 +02:00
|
|
|
|
2021-05-15 20:09:52 +08:00
|
|
|
import fs from "node:fs/promises";
|
2023-12-17 19:34:12 +00:00
|
|
|
import path from "node:path";
|
2021-05-15 20:09:52 +08:00
|
|
|
import readline from "node:readline";
|
2023-12-17 19:34:12 +00:00
|
|
|
|
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";
|
2023-12-17 19:34:12 +00:00
|
|
|
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";
|
2023-08-23 15:15:25 +08:00
|
|
|
import parseArguments from "./parse-arguments.js";
|
2017-06-28 22:34:59 +02:00
|
|
|
|
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
|
|
|
|
2020-12-16 08:45:29 +09: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);
|
2020-12-16 08:45:29 +09:00
|
|
|
input += chalk.dim(".").repeat(repeatCount) + suffix;
|
2018-05-24 15:30:45 -03:00
|
|
|
}
|
2018-05-29 14:59:57 -03:00
|
|
|
return input;
|
2017-06-28 22:34:59 +02:00
|
|
|
}
|
|
|
|
|
|
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") ||
|
2023-02-23 22:15:18 +09:00
|
|
|
(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)) ||
|
2022-10-06 15:20:19 +08:00
|
|
|
(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;
|
2020-06-12 06:42:42 +05:30
|
|
|
try {
|
2022-10-06 15:20:19 +08:00
|
|
|
result = await file.build({
|
2022-09-25 11:50:59 +08:00
|
|
|
file,
|
|
|
|
|
files,
|
|
|
|
|
shouldCollectLicenses,
|
|
|
|
|
cliOptions,
|
2022-10-06 15:20:19 +08:00
|
|
|
});
|
2020-06-12 06:42:42 +05:30
|
|
|
} catch (error) {
|
2020-10-26 09:44:09 +08:00
|
|
|
console.log(status.FAIL + "\n");
|
2022-04-03 01:42:20 +08:00
|
|
|
console.error(error);
|
|
|
|
|
throw error;
|
2020-06-12 06:42:42 +05:30
|
|
|
}
|
2022-09-25 11:50:59 +08:00
|
|
|
|
2022-10-06 15:20:19 +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
|
|
|
}
|
|
|
|
|
|
2023-08-23 15:15:25 +08: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 =
|
2023-08-23 15:15:25 +08:00
|
|
|
!cliOptions.playground &&
|
|
|
|
|
!cliOptions.files &&
|
|
|
|
|
typeof cliOptions.minify !== "boolean";
|
2021-12-02 19:47:14 +08:00
|
|
|
|
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 });
|
2021-12-02 19:47:14 +08:00
|
|
|
}
|
2018-05-24 15:30:45 -03:00
|
|
|
}
|
|
|
|
|
|
2023-08-23 15:15:25 +08:00
|
|
|
await run();
|