mcp-apply-patch 0.1.3 → 0.1.4

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.
Binary file
package/bin/run.js CHANGED
@@ -1,20 +1,59 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const { execFileSync } = require("child_process");
3
+ const { spawnSync } = require("child_process");
4
4
  const path = require("path");
5
5
  const fs = require("fs");
6
+ const os = require("os");
6
7
 
7
- const binName = process.platform === "win32" ? "mcp-apply-patch.exe" : "mcp-apply-patch";
8
- const binPath = path.join(__dirname, "..", "bin", binName);
8
+ const isWindows = process.platform === "win32";
9
+ const cacheBinDir = path.join(os.homedir(), ".mcp-apply-patch", "bin");
10
+ const binName = isWindows ? "apply-patch-mcp.exe" : "apply-patch-mcp";
11
+ const cachedBinPath = path.join(cacheBinDir, binName);
9
12
 
10
- if (!fs.existsSync(binPath)) {
11
- console.error("Binary not found. Run 'npm install' to download it.");
12
- process.exit(1);
13
+ function spawnBinary(binPath) {
14
+ const child = require("child_process").spawn(binPath, process.argv.slice(2), { stdio: "inherit" });
15
+ child.on("exit", (code) => process.exit(code ?? 1));
13
16
  }
14
17
 
15
- // Pass through stdio for MCP protocol
16
- const result = require("child_process").spawn(binPath, process.argv.slice(2), {
17
- stdio: "inherit",
18
- });
18
+ (async function main() {
19
+ try {
20
+ if (fs.existsSync(cachedBinPath)) {
21
+ spawnBinary(cachedBinPath);
22
+ return;
23
+ }
19
24
 
20
- result.on("exit", (code) => process.exit(code ?? 1));
25
+ // Fallback: try local node_modules bin path (for installed package)
26
+ const localBin = path.join(__dirname, "..", "bin", binName);
27
+ if (fs.existsSync(localBin)) {
28
+ spawnBinary(localBin);
29
+ return;
30
+ }
31
+
32
+ // If missing, run installer synchronously
33
+ console.error("Binary not found in cache. Installing to global cache...");
34
+ const installer = path.join(__dirname, "..", "scripts", "install.js");
35
+
36
+ try {
37
+ // Run installer as a child process and wait for it to complete
38
+ const res = spawnSync(process.execPath, [installer], { stdio: ["inherit", process.stderr.fd, "inherit"] });
39
+ if (res.status !== 0) {
40
+ console.error("Installer failed");
41
+ process.exit(res.status || 1);
42
+ }
43
+ } catch (e) {
44
+ console.error("Failed to run installer:", e);
45
+ process.exit(1);
46
+ }
47
+
48
+ if (fs.existsSync(cachedBinPath)) {
49
+ spawnBinary(cachedBinPath);
50
+ return;
51
+ }
52
+
53
+ console.error("Binary installation completed but binary not found.");
54
+ process.exit(1);
55
+ } catch (err) {
56
+ console.error(err);
57
+ process.exit(1);
58
+ }
59
+ })();
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "mcp-apply-patch",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "MCP server for structured file patching using V4A diffs. Create, update, and delete files in one atomic operation.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/_blitzzz/mcp-apply-patch"
8
+ "url": "https://github.com/tuanhung303/apply-patch-mcp"
9
9
  },
10
10
  "bin": {
11
11
  "mcp-apply-patch": "bin/run.js"
@@ -15,9 +15,7 @@
15
15
  "scripts/",
16
16
  "README.md"
17
17
  ],
18
- "scripts": {
19
- "postinstall": "node scripts/install.js"
20
- },
18
+ "scripts": {},
21
19
  "os": [
22
20
  "darwin",
23
21
  "linux",
@@ -46,49 +46,55 @@ function download(url) {
46
46
  });
47
47
  }
48
48
 
49
- async function main() {
49
+ async function doInstall() {
50
50
  const platformKey = getPlatformKey();
51
51
  const isWindows = process.platform === "win32";
52
52
  const ext = isWindows ? ".zip" : ".tar.gz";
53
53
  const assetName = `apply-patch-mcp-v${VERSION}-${platformKey}${ext}`;
54
54
  const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${assetName}`;
55
55
 
56
- const binDir = path.join(__dirname, "..", "bin");
56
+ const cacheBinDir = path.join(os.homedir(), ".mcp-apply-patch", "bin");
57
57
  const binName = isWindows ? "apply-patch-mcp.exe" : "apply-patch-mcp";
58
- const binPath = path.join(binDir, binName);
58
+ const binPath = path.join(cacheBinDir, binName);
59
59
 
60
- // Skip if binary already exists
60
+ // Skip if already in global cache
61
61
  if (fs.existsSync(binPath)) {
62
- console.log("apply-patch-mcp binary already installed.");
63
- return;
62
+ console.error(`apply-patch-mcp binary already installed at ${binPath}.`);
63
+ return binPath;
64
64
  }
65
65
 
66
- console.log(`Downloading apply-patch-mcp v${VERSION} for ${platformKey}...`);
66
+ console.error(`Downloading apply-patch-mcp v${VERSION} for ${platformKey}...`);
67
67
 
68
68
  try {
69
69
  const data = await download(url);
70
70
  const tmpFile = path.join(os.tmpdir(), assetName);
71
71
  fs.writeFileSync(tmpFile, data);
72
72
 
73
- fs.mkdirSync(binDir, { recursive: true });
73
+ fs.mkdirSync(cacheBinDir, { recursive: true });
74
74
 
75
75
  if (isWindows) {
76
76
  // For Windows, use PowerShell to extract zip
77
- execSync(`powershell -Command "Expand-Archive -Path '${tmpFile}' -DestinationPath '${binDir}' -Force"`, { stdio: "inherit" });
77
+ execSync(`powershell -Command "Expand-Archive -Path '${tmpFile}' -DestinationPath '${cacheBinDir}' -Force"`, { stdio: "inherit" });
78
78
  } else {
79
- execSync(`tar xzf "${tmpFile}" -C "${binDir}"`, { stdio: "inherit" });
79
+ execSync(`tar xzf "${tmpFile}" -C "${cacheBinDir}"`, { stdio: "inherit" });
80
80
  }
81
81
 
82
- fs.chmodSync(binPath, 0o755);
83
- fs.unlinkSync(tmpFile);
82
+ // Ensure executable permissions
83
+ try { fs.chmodSync(binPath, 0o755); } catch (e) { /* ignore */ }
84
+ try { fs.unlinkSync(tmpFile); } catch (e) { /* ignore */ }
84
85
 
85
- console.log(`apply-patch-mcp v${VERSION} installed successfully.`);
86
+ console.error(`apply-patch-mcp v${VERSION} installed to ${binPath}.`);
87
+ return binPath;
86
88
  } catch (err) {
87
89
  console.error(`Failed to download binary: ${err.message}`);
88
90
  console.error(`URL: ${url}`);
89
91
  console.error("You can build from source: cargo build --release");
90
- process.exit(1);
92
+ throw err;
91
93
  }
92
94
  }
93
95
 
94
- main();
96
+ module.exports = { doInstall };
97
+
98
+ if (require.main === module) {
99
+ doInstall().catch((e) => process.exit(1));
100
+ }