@antfu/install-pkg
Advanced tools
+113
| "use strict"; | ||
| var __create = Object.create; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __getProtoOf = Object.getPrototypeOf; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| 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 to; | ||
| }; | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
| // If the importer is in node compatibility mode or this is not an ESM | ||
| // file that has been converted to a CommonJS file using a Babel- | ||
| // compatible transform (i.e. "__esModule" has not been set), then set | ||
| // "default" to the CommonJS "module.exports" for node compatibility. | ||
| 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 | ||
| var src_exports = {}; | ||
| __export(src_exports, { | ||
| detectPackageManager: () => detectPackageManager, | ||
| installPackage: () => installPackage | ||
| }); | ||
| module.exports = __toCommonJS(src_exports); | ||
| // src/detect.ts | ||
| var import_node_fs = __toESM(require("fs"), 1); | ||
| var import_node_path = __toESM(require("path"), 1); | ||
| var import_node_process = __toESM(require("process"), 1); | ||
| var import_find_up = 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", | ||
| "npm-shrinkwrap.json": "npm" | ||
| }; | ||
| async function detectPackageManager(cwd = import_node_process.default.cwd()) { | ||
| let agent = null; | ||
| const lockPath = await (0, import_find_up.findUp)(Object.keys(LOCKS), { cwd }); | ||
| let packageJsonPath; | ||
| if (lockPath) | ||
| packageJsonPath = import_node_path.default.resolve(lockPath, "../package.json"); | ||
| else | ||
| packageJsonPath = await (0, import_find_up.findUp)("package.json", { cwd }); | ||
| if (packageJsonPath && import_node_fs.default.existsSync(packageJsonPath)) { | ||
| try { | ||
| const pkg = JSON.parse(import_node_fs.default.readFileSync(packageJsonPath, "utf8")); | ||
| if (typeof pkg.packageManager === "string") { | ||
| const [name, version] = pkg.packageManager.split("@"); | ||
| if (name === "yarn" && Number.parseInt(version) > 1) | ||
| agent = "yarn@berry"; | ||
| else if (name === "pnpm" && Number.parseInt(version) < 7) | ||
| agent = "pnpm@6"; | ||
| else if (AGENTS.includes(name)) | ||
| agent = name; | ||
| else | ||
| console.warn("[ni] Unknown packageManager:", pkg.packageManager); | ||
| } | ||
| } catch { | ||
| } | ||
| } | ||
| if (!agent && lockPath) | ||
| agent = LOCKS[import_node_path.default.basename(lockPath)]; | ||
| return agent; | ||
| } | ||
| // src/install.ts | ||
| var import_execa = require("execa"); | ||
| async function installPackage(names, options = {}) { | ||
| 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) { | ||
| if (detectedAgent === "yarn@berry") | ||
| args.unshift("--cached"); | ||
| else | ||
| args.unshift("--prefer-offline"); | ||
| } | ||
| return (0, import_execa.execa)( | ||
| agent, | ||
| [ | ||
| agent === "yarn" ? "add" : "install", | ||
| options.dev ? "-D" : "", | ||
| ...args, | ||
| ...names | ||
| ].filter(Boolean), | ||
| { | ||
| stdio: options.silent ? "ignore" : "inherit", | ||
| cwd: options.cwd | ||
| } | ||
| ); | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| detectPackageManager, | ||
| installPackage | ||
| }); |
| import * as execa from 'execa'; | ||
| type PackageManager = 'pnpm' | 'yarn' | 'npm' | 'bun'; | ||
| declare const AGENTS: readonly ["pnpm", "yarn", "npm", "pnpm@6", "yarn@berry", "bun"]; | ||
| type Agent = typeof AGENTS[number]; | ||
| declare function detectPackageManager(cwd?: string): Promise<Agent | null>; | ||
| interface InstallPackageOptions { | ||
| cwd?: string; | ||
| dev?: boolean; | ||
| silent?: boolean; | ||
| packageManager?: string; | ||
| packageManagerVersion?: string; | ||
| preferOffline?: boolean; | ||
| additionalArgs?: string[]; | ||
| } | ||
| declare function installPackage(names: string | string[], options?: InstallPackageOptions): Promise<execa.ExecaReturnValue<string>>; | ||
| export { type Agent, type InstallPackageOptions, type PackageManager, detectPackageManager, installPackage }; |
+4
-4
@@ -1,6 +0,6 @@ | ||
| import execa from 'execa'; | ||
| import * as execa from 'execa'; | ||
| declare type PackageManager = 'pnpm' | 'yarn' | 'npm' | 'bun'; | ||
| type PackageManager = 'pnpm' | 'yarn' | 'npm' | 'bun'; | ||
| declare const AGENTS: readonly ["pnpm", "yarn", "npm", "pnpm@6", "yarn@berry", "bun"]; | ||
| declare type Agent = typeof AGENTS[number]; | ||
| type Agent = typeof AGENTS[number]; | ||
| declare function detectPackageManager(cwd?: string): Promise<Agent | null>; | ||
@@ -19,2 +19,2 @@ | ||
| export { Agent, InstallPackageOptions, PackageManager, detectPackageManager, installPackage }; | ||
| export { type Agent, type InstallPackageOptions, type PackageManager, detectPackageManager, installPackage }; |
+17
-50
@@ -1,38 +0,6 @@ | ||
| "use strict"; | ||
| var __create = Object.create; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __getProtoOf = Object.getPrototypeOf; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| 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 to; | ||
| }; | ||
| 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 | ||
| var src_exports = {}; | ||
| __export(src_exports, { | ||
| detectPackageManager: () => detectPackageManager, | ||
| installPackage: () => installPackage | ||
| }); | ||
| module.exports = __toCommonJS(src_exports); | ||
| // src/detect.ts | ||
| var import_fs = __toESM(require("fs")); | ||
| var import_path = __toESM(require("path")); | ||
| var import_find_up = __toESM(require("find-up")); | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import process from "process"; | ||
| import { findUp } from "find-up"; | ||
| var AGENTS = ["pnpm", "yarn", "npm", "pnpm@6", "yarn@berry", "bun"]; | ||
@@ -48,18 +16,18 @@ var LOCKS = { | ||
| let agent = null; | ||
| const lockPath = await (0, import_find_up.default)(Object.keys(LOCKS), { cwd }); | ||
| const lockPath = await findUp(Object.keys(LOCKS), { cwd }); | ||
| let packageJsonPath; | ||
| if (lockPath) | ||
| packageJsonPath = import_path.default.resolve(lockPath, "../package.json"); | ||
| packageJsonPath = path.resolve(lockPath, "../package.json"); | ||
| else | ||
| packageJsonPath = await (0, import_find_up.default)("package.json", { cwd }); | ||
| if (packageJsonPath && import_fs.default.existsSync(packageJsonPath)) { | ||
| packageJsonPath = await findUp("package.json", { cwd }); | ||
| if (packageJsonPath && fs.existsSync(packageJsonPath)) { | ||
| try { | ||
| const pkg = JSON.parse(import_fs.default.readFileSync(packageJsonPath, "utf8")); | ||
| const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); | ||
| if (typeof pkg.packageManager === "string") { | ||
| const [name, version] = pkg.packageManager.split("@"); | ||
| if (name === "yarn" && parseInt(version) > 1) | ||
| if (name === "yarn" && Number.parseInt(version) > 1) | ||
| agent = "yarn@berry"; | ||
| else if (name === "pnpm" && parseInt(version) < 7) | ||
| else if (name === "pnpm" && Number.parseInt(version) < 7) | ||
| agent = "pnpm@6"; | ||
| else if (name in AGENTS) | ||
| else if (AGENTS.includes(name)) | ||
| agent = name; | ||
@@ -73,3 +41,3 @@ else | ||
| if (!agent && lockPath) | ||
| agent = LOCKS[import_path.default.basename(lockPath)]; | ||
| agent = LOCKS[path.basename(lockPath)]; | ||
| return agent; | ||
@@ -79,3 +47,3 @@ } | ||
| // src/install.ts | ||
| var import_execa = __toESM(require("execa")); | ||
| import { execa } from "execa"; | ||
| async function installPackage(names, options = {}) { | ||
@@ -93,3 +61,3 @@ const detectedAgent = options.packageManager || await detectPackageManager(options.cwd) || "npm"; | ||
| } | ||
| return (0, import_execa.default)( | ||
| return execa( | ||
| agent, | ||
@@ -108,6 +76,5 @@ [ | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| export { | ||
| detectPackageManager, | ||
| installPackage | ||
| }); | ||
| }; |
+16
-15
| { | ||
| "name": "@antfu/install-pkg", | ||
| "version": "0.1.1", | ||
| "packageManager": "pnpm@7.12.0", | ||
| "type": "module", | ||
| "version": "0.2.0", | ||
| "packageManager": "pnpm@8.10.5", | ||
| "description": "Install package programmatically.", | ||
@@ -22,8 +23,8 @@ "author": "Anthony Fu <anthonyfu117@hotmail.com>", | ||
| "types": "./dist/index.d.ts", | ||
| "require": "./dist/index.js", | ||
| "import": "./dist/index.mjs" | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "main": "dist/index.js", | ||
| "module": "dist/index.mjs", | ||
| "module": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
@@ -34,14 +35,14 @@ "files": [ | ||
| "dependencies": { | ||
| "execa": "^5.1.1", | ||
| "find-up": "^5.0.0" | ||
| "execa": "^8.0.1", | ||
| "find-up": "^7.0.0" | ||
| }, | ||
| "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" | ||
| "@antfu/eslint-config": "^2.0.0", | ||
| "@antfu/ni": "^0.21.10", | ||
| "@types/node": "^20.9.3", | ||
| "bumpp": "^9.2.0", | ||
| "eslint": "^8.54.0", | ||
| "esno": "^4.0.0", | ||
| "tsup": "^8.0.1", | ||
| "typescript": "^5.3.2" | ||
| }, | ||
@@ -48,0 +49,0 @@ "scripts": { |
+1
-1
@@ -5,3 +5,3 @@ # install-pkg | ||
| Install package programmatically. Detect package managers automatically (`npm`, `yarn` and `pnpm`). | ||
| Install package programmatically. Detect package managers automatically (`npm`, `yarn`, `bun` and `pnpm`). | ||
@@ -8,0 +8,0 @@ ```bash |
| // src/detect.ts | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import findUp from "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", | ||
| "npm-shrinkwrap.json": "npm" | ||
| }; | ||
| async function detectPackageManager(cwd = process.cwd()) { | ||
| let agent = null; | ||
| const lockPath = await findUp(Object.keys(LOCKS), { cwd }); | ||
| let packageJsonPath; | ||
| if (lockPath) | ||
| packageJsonPath = path.resolve(lockPath, "../package.json"); | ||
| else | ||
| packageJsonPath = await findUp("package.json", { cwd }); | ||
| if (packageJsonPath && fs.existsSync(packageJsonPath)) { | ||
| try { | ||
| const pkg = JSON.parse(fs.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[path.basename(lockPath)]; | ||
| return agent; | ||
| } | ||
| // src/install.ts | ||
| import execa from "execa"; | ||
| async function installPackage(names, options = {}) { | ||
| 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) { | ||
| if (detectedAgent === "yarn@berry") | ||
| args.unshift("--cached"); | ||
| else | ||
| args.unshift("--prefer-offline"); | ||
| } | ||
| return execa( | ||
| agent, | ||
| [ | ||
| agent === "yarn" ? "add" : "install", | ||
| options.dev ? "-D" : "", | ||
| ...args, | ||
| ...names | ||
| ].filter(Boolean), | ||
| { | ||
| stdio: options.silent ? "ignore" : "inherit", | ||
| cwd: options.cwd | ||
| } | ||
| ); | ||
| } | ||
| export { | ||
| detectPackageManager, | ||
| installPackage | ||
| }; |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
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
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
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
10952
12.74%7
16.67%200
3.09%Yes
NaN+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated