forked from prettier/prettier
/
utils.js
123 lines (102 loc) · 2.93 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import fs from "node:fs";
import readline from "node:readline";
import chalk from "chalk";
import { execa } from "execa";
import stringWidth from "string-width";
import fetch from "node-fetch";
import outdent from "outdent";
import getFormattedDate from "./get-formatted-date.js";
readline.emitKeypressEvents(process.stdin);
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) {
input += chalk.dim(".").repeat(WIDTH - input.length - 1);
}
return input;
}
async function logPromise(name, promiseOrAsyncFunction) {
const promise =
typeof promiseOrAsyncFunction === "function"
? promiseOrAsyncFunction()
: promiseOrAsyncFunction;
process.stdout.write(fitTerminal(name));
try {
const result = await promise;
process.stdout.write(`${OK}\n`);
return result;
} catch (error) {
process.stdout.write(`${FAIL}\n`);
throw error;
}
}
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);
}
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") {
reject(new Error("Process terminated by the user"));
}
}
});
}
function readJson(filename) {
return JSON.parse(fs.readFileSync(filename));
}
function writeJson(filename, content) {
fs.writeFileSync(filename, JSON.stringify(content, null, 2) + "\n");
}
function processFile(filename, fn) {
const content = fs.readFileSync(filename, "utf8");
fs.writeFileSync(filename, fn(content));
}
async function fetchText(url) {
const response = await fetch(url);
return response.text();
}
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`
[diff](https://github.com/prettier/prettier/compare/${previousVersion}...${version})
${body}
`;
}
export {
runYarn,
runGit,
fetchText,
logPromise,
processFile,
readJson,
writeJson,
waitForEnter,
getBlogPostInfo,
getChangelogContent,
};