2020-04-18 01:24:22 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const path = require("path");
|
|
|
|
|
const shell = require("shelljs");
|
|
|
|
|
const tempy = require("tempy");
|
|
|
|
|
|
|
|
|
|
shell.config.fatal = true;
|
|
|
|
|
|
2020-04-23 10:37:09 +08:00
|
|
|
const client = process.env.NPM_CLIENT || "yarn";
|
2020-04-18 01:24:22 +08:00
|
|
|
|
2021-02-11 00:50:59 +08:00
|
|
|
module.exports = (packageDir) => {
|
2020-04-18 01:24:22 +08:00
|
|
|
const tmpDir = tempy.directory();
|
2020-04-23 10:37:09 +08:00
|
|
|
const file = shell.exec("npm pack", { cwd: packageDir }).stdout.trim();
|
|
|
|
|
shell.mv(path.join(packageDir, file), tmpDir);
|
|
|
|
|
const tarPath = path.join(tmpDir, file);
|
2020-04-18 01:24:22 +08:00
|
|
|
|
2020-04-23 10:37:09 +08:00
|
|
|
shell.exec(`${client} init -y`, { cwd: tmpDir, silent: true });
|
|
|
|
|
let installCommand = "";
|
|
|
|
|
switch (client) {
|
|
|
|
|
case "npm":
|
|
|
|
|
// npm fails when engine requirement only with `--engine-strict`
|
|
|
|
|
installCommand = `npm install "${tarPath}" --engine-strict`;
|
|
|
|
|
break;
|
|
|
|
|
case "pnpm":
|
|
|
|
|
// Note: current pnpm can't work with `--engine-strict` and engineStrict setting in `.npmrc`
|
|
|
|
|
installCommand = `pnpm add "${tarPath}"`;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// yarn fails when engine requirement not compatible by default
|
|
|
|
|
installCommand = `yarn add "${tarPath}"`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shell.exec(installCommand, { cwd: tmpDir });
|
2020-04-18 01:24:22 +08:00
|
|
|
|
|
|
|
|
return path.join(tmpDir, "node_modules/prettier");
|
|
|
|
|
};
|