@leonmw/agw 0.0.0-dev.ad2839736721 → 0.0.0-dev.b9a0f78692b7

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.
Files changed (2) hide show
  1. package/bin/agw.js +113 -9
  2. package/package.json +4 -4
package/bin/agw.js CHANGED
@@ -1,6 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const { spawnSync } = require("node:child_process");
4
+ const fs = require("node:fs");
5
+ const os = require("node:os");
6
+ const path = require("node:path");
7
+
8
+ const MAIN_PACKAGE_NAME = "@leonmw/agw";
4
9
 
5
10
  const platformPackageMap = {
6
11
  "linux-x64": { packageName: "@leonmw/agw-linux-x64", binaryPath: "bin/agw" },
@@ -13,6 +18,65 @@ function getPackageForHost() {
13
18
  return platformPackageMap[key] ?? null;
14
19
  }
15
20
 
21
+ function tryResolveHostBinaryPath(match) {
22
+ const candidates = [];
23
+
24
+ try {
25
+ candidates.push(require.resolve(`${match.packageName}/${match.binaryPath}`));
26
+ } catch {
27
+ // Try fallback locations below.
28
+ }
29
+
30
+ try {
31
+ const pkgJsonPath = require.resolve(`${match.packageName}/package.json`);
32
+ candidates.push(path.join(path.dirname(pkgJsonPath), match.binaryPath));
33
+ } catch {
34
+ // Try fallback locations below.
35
+ }
36
+
37
+ // Nested under @leonmw/agw/node_modules/@leonmw/agw-<platform>
38
+ candidates.push(path.join(__dirname, "..", "node_modules", match.packageName, match.binaryPath));
39
+
40
+ // Hoisted sibling under node_modules/@leonmw/agw-<platform>
41
+ candidates.push(path.join(__dirname, "..", "..", match.packageName, match.binaryPath));
42
+
43
+ for (const candidate of candidates) {
44
+ if (candidate && fs.existsSync(candidate)) {
45
+ return candidate;
46
+ }
47
+ }
48
+
49
+ return null;
50
+ }
51
+
52
+ function runNpmGlobalInstall(packageSpec) {
53
+ const updateResult = spawnSync("npm", ["install", "-g", packageSpec], {
54
+ stdio: "inherit",
55
+ shell: process.platform === "win32"
56
+ });
57
+ if (updateResult.error) {
58
+ throw updateResult.error;
59
+ }
60
+ return updateResult.status ?? 1;
61
+ }
62
+
63
+ function tryHandleMissingBinaryUpdate(args) {
64
+ if (args[0]?.toLowerCase() !== "update") {
65
+ return null;
66
+ }
67
+
68
+ const hasHelp = args.some((a) => a === "--help" || a === "-h" || a === "-?" || a === "/?");
69
+ if (hasHelp) {
70
+ return null;
71
+ }
72
+
73
+ const useDev = args.includes("--dev");
74
+ const distTag = useDev ? "dev" : "latest";
75
+ const packageSpec = `${MAIN_PACKAGE_NAME}@${distTag}`;
76
+ console.log(`Platform binary missing; reinstalling ${packageSpec}...`);
77
+ return runNpmGlobalInstall(packageSpec);
78
+ }
79
+
16
80
  function resolveHostBinaryPath() {
17
81
  const match = getPackageForHost();
18
82
  if (!match) {
@@ -20,25 +84,65 @@ function resolveHostBinaryPath() {
20
84
  throw new Error(`Unsupported platform '${process.platform}-${process.arch}'. Supported: ${supported}.`);
21
85
  }
22
86
 
23
- try {
24
- return require.resolve(`${match.packageName}/${match.binaryPath}`);
25
- } catch {
26
- throw new Error(
27
- `Missing platform binary package '${match.packageName}'. Reinstall with 'npm i -g @leonmw/agw'.`
28
- );
87
+ const resolved = tryResolveHostBinaryPath(match);
88
+ if (resolved) {
89
+ return resolved;
29
90
  }
91
+
92
+ const windowsHint = process.platform === "win32"
93
+ ? " If another 'agw' session is running, close it first (Windows locks agw.exe during npm install)."
94
+ : "";
95
+ throw new Error(
96
+ `Missing platform binary package '${match.packageName}'. Reinstall with 'npm i -g ${MAIN_PACKAGE_NAME}' (or 'agw update --dev').${windowsHint}`
97
+ );
30
98
  }
31
99
 
32
100
  function run() {
33
- const binaryPath = resolveHostBinaryPath();
34
- const result = spawnSync(binaryPath, process.argv.slice(2), {
35
- stdio: "inherit"
101
+ const args = process.argv.slice(2);
102
+ const match = getPackageForHost();
103
+ let binaryPath = match ? tryResolveHostBinaryPath(match) : null;
104
+
105
+ if (!binaryPath) {
106
+ const fallbackStatus = tryHandleMissingBinaryUpdate(args);
107
+ if (fallbackStatus !== null) {
108
+ process.exit(fallbackStatus);
109
+ }
110
+ binaryPath = resolveHostBinaryPath();
111
+ }
112
+
113
+ const handoffFile = process.platform === "win32"
114
+ ? path.join(os.tmpdir(), `agw-update-${process.pid}-${Date.now()}.txt`)
115
+ : null;
116
+ const env = handoffFile
117
+ ? { ...process.env, AGW_UPDATE_HANDOFF_FILE: handoffFile }
118
+ : process.env;
119
+
120
+ const result = spawnSync(binaryPath, args, {
121
+ stdio: "inherit",
122
+ env
36
123
  });
37
124
 
38
125
  if (result.error) {
39
126
  throw result.error;
40
127
  }
41
128
 
129
+ if (handoffFile && fs.existsSync(handoffFile)) {
130
+ let packageSpec = "";
131
+ try {
132
+ packageSpec = fs.readFileSync(handoffFile, "utf8").trim();
133
+ } finally {
134
+ try {
135
+ fs.unlinkSync(handoffFile);
136
+ } catch {
137
+ // Best effort cleanup
138
+ }
139
+ }
140
+
141
+ if (packageSpec) {
142
+ process.exit(runNpmGlobalInstall(packageSpec));
143
+ }
144
+ }
145
+
42
146
  process.exit(result.status ?? 1);
43
147
  }
44
148
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonmw/agw",
3
- "version": "0.0.0-dev.ad2839736721",
3
+ "version": "0.0.0-dev.b9a0f78692b7",
4
4
  "description": "Run Antigravity CLI in Docker with strict isolation, persistent state, and profile management.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -11,9 +11,9 @@
11
11
  "bin/"
12
12
  ],
13
13
  "optionalDependencies": {
14
- "@leonmw/agw-linux-x64": "0.0.0-dev.ad2839736721",
15
- "@leonmw/agw-darwin-arm64": "0.0.0-dev.ad2839736721",
16
- "@leonmw/agw-win32-x64": "0.0.0-dev.ad2839736721"
14
+ "@leonmw/agw-linux-x64": "0.0.0-dev.b9a0f78692b7",
15
+ "@leonmw/agw-darwin-arm64": "0.0.0-dev.b9a0f78692b7",
16
+ "@leonmw/agw-win32-x64": "0.0.0-dev.b9a0f78692b7"
17
17
  },
18
18
  "engines": {
19
19
  "node": ">=18"