@aptove/bridge 0.1.11 → 0.1.12

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/package.json +6 -6
  2. package/postinstall.js +98 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptove/bridge",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "ACP bridge — connects ACP agents to mobile and desktop clients over WebSocket",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,10 +33,10 @@
33
33
  "node": ">=16"
34
34
  },
35
35
  "optionalDependencies": {
36
- "@aptove/bridge-darwin-arm64": "0.1.11",
37
- "@aptove/bridge-darwin-x64": "0.1.11",
38
- "@aptove/bridge-linux-arm64": "0.1.11",
39
- "@aptove/bridge-linux-x64": "0.1.11",
40
- "@aptove/bridge-win32-x64": "0.1.11"
36
+ "@aptove/bridge-darwin-arm64": "0.1.12",
37
+ "@aptove/bridge-darwin-x64": "0.1.12",
38
+ "@aptove/bridge-linux-arm64": "0.1.12",
39
+ "@aptove/bridge-linux-x64": "0.1.12",
40
+ "@aptove/bridge-win32-x64": "0.1.12"
41
41
  }
42
42
  }
package/postinstall.js CHANGED
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * bridge postinstall script
3
3
  *
4
- * Checks whether the platform-specific binary package was installed.
5
- * npm sometimes skips optional dependencies during global installs.
6
- * If so, prints the exact command to complete installation.
4
+ * Ensures the platform-specific binary package is installed at the correct version.
5
+ * npm sometimes skips optional dependencies during global installs, so we detect
6
+ * this and install the platform package explicitly if needed.
7
+ * Validates the final binary by running it with --version.
7
8
  */
8
9
 
10
+ const { execSync, spawnSync } = require('child_process');
9
11
  const path = require('path');
10
12
  const fs = require('fs');
11
13
 
@@ -26,22 +28,101 @@ if (!packageName) {
26
28
  }
27
29
 
28
30
  const binaryName = process.platform === 'win32' ? 'bridge.exe' : 'bridge';
29
- let installed = false;
31
+ const shortName = packageName.split('/')[1]; // e.g. "bridge-darwin-arm64"
30
32
 
31
- try {
32
- const packageJsonPath = require.resolve(`${packageName}/package.json`);
33
- const binaryPath = path.join(path.dirname(packageJsonPath), 'bin', binaryName);
34
- installed = fs.existsSync(binaryPath);
35
- } catch (e) {
36
- // package not installed
33
+ // postinstall.js lives at @aptove/bridge/postinstall.js
34
+ // platform binary lives at @aptove/bridge-darwin-arm64/bin/bridge (sibling package)
35
+ const siblingBinaryPath = path.join(__dirname, '..', shortName, 'bin', binaryName);
36
+
37
+ function isBinaryPresent() {
38
+ return fs.existsSync(siblingBinaryPath);
39
+ }
40
+
41
+ // Returns the version string from `bridge --version` (e.g. "0.1.11"), or null on failure.
42
+ function getBinaryVersion() {
43
+ const result = spawnSync(siblingBinaryPath, ['--version'], { stdio: 'pipe' });
44
+ if (result.error || result.status !== 0) return null;
45
+ const output = (result.stdout || '').toString().trim(); // e.g. "bridge 0.1.11"
46
+ const parts = output.split(' ');
47
+ return parts.length >= 2 ? parts[1] : output;
48
+ }
49
+
50
+ function getExpectedVersion() {
51
+ try {
52
+ const pkg = require('./package.json');
53
+ // optionalDependencies values are updated by the release workflow to match the published version
54
+ const deps = pkg.optionalDependencies || {};
55
+ const versions = Object.values(deps).filter(v => v !== '*');
56
+ return versions[0] || pkg.version;
57
+ } catch (e) {
58
+ return null;
59
+ }
60
+ }
61
+
62
+ function installPlatformPackage(version) {
63
+ // During `npm install -g`, npm_config_prefix points to the global prefix.
64
+ // Installing with --prefix ensures the package lands in the same global node_modules tree.
65
+ const prefix = process.env.npm_config_prefix;
66
+ const prefixFlag = prefix ? `--prefix "${prefix}"` : '';
67
+ console.log(` Installing ${packageName}@${version}...`);
68
+ execSync(
69
+ `npm install ${prefixFlag} --no-save --no-audit --no-fund "${packageName}@${version}"`,
70
+ { stdio: 'inherit' }
71
+ );
72
+ }
73
+
74
+ function tryInstall(version) {
75
+ try {
76
+ installPlatformPackage(version);
77
+ return true;
78
+ } catch (e) {
79
+ return false;
80
+ }
81
+ }
82
+
83
+ // --- Main ---
84
+
85
+ const expectedVersion = getExpectedVersion();
86
+
87
+ // Check if binary is present and at the correct version.
88
+ if (isBinaryPresent()) {
89
+ const installedVersion = getBinaryVersion();
90
+ if (installedVersion && expectedVersion && installedVersion !== expectedVersion) {
91
+ console.log(`⬆ bridge: updating platform binary ${installedVersion} → ${expectedVersion}...`);
92
+ if (!tryInstall(expectedVersion)) {
93
+ console.warn(`⚠️ bridge: update failed — run: npm install -g ${packageName}@${expectedVersion}`);
94
+ process.exit(0);
95
+ }
96
+ } else {
97
+ const version = installedVersion || getBinaryVersion();
98
+ console.log(`✓ bridge ${version || installedVersion} installed successfully for ${platformKey}`);
99
+ process.exit(0);
100
+ }
101
+ } else {
102
+ // Optional dependency was not installed (common with `npm install -g`).
103
+ console.log(`\n⬇ bridge: platform binary not found, installing ${packageName}...`);
104
+ if (!tryInstall(expectedVersion || 'latest')) {
105
+ console.warn(`\n⚠️ bridge: failed to install ${packageName}`);
106
+ console.warn(` Run manually: npm install -g ${packageName}${expectedVersion ? '@' + expectedVersion : ''}`);
107
+ process.exit(0);
108
+ }
37
109
  }
38
110
 
39
- if (installed) {
40
- console.log(`✓ bridge installed successfully for ${platformKey}`);
111
+ // Validate after install using sibling path directly.
112
+ // (require.resolve cannot be used here — Node.js caches negative module
113
+ // resolution results within the same process.)
114
+ if (isBinaryPresent()) {
115
+ const installedVersion = getBinaryVersion();
116
+ if (installedVersion) {
117
+ if (expectedVersion && installedVersion !== expectedVersion) {
118
+ console.warn(`⚠️ bridge: installed ${installedVersion} but expected ${expectedVersion} (may not be published yet)`);
119
+ } else {
120
+ console.log(`✓ bridge ${installedVersion} installed successfully for ${platformKey}`);
121
+ }
122
+ } else {
123
+ console.warn(`⚠️ bridge: binary present but failed to run — try: ${siblingBinaryPath} --version`);
124
+ }
41
125
  } else {
42
- console.log('');
43
- console.log(`⚠️ bridge: platform binary not found (npm skipped optional dependency)`);
44
- console.log(` To complete installation, run:`);
45
- console.log(` npm install -g ${packageName}`);
46
- console.log('');
126
+ console.warn(`\n⚠️ bridge: binary not found after installation`);
127
+ console.warn(` Run manually: npm install -g ${packageName}${expectedVersion ? '@' + expectedVersion : ''}`);
47
128
  }