forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-worker.js
More file actions
109 lines (94 loc) · 3.06 KB
/
cli-worker.js
File metadata and controls
109 lines (94 loc) · 3.06 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
import fs from "node:fs";
import path from "node:path";
import readline from "node:readline";
import url from "node:url";
import { parentPort, workerData } from "node:worker_threads";
import { prettierCli, prettierMainEntry } from "./env.js";
const normalizeToPosix =
path.sep === "\\"
? (filepath) => replaceAll(filepath, "\\", "/")
: (filepath) => filepath;
const hasOwn =
Object.hasOwn ??
((object, property) =>
// eslint-disable-next-line prefer-object-has-own
Object.prototype.hasOwnProperty.call(object, property));
const replaceAll = (text, find, replacement) =>
text.replaceAll
? text.replaceAll(find, replacement)
: text.split(find).join(replacement);
async function run() {
const { options } = workerData;
Date.now = () => 0;
/*
A fake non-existing directory to test plugin search won't crash.
See:
- `isDirectory` function in `src/common/load-plugins.js`
- Test file `./__tests__/plugin-virtual-directory.js`
- Pull request #5819
*/
const originalStat = fs.promises.stat;
fs.promises.statSync = (filename) =>
originalStat(
path.basename(filename) === "virtualDirectory"
? import.meta.url
: filename,
);
readline.clearLine = (stream) => {
stream.write(
`\n[[called readline.clearLine(${
stream === process.stdout
? "process.stdout"
: stream === process.stderr
? "process.stderr"
: "unknown stream"
})]]\n`,
);
};
process.stdin.isTTY = Boolean(options.isTTY);
process.stdout.isTTY = Boolean(options.stdoutIsTTY);
const prettier = await import(url.pathToFileURL(prettierMainEntry));
const { mockable } = prettier.__debug;
// We cannot use `jest.setMock("get-stream", impl)` here, because in the
// production build everything is bundled into one file so there is no
// "get-stream" module to mock.
// eslint-disable-next-line require-await
mockable.getStdin = async () => options.input || "";
mockable.isCI = () => Boolean(options.ci);
mockable.getPrettierConfigSearchStopDirectory = () =>
url.fileURLToPath(new URL("./cli", import.meta.url));
// eslint-disable-next-line require-await
mockable.writeFormattedFile = async (filename, content) => {
filename = normalizeToPosix(path.relative(process.cwd(), filename));
if (
options.mockWriteFileErrors &&
hasOwn(options.mockWriteFileErrors, filename)
) {
throw new Error(
options.mockWriteFileErrors[filename] + " (mocked error)",
);
}
parentPort.postMessage({
action: "write-file",
data: { filename, content },
});
};
const { __promise: promise } = await import(url.pathToFileURL(prettierCli));
await promise;
}
parentPort.on("message", async () => {
const originalExit = process.exit;
// https://github.com/nodejs/node/issues/30491
process.stdout.cork();
process.stderr.cork();
process.exit = (code) => {
process.stdout.end();
process.stderr.end();
originalExit(code ?? process.exitCode ?? 0);
};
try {
await run();
} finally {
process.exit();
}
});