forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
158 lines (132 loc) · 3.82 KB
/
utils.js
File metadata and controls
158 lines (132 loc) · 3.82 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import fs from "node:fs";
import path from "node:path";
import readline from "node:readline";
import url from "node:url";
import chalk from "chalk";
import { execa } from "execa";
import outdent from "outdent";
import getFormattedDate from "./get-formatted-date.js";
readline.emitKeypressEvents(process.stdin);
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));
}
function fitTerminal(input, suffix = "") {
const columns = Math.min(process.stdout.columns || 40, 80);
const WIDTH = columns - maxLength + 1;
if (input.length < WIDTH) {
const repeatCount = Math.max(WIDTH - input.length - 1 - suffix.length, 0);
input += chalk.dim(".").repeat(repeatCount) + suffix;
}
return input;
}
async function logPromise(name, promiseOrAsyncFunction, shouldSkip = false) {
process.stdout.write(fitTerminal(name));
if (shouldSkip) {
process.stdout.write(`${status.SKIPPED}\n`);
return;
}
try {
const result = await (typeof promiseOrAsyncFunction === "function"
? promiseOrAsyncFunction()
: promiseOrAsyncFunction);
process.stdout.write(`${status.DONE}\n`);
return result;
} catch (error) {
process.stdout.write(`${status.FAIL}\n`);
throw error;
}
}
async function runYarn(args, options) {
args = Array.isArray(args) ? args : [args];
try {
return await execa("yarn", [...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() {
console.log();
console.log(chalk.gray("Press ENTER to continue."));
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(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);
}
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 {
fetchText,
getBlogPostInfo,
getChangelogContent,
logPromise,
processFile,
readJson,
runGit,
runYarn,
waitForEnter,
writeFile,
writeJson,
};