MCPcopy
hub / github.com/prettier/prettier / installPrettier

Function installPrettier

tests/config/install-prettier.js:82–160  ·  view source on GitHub ↗
(packageDirectory)

Source from the content-addressed store, hash-verified

80}
81
82function installPrettier(packageDirectory) {
83 const start = performance.now();
84 const temporaryDirectory = createTemporaryDirectory();
85 directoriesToClean.add(temporaryDirectory);
86 const fileName = spawn("npm", ["pack"], {
87 cwd: packageDirectory,
88 encoding: "utf8",
89 }).stdout.trim();
90 const file = path.join(packageDirectory, fileName);
91 const packed = path.join(temporaryDirectory, fileName);
92 fs.copyFileSync(file, packed);
93 fs.unlinkSync(file);
94
95 const runNpmClient = (args) => {
96 const result = spawn(client, args, {
97 cwd: temporaryDirectory,
98 encoding: "utf8",
99 });
100
101 if (result.status === 0) {
102 return result;
103 }
104
105 const { stdout, stderr } = result;
106 const output = [stdout, stderr].filter(Boolean).join("\n");
107
108 throw new Error(outdent`
109 Failed to execute ${picocolors.blue([client, ...args].join(" "))}.
110 ${output}
111 `);
112 };
113
114 runNpmClient(client === "pnpm" ? ["init"] : ["init", "-y"]);
115
116 switch (client) {
117 case "npm":
118 // npm fails when engine requirement only with `--engine-strict`
119 runNpmClient(["install", packed, "--engine-strict"]);
120 break;
121 case "pnpm":
122 // fails engine mismatch
123 runNpmClient(["add", packed, "--engine-strict"]);
124 break;
125 case "yarn": {
126 // Yarn currently doesn't fail on engine requirement
127 // https://github.com/yarnpkg/berry/issues/1177
128 const { stdout } = runNpmClient(["--version"]);
129 if (!/^3\./.test(stdout)) {
130 runNpmClient(["config", "set", "npmMinimalAgeGate", "0"]);
131 }
132
133 runNpmClient(["config", "set", "nodeLinker", "node-modules"]);
134 runNpmClient(["add", `prettier@file:${packed}`]);
135 break;
136 }
137 // No default
138 }
139

Callers 1

jest.config.jsFile · 0.85

Calls 5

createTemporaryDirectoryFunction · 0.85
spawnFunction · 0.85
runNpmClientFunction · 0.85
trimMethod · 0.45
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…