forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
183 lines (158 loc) · 5.01 KB
/
build.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use strict";
const path = require("path");
const fs = require("fs");
const readline = require("readline");
const chalk = require("chalk");
const execa = require("execa");
const minimist = require("minimist");
const bundler = require("./bundler");
const bundleConfigs = require("./config");
const util = require("./util");
const Cache = require("./cache");
// Errors in promises should be fatal.
const loggedErrors = new Set();
process.on("unhandledRejection", (err) => {
// No need to print it twice.
if (!loggedErrors.has(err)) {
console.error(err);
}
process.exit(1);
});
const CACHE_VERSION = "v32"; // This need update when updating build scripts
const statusConfig = [
{ color: "bgYellow", text: "CACHED" },
{ 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 = WIDTH - input.length - 1 - suffix.length;
input += chalk.dim(".").repeat(repeatCount) + suffix;
}
return input;
}
async function createBundle(bundleConfig, cache, options) {
const { output, target, format, type } = bundleConfig;
process.stdout.write(fitTerminal(output));
try {
const { cached, skipped } = await bundler(bundleConfig, cache, options);
if (skipped) {
console.log(status.SKIPPED);
return;
}
if (cached) {
console.log(status.CACHED);
return;
}
const file = path.join("dist", output);
const content = fs.readFileSync(file, "utf8");
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
if (target === "universal") {
if (content.includes("\ufffe")) {
throw new Error("Bundled umd file should not have U+FFFE character.");
}
}
if (options["print-size"]) {
// Clear previous line
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
const prettyBytes = require("pretty-bytes");
const getSizeText = (file) => prettyBytes(fs.statSync(file).size);
const sizeTexts = [getSizeText(file, content)];
if (
type !== "core" &&
format !== "esm" &&
bundleConfig.bundler !== "webpack" &&
target === "universal"
) {
const esmFile = path.join("dist/esm", output.replace(".js", ".mjs"));
sizeTexts.push(`esm ${getSizeText(esmFile)}`);
}
process.stdout.write(
fitTerminal(output, sizeTexts.join(", ").concat(" "))
);
}
console.log(status.DONE);
} catch (error) {
console.log(status.FAIL + "\n");
handleError(error);
}
}
function handleError(error) {
loggedErrors.add(error);
console.error(error);
throw error;
}
async function cacheFiles(cache) {
// Copy built files to .cache
try {
await execa("rm", ["-rf", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files")]);
await execa("mkdir", ["-p", path.join(".cache", "files", "esm")]);
const manifest = cache.updated;
for (const file of Object.keys(manifest.files)) {
await execa("cp", [
file,
path.join(".cache", file.replace("dist", "files")),
]);
}
} catch (err) {
// Don't fail the build
}
}
async function preparePackage() {
const pkg = await util.readJson("package.json");
pkg.bin = "./bin-prettier.js";
delete pkg.dependencies;
delete pkg.devDependencies;
pkg.scripts = {
prepublishOnly:
"node -e \"assert.equal(require('.').version, require('..').version)\"",
};
pkg.files = ["*.js", "esm/*.mjs"];
await util.writeJson("dist/package.json", pkg);
await util.copyFile("./README.md", "./dist/README.md");
await util.copyFile("./LICENSE", "./dist/LICENSE");
}
async function run(params) {
await execa("rm", ["-rf", "dist"]);
await execa("mkdir", ["-p", "dist"]);
if (!params.playground) {
await execa("mkdir", ["-p", "dist/esm"]);
}
if (params["purge-cache"]) {
await execa("rm", ["-rf", ".cache"]);
}
const bundleCache = new Cache(".cache/", CACHE_VERSION);
await bundleCache.load();
console.log(chalk.inverse(" Building packages "));
for (const bundleConfig of bundleConfigs) {
await createBundle(bundleConfig, bundleCache, params);
}
await cacheFiles(bundleCache);
await bundleCache.save();
if (!params.playground) {
await preparePackage();
}
}
run(
minimist(process.argv.slice(2), {
boolean: ["purge-cache", "playground", "print-size"],
})
);