package-manager-manager
Advanced tools
Comparing version 0.0.0-1dd7c4b to 0.0.0-1f2f2a2
type CommandStructBase = { | ||
/** | ||
* Package Manager specific keywords that representing the command to run | ||
* (e.g. `['pnpm', 'exec']`, `['yarn', 'dlx']`, etc...) | ||
* Base command to run (`npm`, `npx`, `yarn`, `pnpm`, `bun` or `bunx`) | ||
*/ | ||
pmKeywords: string[]; | ||
cmd: string; | ||
/** | ||
* All the arguments to provide to the base command. | ||
* | ||
* Note: this comprises everything except the base command, like the package manager | ||
* specific command, its arguments, potential double-dashes, etc... | ||
* | ||
*/ | ||
cmdArgs: string[]; | ||
/** | ||
* Package Manager specific keyword that representing the command to run (if any) | ||
* (e.g. `run`, `exec`, `dlx`, etc...) | ||
*/ | ||
pmCmd?: string; | ||
/** | ||
* The arguments passed to the target command | ||
* (e.g. `['--help']`, `['./dist']`, `['--ignore-path', '.gitignore']`, etc...) | ||
*/ | ||
args: string[]; | ||
targetArgs: string[]; | ||
/** | ||
@@ -24,6 +36,6 @@ * Flag indicating whether double dashes need to prepend the command's arguments. | ||
/** | ||
* The command (from some dependency) the object is targeting | ||
* The package specific command (from some dependency) the object is targeting | ||
* (e.g. `'eslint'`, `'esbuild'`, `'prettier'`, etc...) | ||
*/ | ||
command: string; | ||
pkgCmd: string; | ||
}; | ||
@@ -30,0 +42,0 @@ export type CommandScriptStruct = CommandStructBase & { |
@@ -1,11 +0,11 @@ | ||
var b = (r, e, t) => { | ||
var w = (r, e, s) => { | ||
if (!e.has(r)) | ||
throw TypeError("Cannot " + t); | ||
throw TypeError("Cannot " + s); | ||
}; | ||
var d = (r, e, t) => { | ||
var u = (r, e, s) => { | ||
if (e.has(r)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
e instanceof WeakSet ? e.add(r) : e.set(r, t); | ||
e instanceof WeakSet ? e.add(r) : e.set(r, s); | ||
}; | ||
var l = (r, e, t) => (b(r, e, "access private method"), t); | ||
var l = (r, e, s) => (w(r, e, "access private method"), s); | ||
import "node:fs/promises"; | ||
@@ -15,51 +15,61 @@ import "node:path"; | ||
import "../utils/locks.js"; | ||
import { isYarnClassic as D } from "../utils/yarn.js"; | ||
var a, y, m, f; | ||
import { isYarnClassic as b } from "../utils/yarn.js"; | ||
var i, p, n, f; | ||
const h = class { | ||
constructor(e, t, s) { | ||
this.command = t, this.pmKeywords = [], this.args = (s == null ? void 0 : s.args) ?? []; | ||
const c = (s == null ? void 0 : s.format) ?? "short", n = (s == null ? void 0 : s.download) ?? "prefer-always"; | ||
this.structIsReady = new Promise((u) => { | ||
var o; | ||
l(o = h, a, y).call(o, e, t, c, n).then((w) => { | ||
var x; | ||
this.pmKeywords = w, ["yarn", "pnpm"].includes(e.name) && w[1] === "exec" && (this.command = l(x = h, m, f).call(x, this.command)), e.name === "npm" && this.pmKeywords[1] === "exec" && (this.argsNeedDoubleDashes = !0), u(); | ||
}); | ||
}), this.argsNeedDoubleDashes = D(e); | ||
constructor(e, s, t) { | ||
this.pkgCmd = s, this.pmCmd = void 0, this.targetArgs = (t == null ? void 0 : t.args) ?? []; | ||
const c = (t == null ? void 0 : t.format) ?? "short", m = (t == null ? void 0 : t.download) ?? "prefer-always"; | ||
this.cmd = e.name, this.structIsReady = new Promise((a) => { | ||
var g; | ||
l(g = h, i, p).call(g, e, s, c, m).then( | ||
({ cmd: o, pmCmd: C }) => { | ||
var x; | ||
this.cmd = o, this.pmCmd = C, ["yarn", "pnpm"].includes(e.name) && C === "exec" && (this.pkgCmd = l(x = h, n, f).call(x, this.pkgCmd)), e.name === "npm" && this.pmCmd === "exec" && (this.argsNeedDoubleDashes = !0), a(); | ||
} | ||
); | ||
}), this.argsNeedDoubleDashes = b(e); | ||
} | ||
get cmdArgs() { | ||
return [ | ||
...this.pmCmd ? [this.pmCmd] : [], | ||
this.pkgCmd, | ||
...this.targetArgs.length && this.argsNeedDoubleDashes ? ["--"] : [], | ||
...this.targetArgs | ||
]; | ||
} | ||
toString() { | ||
return `${this.pmKeywords.join(" ")} ${this.command}${this.args.length ? `${this.argsNeedDoubleDashes ? " --" : ""} ${this.args.join(" ")}` : ""}`; | ||
return `${this.cmd}${this.pmCmd ? ` ${this.pmCmd}` : ""} ${this.pkgCmd}${this.targetArgs.length ? `${this.argsNeedDoubleDashes ? " --" : ""} ${this.targetArgs.join(" ")}` : ""}`; | ||
} | ||
}; | ||
let i = h; | ||
a = new WeakSet(), y = async function(e, t, s, c) { | ||
let d = h; | ||
i = new WeakSet(), p = async function(e, s, t, c) { | ||
switch (e.name) { | ||
case "bun": | ||
return s === "short" ? ["bunx"] : ["bun", "x"]; | ||
return t === "short" ? { cmd: "bunx" } : { cmd: "bun", pmCmd: "x" }; | ||
case "npm": | ||
return s === "short" ? ["npx"] : ["npm", "exec"]; | ||
return t === "short" ? { cmd: "npx" } : { cmd: "npm", pmCmd: "exec" }; | ||
case "yarn": | ||
if (e.version.startsWith("1.")) | ||
return ["yarn", "exec"]; | ||
return { cmd: "yarn", pmCmd: "exec" }; | ||
break; | ||
} | ||
let n = []; | ||
const m = { cmd: e.name }; | ||
switch (c) { | ||
case "prefer-always": | ||
n = [e.name, "dlx"]; | ||
m.pmCmd = "dlx"; | ||
break; | ||
case "prefer-never": | ||
n = [e.name, "exec"]; | ||
m.pmCmd = "exec"; | ||
break; | ||
case "prefer-if-needed": { | ||
const u = await e.getPackageInfo(t); | ||
n = [e.name, u ? "exec" : "dlx"]; | ||
const a = await e.getPackageInfo(s); | ||
m.pmCmd = a ? "exec" : "dlx"; | ||
break; | ||
} | ||
} | ||
return n; | ||
}, m = new WeakSet(), f = function(e) { | ||
const t = e.match(/^@[^/]+\/(.*)/); | ||
return (t == null ? void 0 : t[1]) ?? e; | ||
}, d(i, a), /** | ||
return m; | ||
}, n = new WeakSet(), f = function(e) { | ||
const s = e.match(/^@[^/]+\/(.*)/); | ||
return (s == null ? void 0 : s[1]) ?? e; | ||
}, u(d, i), /** | ||
* Unscopes a given command, for example it converts "@org/my-cmd" to "my-cmd" | ||
@@ -71,18 +81,18 @@ * (already non scoped commands are left untouched) | ||
*/ | ||
d(i, m); | ||
function g(r) { | ||
const e = async (s, c) => { | ||
if (!s) | ||
u(d, n); | ||
function k(r) { | ||
const e = async (t, c) => { | ||
if (!t) | ||
return null; | ||
const n = new i(r, s, c); | ||
return await n.structIsReady, n; | ||
const m = new d(r, t, c); | ||
return await m.structIsReady, m; | ||
}; | ||
return { getRunExec: async (...s) => { | ||
return { getRunExec: async (...t) => { | ||
var c; | ||
return ((c = await e(...s)) == null ? void 0 : c.toString()) ?? null; | ||
return ((c = await e(...t)) == null ? void 0 : c.toString()) ?? null; | ||
}, getRunExecStruct: e }; | ||
} | ||
export { | ||
g as getRunExecFunctions | ||
k as getRunExecFunctions | ||
}; | ||
//# sourceMappingURL=getRunExec.js.map |
@@ -1,37 +0,45 @@ | ||
var o = (s, r, e) => { | ||
if (!r.has(s)) | ||
var g = (s, t, e) => { | ||
if (!t.has(s)) | ||
throw TypeError("Cannot " + e); | ||
}; | ||
var d = (s, r, e) => { | ||
if (r.has(s)) | ||
var l = (s, t, e) => { | ||
if (t.has(s)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
r instanceof WeakSet ? r.add(s) : r.set(s, e); | ||
t instanceof WeakSet ? t.add(s) : t.set(s, e); | ||
}; | ||
var c = (s, r, e) => (o(s, r, "access private method"), e); | ||
var i, f; | ||
const l = class { | ||
constructor(r, e, t) { | ||
var h; | ||
this.script = e, this.args = (t == null ? void 0 : t.args) ?? []; | ||
const n = (t == null ? void 0 : t.format) ?? "short", m = c(h = l, i, f).call(h, r, e, n); | ||
this.pmKeywords = [r.name, ...m ? ["run"] : []], this.argsNeedDoubleDashes = ["npm", "bun"].includes(r.name); | ||
var m = (s, t, e) => (g(s, t, "access private method"), e); | ||
var u, c; | ||
const h = class { | ||
constructor(t, e, r) { | ||
var d; | ||
this.script = e, this.pmCmd = void 0, this.targetArgs = (r == null ? void 0 : r.args) ?? []; | ||
const i = (r == null ? void 0 : r.format) ?? "short"; | ||
this.cmd = t.name, m(d = h, u, c).call(d, t, e, i) && (this.pmCmd = "run"), this.argsNeedDoubleDashes = ["npm", "bun"].includes(t.name); | ||
} | ||
get cmdArgs() { | ||
return [ | ||
...this.pmCmd ? [this.pmCmd] : [], | ||
this.script, | ||
...this.targetArgs.length && this.argsNeedDoubleDashes ? ["--"] : [], | ||
...this.targetArgs | ||
]; | ||
} | ||
toString() { | ||
return `${this.pmKeywords.join(" ")} ${this.script}${this.args.length ? `${this.argsNeedDoubleDashes ? " --" : ""} ${this.args.join(" ")}` : ""}`; | ||
return `${this.cmd}${this.pmCmd ? ` ${this.pmCmd}` : ""} ${this.script}${this.targetArgs.length ? `${this.argsNeedDoubleDashes ? " --" : ""} ${this.targetArgs.join(" ")}` : ""}`; | ||
} | ||
}; | ||
let u = l; | ||
i = new WeakSet(), f = function(r, e, t) { | ||
return t === "full" ? !0 : e === "start" ? !1 : !!(r.name === "npm" || r.cliCommandKeywords.has(e)); | ||
}, d(u, i); | ||
function g(s) { | ||
const r = (t, n) => t ? new u(s, t, n) : null; | ||
return { getRunScript: (...t) => { | ||
var n; | ||
return ((n = r(...t)) == null ? void 0 : n.toString()) ?? null; | ||
}, getRunScriptStruct: r }; | ||
let n = h; | ||
u = new WeakSet(), c = function(t, e, r) { | ||
return r === "full" ? !0 : e === "start" ? !1 : !!(t.name === "npm" || t.cliCommandKeywords.has(e)); | ||
}, l(n, u); | ||
function C(s) { | ||
const t = (r, i) => r ? new n(s, r, i) : null; | ||
return { getRunScript: (...r) => { | ||
var i; | ||
return ((i = t(...r)) == null ? void 0 : i.toString()) ?? null; | ||
}, getRunScriptStruct: t }; | ||
} | ||
export { | ||
g as getRunScriptFunctions | ||
C as getRunScriptFunctions | ||
}; | ||
//# sourceMappingURL=getRunScript.js.map |
export { getPackageManager } from './packageManager'; | ||
export type { PackageManager, PackageManagerName } from './packageManager'; | ||
//# sourceMappingURL=index.d.ts.map |
{ | ||
"name": "package-manager-manager", | ||
"version": "0.0.0-1dd7c4b", | ||
"version": "0.0.0-1f2f2a2", | ||
"description": "Utilities for managing package managers.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -47,2 +47,80 @@ <p align="center"> | ||
> **Note** | ||
> Note that this library comes with properly defined and documented typescript types, meaning that once you obtain the `PackageManager` object you will be able to easily see what's available on it and get all necessary details directly in your IDE | ||
> This library comes with properly defined and documented typescript types, meaning that once you obtain the `PackageManager` object you will be able to easily see what's available on it and get all necessary details directly in your IDE | ||
### API | ||
### getPackageInfo | ||
`packageManager.getPackageInfo` allows you to get the information regarding a locally installed package that your client application is using, it can for example be used to make sure your user's application has a certain dependency or to gather and display the package version of such dependency. | ||
For example: | ||
```js | ||
const zodPackage = await packageManager.getPackageInfo('zod'); | ||
if (zodPackage) { | ||
console.log(`starting validation using zod (version: ${zodPackage.version}`); | ||
} else { | ||
throw new Error('Error: zod is not installed'); | ||
} | ||
``` | ||
> **Note** | ||
> This method only returns the information of a **locally installed package**, or _null_ in case the package is not installed, it does not return information of packages not locally installed (the API could be extended in the future to also include such use case) | ||
### getRunScript | ||
`packageManager.getRunScript` let's you create a command that can be used to run a script defined in the package.json file. | ||
For example: | ||
```js | ||
const buildStr = packageManager.getRunScript('build', { | ||
args: ['./dist', '--verbose'], | ||
}); | ||
console.log(`To build your application run: ${buildStr}`); | ||
``` | ||
If you need more fine grained control over the command you can use its `packageManager.getRunScriptStruct` alternative to obtain an object representing the command. | ||
For example: | ||
```js | ||
import { spawn } from 'child_process'; | ||
const buildCmd = packageManager.getRunScriptStruct('build', { | ||
args: ['./dist', '--verbose'], | ||
}); | ||
// run the command for the user | ||
spawn(buildCmd.cmd, buildCmd.cmdArgs); | ||
``` | ||
### getRunExec | ||
`packageManager.getRunExec` let's you create a command that can be used to execute a command from a target package (which may or may not be locally installed). | ||
For example: | ||
```js | ||
const eslintStr = packageManager.getRunExec('eslint', { | ||
args: ['./src', '--quiet'], | ||
}); | ||
console.log(`To run eslint on your application run: ${eslintStr}`); | ||
``` | ||
If you need more fine grained control over the command you can use its `packageManager.getRunExecStruct` alternative to obtain an object representing the command. | ||
For example: | ||
```js | ||
import { spawn } from 'child_process'; | ||
const eslintCmd = packageManager.getRunExec('eslint', { | ||
args: ['./src', '--quiet'], | ||
}); | ||
// run the command for the user | ||
spawn(eslintCmd.cmd, eslintCmd.cmdArgs); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
124615
918
126
0