@cydm/magic-shell-agent-node 0.1.11 → 0.1.13
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/dist/adapters/pty-adapter.js +64 -12
- package/dist/plugin-loader.js +1 -1
- package/package.json +1 -1
|
@@ -1,15 +1,53 @@
|
|
|
1
1
|
import { spawn } from "node-pty";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import {
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import path, { dirname } from "node:path";
|
|
4
5
|
import { fileURLToPath } from "url";
|
|
5
6
|
const __filename = fileURLToPath(import.meta.url);
|
|
6
7
|
const __dirname = dirname(__filename);
|
|
7
|
-
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const DEFAULT_WINDOWS_SHELL = process.env.ComSpec || "C:\\Windows\\System32\\cmd.exe";
|
|
10
|
+
function quoteWindowsArg(value) {
|
|
11
|
+
if (!value)
|
|
12
|
+
return "\"\"";
|
|
13
|
+
if (!/[\s"]/u.test(value))
|
|
14
|
+
return value;
|
|
15
|
+
return `"${value.replace(/"/g, '\\"')}"`;
|
|
16
|
+
}
|
|
17
|
+
function resolvePackageBinScript(packageName, binName) {
|
|
18
|
+
try {
|
|
19
|
+
const packageJsonPath = require.resolve(`${packageName}/package.json`);
|
|
20
|
+
const packageDir = path.dirname(packageJsonPath);
|
|
21
|
+
const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
22
|
+
const binField = typeof pkg.bin === "string"
|
|
23
|
+
? pkg.bin
|
|
24
|
+
: pkg.bin && typeof pkg.bin[binName] === "string"
|
|
25
|
+
? pkg.bin[binName]
|
|
26
|
+
: null;
|
|
27
|
+
if (!binField)
|
|
28
|
+
return null;
|
|
29
|
+
const entry = path.resolve(packageDir, binField);
|
|
30
|
+
return existsSync(entry) ? entry : null;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function resolveWindowsCommand(command, args) {
|
|
8
37
|
if (!command || process.platform !== "win32") {
|
|
9
|
-
return command;
|
|
38
|
+
return { command, args };
|
|
39
|
+
}
|
|
40
|
+
if (command === "pie") {
|
|
41
|
+
const pieEntry = resolvePackageBinScript("@cydm/pie", "pie");
|
|
42
|
+
if (pieEntry) {
|
|
43
|
+
return {
|
|
44
|
+
command: process.execPath,
|
|
45
|
+
args: [pieEntry, ...args],
|
|
46
|
+
};
|
|
47
|
+
}
|
|
10
48
|
}
|
|
11
49
|
if (command.includes("\\") || command.includes("/") || /^[a-zA-Z]:/.test(command)) {
|
|
12
|
-
return command;
|
|
50
|
+
return { command, args };
|
|
13
51
|
}
|
|
14
52
|
const pathEntries = (process.env.PATH || "").split(";").filter(Boolean);
|
|
15
53
|
const pathext = (process.env.PATHEXT || ".COM;.EXE;.BAT;.CMD")
|
|
@@ -19,20 +57,34 @@ function resolveExecutableCommand(command) {
|
|
|
19
57
|
for (const entry of pathEntries) {
|
|
20
58
|
const bareCandidate = `${entry}\\${command}`;
|
|
21
59
|
if (existsSync(bareCandidate)) {
|
|
22
|
-
return bareCandidate;
|
|
60
|
+
return { command: bareCandidate, args };
|
|
23
61
|
}
|
|
24
62
|
for (const ext of pathext) {
|
|
25
63
|
const candidate = `${entry}\\${command}${ext.toLowerCase()}`;
|
|
26
64
|
if (existsSync(candidate)) {
|
|
27
|
-
|
|
65
|
+
const lowerExt = ext.toLowerCase();
|
|
66
|
+
if (lowerExt === ".cmd" || lowerExt === ".bat") {
|
|
67
|
+
return {
|
|
68
|
+
command: DEFAULT_WINDOWS_SHELL,
|
|
69
|
+
args: ["/d", "/s", "/c", [quoteWindowsArg(candidate), ...args.map(quoteWindowsArg)].join(" ")],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return { command: candidate, args };
|
|
28
73
|
}
|
|
29
74
|
const candidateUpper = `${entry}\\${command}${ext.toUpperCase()}`;
|
|
30
75
|
if (existsSync(candidateUpper)) {
|
|
31
|
-
|
|
76
|
+
const upperExt = ext.toUpperCase();
|
|
77
|
+
if (upperExt === ".CMD" || upperExt === ".BAT") {
|
|
78
|
+
return {
|
|
79
|
+
command: DEFAULT_WINDOWS_SHELL,
|
|
80
|
+
args: ["/d", "/s", "/c", [quoteWindowsArg(candidateUpper), ...args.map(quoteWindowsArg)].join(" ")],
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return { command: candidateUpper, args };
|
|
32
84
|
}
|
|
33
85
|
}
|
|
34
86
|
}
|
|
35
|
-
return command;
|
|
87
|
+
return { command, args };
|
|
36
88
|
}
|
|
37
89
|
export class PtyAdapter {
|
|
38
90
|
name = "pty";
|
|
@@ -47,8 +99,8 @@ export class PtyAdapter {
|
|
|
47
99
|
}
|
|
48
100
|
async start(config) {
|
|
49
101
|
const agentId = `pty-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
50
|
-
const
|
|
51
|
-
const pty = spawn(
|
|
102
|
+
const resolved = resolveWindowsCommand(config.command, config.args || []);
|
|
103
|
+
const pty = spawn(resolved.command, resolved.args, {
|
|
52
104
|
name: "xterm-256color",
|
|
53
105
|
cols: 80,
|
|
54
106
|
rows: 24,
|
|
@@ -86,7 +138,7 @@ export class PtyAdapter {
|
|
|
86
138
|
this.agents.delete(agentId);
|
|
87
139
|
});
|
|
88
140
|
this.agents.set(agentId, agent);
|
|
89
|
-
console.log(`[PtyAdapter] Started ${agentId}: ${
|
|
141
|
+
console.log(`[PtyAdapter] Started ${agentId}: ${resolved.command}`);
|
|
90
142
|
return agentId;
|
|
91
143
|
}
|
|
92
144
|
async stop(agentId) {
|
package/dist/plugin-loader.js
CHANGED
|
@@ -8,7 +8,7 @@ export function loadPlugin(path) {
|
|
|
8
8
|
if (!existsSync(path)) {
|
|
9
9
|
throw new Error(`Plugin file not found: ${path}`);
|
|
10
10
|
}
|
|
11
|
-
const content = readFileSync(path, "utf-8");
|
|
11
|
+
const content = readFileSync(path, "utf-8").replace(/^\uFEFF/, "");
|
|
12
12
|
const config = JSON.parse(content);
|
|
13
13
|
const configDir = dirname(path);
|
|
14
14
|
const repoRoot = dirname(configDir);
|