@antfu/install-pkg
Advanced tools
Comparing version 0.1.0 to 0.1.1
import execa from 'execa'; | ||
declare type PackageManager = 'pnpm' | 'yarn' | 'npm'; | ||
declare function detectPackageManager(cwd?: string): Promise<PackageManager | null>; | ||
declare type PackageManager = 'pnpm' | 'yarn' | 'npm' | 'bun'; | ||
declare const AGENTS: readonly ["pnpm", "yarn", "npm", "pnpm@6", "yarn@berry", "bun"]; | ||
declare type Agent = typeof AGENTS[number]; | ||
declare function detectPackageManager(cwd?: string): Promise<Agent | null>; | ||
@@ -11,2 +13,3 @@ interface InstallPackageOptions { | ||
packageManager?: string; | ||
packageManagerVersion?: string; | ||
preferOffline?: boolean; | ||
@@ -17,2 +20,2 @@ additionalArgs?: string[]; | ||
export { InstallPackageOptions, PackageManager, detectPackageManager, installPackage }; | ||
export { Agent, InstallPackageOptions, PackageManager, detectPackageManager, installPackage }; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __create = Object.create; | ||
@@ -7,37 +8,67 @@ var __defProp = Object.defineProperty; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
__markAsModule(target); | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __reExport = (target, module2, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && key !== "default") | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
return to; | ||
}; | ||
var __toModule = (module2) => { | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
// src/index.ts | ||
__export(exports, { | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
detectPackageManager: () => detectPackageManager, | ||
installPackage: () => installPackage | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/detect.ts | ||
var import_path = __toModule(require("path")); | ||
var import_find_up = __toModule(require("find-up")); | ||
var import_fs = __toESM(require("fs")); | ||
var import_path = __toESM(require("path")); | ||
var import_find_up = __toESM(require("find-up")); | ||
var AGENTS = ["pnpm", "yarn", "npm", "pnpm@6", "yarn@berry", "bun"]; | ||
var LOCKS = { | ||
"bun.lockb": "bun", | ||
"pnpm-lock.yaml": "pnpm", | ||
"yarn.lock": "yarn", | ||
"package-lock.json": "npm" | ||
"package-lock.json": "npm", | ||
"npm-shrinkwrap.json": "npm" | ||
}; | ||
async function detectPackageManager(cwd = process.cwd()) { | ||
const result = await (0, import_find_up.default)(Object.keys(LOCKS), { cwd }); | ||
const agent = result ? LOCKS[import_path.default.basename(result)] : null; | ||
let agent = null; | ||
const lockPath = await (0, import_find_up.default)(Object.keys(LOCKS), { cwd }); | ||
let packageJsonPath; | ||
if (lockPath) | ||
packageJsonPath = import_path.default.resolve(lockPath, "../package.json"); | ||
else | ||
packageJsonPath = await (0, import_find_up.default)("package.json", { cwd }); | ||
if (packageJsonPath && import_fs.default.existsSync(packageJsonPath)) { | ||
try { | ||
const pkg = JSON.parse(import_fs.default.readFileSync(packageJsonPath, "utf8")); | ||
if (typeof pkg.packageManager === "string") { | ||
const [name, version] = pkg.packageManager.split("@"); | ||
if (name === "yarn" && parseInt(version) > 1) | ||
agent = "yarn@berry"; | ||
else if (name === "pnpm" && parseInt(version) < 7) | ||
agent = "pnpm@6"; | ||
else if (name in AGENTS) | ||
agent = name; | ||
else | ||
console.warn("[ni] Unknown packageManager:", pkg.packageManager); | ||
} | ||
} catch { | ||
} | ||
} | ||
if (!agent && lockPath) | ||
agent = LOCKS[import_path.default.basename(lockPath)]; | ||
return agent; | ||
@@ -47,19 +78,28 @@ } | ||
// src/install.ts | ||
var import_execa = __toModule(require("execa")); | ||
var import_execa = __toESM(require("execa")); | ||
async function installPackage(names, options = {}) { | ||
const agent = options.packageManager || await detectPackageManager(options.cwd) || "npm"; | ||
const detectedAgent = options.packageManager || await detectPackageManager(options.cwd) || "npm"; | ||
const [agent] = detectedAgent.split("@"); | ||
if (!Array.isArray(names)) | ||
names = [names]; | ||
const args = options.additionalArgs || []; | ||
if (options.preferOffline) | ||
args.unshift("--prefer-offline"); | ||
return (0, import_execa.default)(agent, [ | ||
agent === "yarn" ? "add" : "install", | ||
options.dev ? "-D" : "", | ||
...args, | ||
...names | ||
].filter(Boolean), { | ||
stdio: options.silent ? "ignore" : "inherit", | ||
cwd: options.cwd | ||
}); | ||
if (options.preferOffline) { | ||
if (detectedAgent === "yarn@berry") | ||
args.unshift("--cached"); | ||
else | ||
args.unshift("--prefer-offline"); | ||
} | ||
return (0, import_execa.default)( | ||
agent, | ||
[ | ||
agent === "yarn" ? "add" : "install", | ||
options.dev ? "-D" : "", | ||
...args, | ||
...names | ||
].filter(Boolean), | ||
{ | ||
stdio: options.silent ? "ignore" : "inherit", | ||
cwd: options.cwd | ||
} | ||
); | ||
} | ||
@@ -66,0 +106,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "@antfu/install-pkg", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"packageManager": "pnpm@7.12.0", | ||
"description": "Install package programmatically.", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"funding": "https://github.com/sponsors/antfu", | ||
"author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"bugs": { | ||
"url": "https://github.com/antfu/install-pkg/issues" | ||
}, | ||
"funding": "https://github.com/sponsors/antfu", | ||
"homepage": "https://github.com/antfu/install-pkg#readme", | ||
@@ -26,16 +14,20 @@ "repository": { | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/antfu/install-pkg/issues" | ||
}, | ||
"keywords": [], | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^0.7.0", | ||
"@antfu/ni": "^0.7.0", | ||
"@types/node": "^16.10.1", | ||
"bumpp": "^7.1.1", | ||
"eslint": "^7.32.0", | ||
"esno": "^0.9.1", | ||
"tsup": "^4.14.0", | ||
"typescript": "^4.4.3" | ||
}, | ||
"dependencies": { | ||
@@ -45,2 +37,12 @@ "execa": "^5.1.1", | ||
}, | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^0.27.0", | ||
"@antfu/ni": "^0.18.0", | ||
"@types/node": "^16.11.59", | ||
"bumpp": "^8.2.1", | ||
"eslint": "^8.23.1", | ||
"esno": "^0.16.3", | ||
"tsup": "^6.2.3", | ||
"typescript": "^4.8.3" | ||
}, | ||
"scripts": { | ||
@@ -51,5 +53,4 @@ "dev": "nr build --watch", | ||
"release": "bumpp --commit --push --tag && pnpm publish", | ||
"lint": "eslint \"{src,test}/**/*.ts\"", | ||
"lint:fix": "nr lint -- --fix" | ||
"lint": "eslint ." | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9714
194
2