@visulima/package
Version:
One Package to rule them all, finds your root-dir, monorepo, or package manager.
181 lines (174 loc) • 6.53 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const node_child_process = require('node:child_process');
const node_fs = require('node:fs');
const fs = require('@visulima/fs');
const error = require('@visulima/fs/error');
const path = require('@visulima/path');
const packageJson = require('./package-json.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const lockFileNames = ["yarn.lock", "package-lock.json", "pnpm-lock.yaml", "npm-shrinkwrap.json", "bun.lockb"];
const packageMangerFindUpMatcher = /* @__PURE__ */ __name((directory) => {
let lockFile;
lockFileNames.forEach((lockFileName) => {
if (!lockFile && node_fs.existsSync(path.join(directory, lockFileName))) {
lockFile = path.join(directory, lockFileName);
}
});
if (lockFile) {
return lockFile;
}
const packageJsonFilePath = path.join(directory, "package.json");
if (node_fs.existsSync(packageJsonFilePath)) {
const packageJson$1 = packageJson.parsePackageJson(node_fs.readFileSync(packageJsonFilePath, "utf8"));
if (packageJson$1.packageManager !== void 0) {
return packageJsonFilePath;
}
}
return void 0;
}, "packageMangerFindUpMatcher");
const findPackageManagerOnFile = /* @__PURE__ */ __name((foundFile) => {
if (!foundFile) {
throw new error.NotFoundError("Could not find a package manager");
}
if (foundFile.endsWith("package.json")) {
const packageJson$1 = packageJson.parsePackageJson(foundFile);
if (packageJson$1.packageManager) {
const packageManagerNames = ["npm", "yarn", "pnpm", "bun"];
const foundPackageManager = packageManagerNames.find((prefix) => packageJson$1.packageManager.startsWith(prefix));
if (foundPackageManager) {
return {
packageManager: foundPackageManager,
path: path.dirname(foundFile)
};
}
}
}
if (foundFile.endsWith("yarn.lock")) {
return {
packageManager: "yarn",
path: path.dirname(foundFile)
};
}
if (foundFile.endsWith("package-lock.json") || foundFile.endsWith("npm-shrinkwrap.json")) {
return {
packageManager: "npm",
path: path.dirname(foundFile)
};
}
if (foundFile.endsWith("pnpm-lock.yaml")) {
return {
packageManager: "pnpm",
path: path.dirname(foundFile)
};
}
if (foundFile.endsWith("bun.lockb")) {
return {
packageManager: "bun",
path: path.dirname(foundFile)
};
}
throw new error.NotFoundError("Could not find a package manager");
}, "findPackageManagerOnFile");
const findLockFile = /* @__PURE__ */ __name(async (cwd) => {
const filePath = await fs.findUp(lockFileNames, {
type: "file",
...cwd && { cwd }
});
if (!filePath) {
throw new Error("Could not find lock file");
}
return filePath;
}, "findLockFile");
const findLockFileSync = /* @__PURE__ */ __name((cwd) => {
const filePath = fs.findUpSync(lockFileNames, {
type: "file",
...cwd && { cwd }
});
if (!filePath) {
throw new Error("Could not find lock file");
}
return filePath;
}, "findLockFileSync");
const findPackageManager = /* @__PURE__ */ __name(async (cwd) => {
const foundFile = await fs.findUp(packageMangerFindUpMatcher, {
...cwd && { cwd }
});
return findPackageManagerOnFile(foundFile);
}, "findPackageManager");
const findPackageManagerSync = /* @__PURE__ */ __name((cwd) => {
const foundFile = fs.findUpSync(packageMangerFindUpMatcher, {
...cwd && { cwd }
});
return findPackageManagerOnFile(foundFile);
}, "findPackageManagerSync");
const getPackageManagerVersion = /* @__PURE__ */ __name((name) => node_child_process.execSync(`${name} --version`).toString("utf8").trim(), "getPackageManagerVersion");
const identifyInitiatingPackageManager = /* @__PURE__ */ __name(async () => {
if (!process.env.npm_config_user_agent) {
return void 0;
}
const pmSpec = process.env.npm_config_user_agent.split(" ")[0];
const separatorPos = pmSpec.lastIndexOf("/");
const name = pmSpec.slice(0, Math.max(0, separatorPos));
return {
name: name === "npminstall" ? "cnpm" : name,
version: pmSpec.slice(Math.max(0, separatorPos + 1))
};
}, "identifyInitiatingPackageManager");
const generateMissingPackagesInstallMessage = /* @__PURE__ */ __name((packageName, missingPackages, options) => {
const s = missingPackages.length === 1 ? "" : "s";
if (options.packageManagers === void 0) {
options.packageManagers = ["npm", "pnpm", "yarn"];
}
if (options.packageManagers.length === 0) {
throw new Error("No package managers provided, please provide at least one package manager");
}
if (missingPackages.length === 0) {
throw new Error("No missing packages provided, please provide at least one missing package");
}
let message = `
${options.preMessage ?? ""}
${packageName} could not find the following package${s}
${missingPackages.join("\n ")}
To install the missing package${s}, please run the following command:
`;
const atLatest = /* @__PURE__ */ __name((name) => {
if (!name.split("@").includes("@")) {
return `${name}@latest`;
}
return name;
}, "atLatest");
const packageManagerCommands = options.packageManagers.map((packageManager) => {
const missingPackagesString = missingPackages.map((element) => atLatest(element)).join(" ");
switch (packageManager) {
case "bun": {
return ` bun add ${missingPackagesString} -D`;
}
case "npm": {
return ` npm install ${missingPackagesString} --save-dev`;
}
case "pnpm": {
return ` pnpm add ${missingPackagesString} -D`;
}
case "yarn": {
return ` yarn add ${missingPackagesString} --dev`;
}
default: {
throw new Error("Unknown package manager");
}
}
});
message += packageManagerCommands.join("\n\nor\n\n");
if (options.postMessage) {
message += options.postMessage;
}
return message;
}, "generateMissingPackagesInstallMessage");
exports.findLockFile = findLockFile;
exports.findLockFileSync = findLockFileSync;
exports.findPackageManager = findPackageManager;
exports.findPackageManagerSync = findPackageManagerSync;
exports.generateMissingPackagesInstallMessage = generateMissingPackagesInstallMessage;
exports.getPackageManagerVersion = getPackageManagerVersion;
exports.identifyInitiatingPackageManager = identifyInitiatingPackageManager;