2021-07-26 09:24:44 +08:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
import readline from "node:readline";
|
|
|
|
|
import chalk from "chalk";
|
2021-11-19 20:50:30 +08:00
|
|
|
import { execa } from "execa";
|
2021-07-26 09:24:44 +08:00
|
|
|
import stringWidth from "string-width";
|
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
|
|
|
|
|
|
|
|
const OK = chalk.bgGreen.black(" DONE ");
|
|
|
|
|
const FAIL = chalk.bgRed.black(" FAIL ");
|
|
|
|
|
|
|
|
|
|
function fitTerminal(input) {
|
|
|
|
|
const columns = Math.min(process.stdout.columns, 80);
|
|
|
|
|
const WIDTH = columns - stringWidth(OK) + 1;
|
|
|
|
|
if (input.length < WIDTH) {
|
2020-01-19 06:48:04 +08:00
|
|
|
input += chalk.dim(".").repeat(WIDTH - input.length - 1);
|
2018-06-05 16:37:52 -03:00
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 22:15:12 +08:00
|
|
|
async function logPromise(name, promiseOrAsyncFunction) {
|
|
|
|
|
const promise =
|
|
|
|
|
typeof promiseOrAsyncFunction === "function"
|
|
|
|
|
? promiseOrAsyncFunction()
|
|
|
|
|
: promiseOrAsyncFunction;
|
|
|
|
|
|
2018-06-05 16:37:52 -03:00
|
|
|
process.stdout.write(fitTerminal(name));
|
|
|
|
|
|
2021-04-21 22:15:12 +08:00
|
|
|
try {
|
|
|
|
|
const result = await promise;
|
|
|
|
|
process.stdout.write(`${OK}\n`);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
process.stdout.write(`${FAIL}\n`);
|
|
|
|
|
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 execa("yarn", ["--silent", ...args], options);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`\`yarn ${args.join(" ")}\` failed\n${error.stdout}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runGit(args, options) {
|
|
|
|
|
args = Array.isArray(args) ? args : [args];
|
|
|
|
|
return execa("git", args, options);
|
2018-06-05 16:37:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function waitForEnter() {
|
|
|
|
|
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) {
|
2022-01-04 13:56:10 +08:00
|
|
|
return JSON.parse(fs.readFileSync(filename));
|
2018-06-05 16:37:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function writeJson(filename, content) {
|
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(content, null, 2) + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 12:37:52 +08:00
|
|
|
function processFile(filename, fn) {
|
2022-03-25 09:35:28 +08:00
|
|
|
const content = fs.readFileSync(filename, "utf8");
|
2018-06-22 12:37:52 +08:00
|
|
|
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}.html`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getChangelogContent({ version, previousVersion, body }) {
|
|
|
|
|
return outdent`
|
2022-03-17 20:13:28 +08:00
|
|
|
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
|
2021-09-11 16:20:26 +09:00
|
|
|
|
2022-03-17 20:13:28 +08:00
|
|
|
${body}
|
2021-09-11 16:20:26 +09:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-26 09:24:44 +08:00
|
|
|
export {
|
2018-06-05 16:37:52 -03:00
|
|
|
runYarn,
|
2021-04-21 22:15:12 +08:00
|
|
|
runGit,
|
|
|
|
|
fetchText,
|
2018-06-05 16:37:52 -03:00
|
|
|
logPromise,
|
2018-06-22 12:37:52 +08:00
|
|
|
processFile,
|
2018-06-05 16:37:52 -03:00
|
|
|
readJson,
|
|
|
|
|
writeJson,
|
|
|
|
|
waitForEnter,
|
2021-09-11 16:20:26 +09:00
|
|
|
getBlogPostInfo,
|
|
|
|
|
getChangelogContent,
|
2018-06-05 16:37:52 -03:00
|
|
|
};
|