mick-templates 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +28 -2
- package/package.json +1 -1
- package/src/lib/pm.ts +44 -6
package/bin/cli.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import { spawn } from "child_process";
|
|
3
3
|
import { dirname, join } from "path";
|
|
4
4
|
import { fileURLToPath } from "url";
|
|
5
|
+
import { createRequire } from "module";
|
|
6
|
+
import { getUserAgent } from "package-manager-detector";
|
|
5
7
|
|
|
6
8
|
// Get the directory where this script is located
|
|
7
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -9,8 +11,32 @@ const binDir = dirname(__filename);
|
|
|
9
11
|
const projectRoot = join(binDir, "..");
|
|
10
12
|
const cliPath = join(projectRoot, "src", "index.tsx");
|
|
11
13
|
|
|
12
|
-
//
|
|
13
|
-
const
|
|
14
|
+
// Detect the package manager agent
|
|
15
|
+
const agent = getUserAgent() ?? "npm";
|
|
16
|
+
|
|
17
|
+
let command;
|
|
18
|
+
let args;
|
|
19
|
+
|
|
20
|
+
if (agent === "bun") {
|
|
21
|
+
command = "bun";
|
|
22
|
+
args = [cliPath, ...process.argv.slice(2)];
|
|
23
|
+
} else {
|
|
24
|
+
const require = createRequire(import.meta.url);
|
|
25
|
+
try {
|
|
26
|
+
// Try to resolve tsx from the package's dependencies
|
|
27
|
+
const tsxPath = require.resolve("tsx/cli");
|
|
28
|
+
command = process.execPath || "node";
|
|
29
|
+
args = [tsxPath, cliPath, ...process.argv.slice(2)];
|
|
30
|
+
} catch (_err) {
|
|
31
|
+
// Fallback to npx if tsx is not resolvable
|
|
32
|
+
const isWin = process.platform === "win32";
|
|
33
|
+
command = isWin ? "npx.cmd" : "npx";
|
|
34
|
+
args = ["tsx", cliPath, ...process.argv.slice(2)];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Spawn the command
|
|
39
|
+
const child = spawn(command, args, {
|
|
14
40
|
stdio: "inherit",
|
|
15
41
|
cwd: process.cwd(),
|
|
16
42
|
env: process.env,
|
package/package.json
CHANGED
package/src/lib/pm.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { getUserAgent, resolveCommand } from "package-manager-detector";
|
|
2
2
|
import type { AgentName } from "package-manager-detector";
|
|
3
3
|
import { spawn } from "child_process";
|
|
4
|
+
import { createRequire } from "module";
|
|
5
|
+
import { join, dirname } from "path";
|
|
6
|
+
import { readFile } from "fs/promises";
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Get the current package manager agent.
|
|
7
10
|
*/
|
|
8
11
|
export function getAgent(): AgentName {
|
|
9
|
-
|
|
10
|
-
return ua?.name ?? "npm";
|
|
12
|
+
return getUserAgent() ?? "npm";
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
/**
|
|
@@ -16,7 +18,9 @@ export function getAgent(): AgentName {
|
|
|
16
18
|
function run(cmd: string, args: string[], cwd: string): Promise<void> {
|
|
17
19
|
return new Promise((resolve, reject) => {
|
|
18
20
|
spawn(cmd, args, { cwd, stdio: "inherit" })
|
|
19
|
-
.on("close", (code) =>
|
|
21
|
+
.on("close", (code) =>
|
|
22
|
+
code === 0 ? resolve() : reject(new Error(`Exit ${code}`))
|
|
23
|
+
)
|
|
20
24
|
.on("error", reject);
|
|
21
25
|
});
|
|
22
26
|
}
|
|
@@ -28,17 +32,51 @@ export async function install(cwd: string): Promise<void> {
|
|
|
28
32
|
const agent = getAgent();
|
|
29
33
|
const resolved = resolveCommand(agent, "install", []);
|
|
30
34
|
if (!resolved) throw new Error(`Cannot resolve install command for ${agent}`);
|
|
31
|
-
|
|
35
|
+
|
|
32
36
|
await run(resolved.command, resolved.args, cwd);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
/**
|
|
36
40
|
* Execute a package binary (npx/bunx/etc).
|
|
37
41
|
*/
|
|
38
|
-
export async function execute(
|
|
42
|
+
export async function execute(
|
|
43
|
+
cwd: string,
|
|
44
|
+
pkg: string,
|
|
45
|
+
args: string[]
|
|
46
|
+
): Promise<void> {
|
|
39
47
|
const agent = getAgent();
|
|
48
|
+
|
|
49
|
+
if (agent === "bun") {
|
|
50
|
+
await run("bunx", [pkg, ...args], cwd);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Try to resolve the package binary from our own dependencies
|
|
55
|
+
try {
|
|
56
|
+
const require = createRequire(import.meta.url);
|
|
57
|
+
const pkgJsonPath = require.resolve(`${pkg}/package.json`);
|
|
58
|
+
const pkgJson = JSON.parse(await readFile(pkgJsonPath, "utf-8")) as Record<
|
|
59
|
+
string,
|
|
60
|
+
any
|
|
61
|
+
>;
|
|
62
|
+
|
|
63
|
+
let binPath: string | undefined;
|
|
64
|
+
if (typeof pkgJson.bin === "string") {
|
|
65
|
+
binPath = join(dirname(pkgJsonPath), pkgJson.bin);
|
|
66
|
+
} else if (pkgJson.bin && pkgJson.bin[pkg]) {
|
|
67
|
+
binPath = join(dirname(pkgJsonPath), pkgJson.bin[pkg]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (binPath) {
|
|
71
|
+
await run("node", [binPath, ...args], cwd);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
} catch (_err) {
|
|
75
|
+
// Fallback to global execute
|
|
76
|
+
}
|
|
77
|
+
|
|
40
78
|
const resolved = resolveCommand(agent, "execute", [pkg, ...args]);
|
|
41
79
|
if (!resolved) throw new Error(`Cannot resolve execute command for ${agent}`);
|
|
42
|
-
|
|
80
|
+
|
|
43
81
|
await run(resolved.command, resolved.args, cwd);
|
|
44
82
|
}
|