forked from pre-commit/mirrors-prettier
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun-prettier.js
More file actions
98 lines (81 loc) · 3.62 KB
/
Copy pathrun-prettier.js
File metadata and controls
98 lines (81 loc) · 3.62 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
#!/usr/bin/env node
"use strict";
import { readFileSync, readdirSync, existsSync } from "node:fs";
import path from "node:path";
import { createRequire } from "node:module";
import url from "node:url";
const nodePath = path.resolve(process.env.NODE_PATH);
const pluginsPath = path.resolve(nodePath, "@prettier");
let plugins = [];
if (existsSync(pluginsPath)) {
plugins = readdirSync(pluginsPath, { withFileTypes: true });
}
// if there is a plugin added using '--plugin=<plugin-name>' command line argument
// we add them to the plugins list searching them inside the node folder
for (let argumentIndex = process.argv.length - 1; argumentIndex >= 0; argumentIndex--) {
const argument = process.argv[argumentIndex];
if (!argument.startsWith("--plugin=")) {
continue;
}
const pluginName = argument.slice("--plugin=".length);
const matchingDirectories = readdirSync(nodePath, { withFileTypes: true }).filter(
(pluginPath) => pluginPath.isDirectory() && pluginPath.name.localeCompare(pluginName, "en", { sensitivity: "base" }) === 0
);
if (matchingDirectories.length === 0) {
continue;
}
// consume the command line argument
process.argv.splice(argumentIndex, 1);
plugins.push(
...matchingDirectories.filter((directory) =>
plugins.every((existing) => directory.parentPath !== existing.parentPath && directory.name !== existing.name)
)
);
}
// neither single nor double quotes are supported around the path.
const additionalArguments = plugins
.filter((pluginPath) => pluginPath.isDirectory())
.flatMap((pluginPath) => {
const pluginDirectoryPath = path.join(pluginPath.parentPath, pluginPath.name);
let pluginImportPath;
try {
const packageJsonPath = path.join(pluginDirectoryPath, "package.json");
const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
const packageJson = JSON.parse(packageJsonContent);
if (packageJson.exports && packageJson.exports["."]) {
if (typeof packageJson.exports["."] === "string") {
pluginImportPath = path.join(pluginDirectoryPath, packageJson.exports["."]);
} else if (packageJson.exports["."].default) {
pluginImportPath = path.join(pluginDirectoryPath, packageJson.exports["."].default);
}
}
if (pluginImportPath) {
// path defined by 'exports'
} else if (packageJson.main) {
pluginImportPath = path.join(pluginDirectoryPath, packageJson.main);
} else if (packageJson.module) {
pluginImportPath = path.join(pluginDirectoryPath, packageJson.module);
} else {
pluginImportPath = pluginDirectoryPath;
}
} catch (error) {
console.error(`Error '${error}' reading or parsing package.json from '${pluginDirectoryPath}'`);
pluginImportPath = pluginDirectoryPath;
}
const pluginImportFileUrl = url.pathToFileURL(pluginImportPath);
return ["--plugin", pluginImportFileUrl.toString()];
});
// the first two items of process.argv are reserved (the node.exe and the current file name) so insert after them
process.argv.splice(2, 0, ...additionalArguments);
// Override stdout.write to filter message that end with "(unchanged)"
// prettier currently logs messages when a file is not changed we are not interested in them
const originalWrite = process.stdout.write;
process.stdout.write = function (message, ...optionalParams) {
if (typeof message === "string" && message.includes(" (unchanged)")) {
return;
}
originalWrite.apply(process.stdout, [message, ...optionalParams]);
};
const requireImport = createRequire(import.meta.url);
const runPrettier = requireImport("prettier/bin/prettier.cjs");
await runPrettier;