Files
test-repo/scripts/release/utils.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

156 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-07-26 09:24:44 +08:00
import fs from "node:fs";
2024-01-22 00:02:31 +08:00
import path from "node:path";
2021-07-26 09:24:44 +08:00
import readline from "node:readline";
2024-01-22 00:02:31 +08:00
import url from "node:url";
import spawn from "nano-spawn";
import styleText from "node-style-text";
2021-09-11 16:20:26 +09:00
import outdent from "outdent";
import getFormattedDate from "./get-formatted-date.js";
2018-06-05 16:37:52 -03:00
2021-07-26 09:24:44 +08:00
readline.emitKeypressEvents(process.stdin);
2018-06-05 16:37:52 -03:00
2022-10-14 17:17:47 +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));
2022-10-14 17:17:47 +08:00
}
2018-06-05 16:37:52 -03:00
2022-10-14 17:17:47 +08:00
function fitTerminal(input, suffix = "") {
const columns = Math.min(process.stdout.columns || 40, 80);
const WIDTH = columns - maxLength + 1;
2018-06-05 16:37:52 -03:00
if (input.length < WIDTH) {
2022-10-14 17:17:47 +08:00
const repeatCount = Math.max(WIDTH - input.length - 1 - suffix.length, 0);
input += styleText.dim(".").repeat(repeatCount) + suffix;
2018-06-05 16:37:52 -03:00
}
return input;
}
2022-10-14 17:17:47 +08:00
async function logPromise(name, promiseOrAsyncFunction, shouldSkip = false) {
2018-06-05 16:37:52 -03:00
process.stdout.write(fitTerminal(name));
2022-10-14 17:17:47 +08:00
if (shouldSkip) {
process.stdout.write(`${status.SKIPPED}\n`);
return;
}
2021-04-21 22:15:12 +08:00
try {
2022-10-14 17:17:47 +08:00
const result = await (typeof promiseOrAsyncFunction === "function"
? promiseOrAsyncFunction()
: promiseOrAsyncFunction);
process.stdout.write(`${status.DONE}\n`);
2021-04-21 22:15:12 +08:00
return result;
} catch (error) {
2022-10-14 17:17:47 +08:00
process.stdout.write(`${status.FAIL}\n`);
2021-04-21 22:15:12 +08:00
throw error;
}
2018-06-05 16:37:52 -03:00
}
2021-04-21 22:15:12 +08:00
async function runYarn(args, options) {
args = Array.isArray(args) ? args : [args];
try {
return await spawn("yarn", [...args], options);
2021-04-21 22:15:12 +08:00
} catch (error) {
throw new Error(`\`yarn ${args.join(" ")}\` failed\n${error.stdout}`);
}
}
function runGit(args, options) {
args = Array.isArray(args) ? args : [args];
return spawn("git", args, options);
2018-06-05 16:37:52 -03:00
}
function waitForEnter() {
2022-10-14 17:17:47 +08:00
console.log();
console.log(styleText.gray("Press ENTER to continue."));
2022-10-14 17:17:47 +08:00
2018-06-05 16:37:52 -03:00
process.stdin.setRawMode(true);
return new Promise((resolve, reject) => {
process.stdin.on("keypress", listener);
process.stdin.resume();
function listener(ch, key) {
if (key.name === "return") {
process.stdin.setRawMode(false);
process.stdin.removeListener("keypress", listener);
process.stdin.pause();
resolve();
} else if (key.ctrl && key.name === "c") {
2019-11-17 18:36:21 +08:00
reject(new Error("Process terminated by the user"));
2018-06-05 16:37:52 -03:00
}
}
});
}
function readJson(filename) {
return JSON.parse(fs.readFileSync(filename));
2018-06-05 16:37:52 -03:00
}
2024-01-22 00:02:31 +08:00
function writeJson(file, content) {
writeFile(file, JSON.stringify(content, null, 2) + "\n");
}
const toPath = (urlOrPath) =>
urlOrPath instanceof URL ? url.fileURLToPath(urlOrPath) : urlOrPath;
function writeFile(file, content) {
try {
fs.mkdirSync(path.dirname(toPath(file)), { recursive: true });
} catch {
// noop
}
fs.writeFileSync(file, content);
2018-06-05 16:37:52 -03:00
}
function processFile(filename, fn) {
const content = fs.readFileSync(filename, "utf8");
fs.writeFileSync(filename, fn(content));
}
2021-04-21 22:15:12 +08:00
async function fetchText(url) {
const response = await fetch(url);
return response.text();
}
2021-09-11 16:20:26 +09:00
function getBlogPostInfo(version) {
const { year, month, day } = getFormattedDate();
return {
file: `website/blog/${year}-${month}-${day}-${version}.md`,
path: `blog/${year}/${month}/${day}/${version}`,
2021-09-11 16:20:26 +09:00
};
}
function getChangelogContent({ version, previousVersion, body }) {
return outdent`
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
2021-09-11 16:20:26 +09:00
${body}
2021-09-11 16:20:26 +09:00
`;
}
2021-07-26 09:24:44 +08:00
export {
2021-04-21 22:15:12 +08:00
fetchText,
getBlogPostInfo,
getChangelogContent,
2018-06-05 16:37:52 -03:00
logPromise,
processFile,
2018-06-05 16:37:52 -03:00
readJson,
runGit,
runYarn,
2018-06-05 16:37:52 -03:00
waitForEnter,
2024-01-22 00:02:31 +08:00
writeFile,
writeJson,
2018-06-05 16:37:52 -03:00
};