@tobiastengler/create-relay-app
Version:
Easy configuration of Relay for existing projects
66 lines (65 loc) • 2.36 kB
JavaScript
import { execSync } from "child_process";
import { getExecutingPackageManager } from "../misc/packageManagers/index.js";
import { PackageManagerOptions } from "../types.js";
import { ArgumentBase, getNormalizedCliString } from "./ArgumentBase.js";
export class PackageManagerArgument extends ArgumentBase {
constructor(fs, env) {
super();
this.fs = fs;
this.env = env;
this.name = "packageManager";
this.promptMessage = "What package manager to install packages with";
this.cliArg = "--package-manager";
}
registerCliOption(command) {
const flags = this.getCliFlags("-p", "<manager>");
command.option(flags, "the package manager to use for installing packages", (value) => this.parsePackageManager(value));
}
promptForValue(existingArgs) {
return this.showInquirerPrompt({
type: "list",
choices: PackageManagerOptions,
}, existingArgs);
}
isValid(value, existingArgs) {
return true;
}
getDefaultValue(existingArgs) {
const yarnLockFile = this.env.rel("yarn.lock");
if (this.fs.exists(yarnLockFile.abs)) {
try {
execSync("yarn --version", { stdio: "ignore" });
// Project has a yarn.lock file and yarn is installed.
return Promise.resolve("yarn");
}
catch (_a) { }
}
const pnpmLockFile = this.env.rel("pnpm-lock.yaml");
if (this.fs.exists(pnpmLockFile.abs)) {
try {
execSync("pnpm --version", { stdio: "ignore" });
// Project has a pnpm-lock.yaml file and pnpm is installed.
return Promise.resolve("pnpm");
}
catch (_b) { }
}
const executingPackageManager = getExecutingPackageManager();
return Promise.resolve(executingPackageManager);
}
parsePackageManager(rawInput) {
if (!rawInput) {
return null;
}
const input = getNormalizedCliString(rawInput);
if (input === "yarn") {
return "yarn";
}
if (input === "pnpm") {
return "pnpm";
}
if (input === "npm") {
return "npm";
}
throw this.getInvalidArgError(input, PackageManagerOptions);
}
}