2021-11-29 10:07:11 +08:00
|
|
|
import path from "node:path";
|
|
|
|
|
import fs from "node:fs";
|
2022-03-17 21:32:14 +08:00
|
|
|
import { outdent } from "outdent";
|
2021-11-29 10:07:11 +08:00
|
|
|
import { execaSync } from "execa";
|
2022-07-20 09:50:25 +08:00
|
|
|
import { temporaryDirectory as getTemporaryDirectory } from "tempy";
|
2022-02-11 17:33:56 +08:00
|
|
|
import chalk from "chalk";
|
2021-09-09 18:53:31 +08:00
|
|
|
|
|
|
|
|
const allowedClients = new Set(["yarn", "npm", "pnpm"]);
|
|
|
|
|
|
|
|
|
|
let client = process.env.NPM_CLIENT;
|
|
|
|
|
if (!allowedClients.has(client)) {
|
|
|
|
|
client = "yarn";
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 17:25:51 +08:00
|
|
|
const directoriesToClean = new Set();
|
|
|
|
|
|
|
|
|
|
process.on("exit", cleanUp);
|
|
|
|
|
|
|
|
|
|
function cleanUp() {
|
|
|
|
|
if (directoriesToClean.size === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log(chalk.green("Removing installed Prettier:"));
|
|
|
|
|
|
|
|
|
|
for (const directory of directoriesToClean) {
|
|
|
|
|
// Node.js<14 don't support `fs.rmSync`
|
|
|
|
|
try {
|
|
|
|
|
fs.rmSync(directory, { force: true, recursive: true });
|
|
|
|
|
} catch {
|
|
|
|
|
// No op
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(directory)) {
|
|
|
|
|
console.error(chalk.red(` - ${chalk.inverse(directory)} FAIL`));
|
|
|
|
|
} else {
|
|
|
|
|
console.log(chalk.green(` - ${chalk.inverse(directory)} DONE`));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 10:07:11 +08:00
|
|
|
function installPrettier(packageDir) {
|
2022-07-20 09:50:25 +08:00
|
|
|
const tmpDir = getTemporaryDirectory();
|
2022-02-11 17:25:51 +08:00
|
|
|
directoriesToClean.add(tmpDir);
|
2021-11-29 10:07:11 +08:00
|
|
|
const fileName = execaSync("npm", ["pack"], {
|
|
|
|
|
cwd: packageDir,
|
|
|
|
|
}).stdout.trim();
|
2021-09-09 18:53:31 +08:00
|
|
|
const file = path.join(packageDir, fileName);
|
|
|
|
|
const packed = path.join(tmpDir, fileName);
|
|
|
|
|
fs.copyFileSync(file, packed);
|
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
|
|
2022-05-06 20:27:33 +08:00
|
|
|
const runNpmClient = (args) => execaSync(client, args, { cwd: tmpDir });
|
|
|
|
|
|
|
|
|
|
runNpmClient(client === "pnpm" ? ["init"] : ["init", "-y"]);
|
2021-09-09 18:53:31 +08:00
|
|
|
|
|
|
|
|
switch (client) {
|
|
|
|
|
case "npm":
|
|
|
|
|
// npm fails when engine requirement only with `--engine-strict`
|
2022-05-06 20:27:33 +08:00
|
|
|
runNpmClient(["install", packed, "--engine-strict"]);
|
2021-09-09 18:53:31 +08:00
|
|
|
break;
|
|
|
|
|
case "pnpm":
|
|
|
|
|
// Note: current pnpm can't work with `--engine-strict` and engineStrict setting in `.npmrc`
|
2022-05-06 20:27:33 +08:00
|
|
|
runNpmClient(["add", packed, "--engine-strict"]);
|
2021-09-09 18:53:31 +08:00
|
|
|
break;
|
2022-05-06 20:27:33 +08:00
|
|
|
case "yarn":
|
2021-09-09 18:53:31 +08:00
|
|
|
// yarn fails when engine requirement not compatible by default
|
2022-05-06 20:27:33 +08:00
|
|
|
runNpmClient(["config", "set", "nodeLinker", "node-modules"]);
|
|
|
|
|
runNpmClient(["add", `prettier@file:${packed}`]);
|
|
|
|
|
// No default
|
2021-09-09 18:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.unlinkSync(packed);
|
|
|
|
|
|
2022-02-11 17:25:51 +08:00
|
|
|
const installed = path.join(tmpDir, "node_modules/prettier");
|
|
|
|
|
console.log(
|
|
|
|
|
chalk.green(
|
2022-03-17 20:13:28 +08:00
|
|
|
outdent`
|
|
|
|
|
Prettier installed
|
|
|
|
|
at ${chalk.inverse(installed)}
|
|
|
|
|
from ${chalk.inverse(packageDir)}
|
|
|
|
|
with ${chalk.inverse(client)}.
|
2022-02-11 17:25:51 +08:00
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return installed;
|
2021-11-29 10:07:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default installPrettier;
|