agent-notify 0.2.4 → 0.2.5

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.
@@ -6,58 +6,26 @@ const path = require('path');
6
6
  const os = require('os');
7
7
  const fs = require('fs');
8
8
 
9
- function getBinaryName() {
10
- return os.platform() === 'win32' ? 'agent-notify.exe' : 'agent-notify';
11
- }
12
-
13
- function getBinaryPath() {
14
- const binaryName = getBinaryName();
15
- const searchPaths = [];
16
-
17
- // 1. npm global bin directory
9
+ function getBinaryDir() {
10
+ // Try to get npm global bin directory
11
+ // npm bin -g is deprecated, use npm prefix -g instead
18
12
  try {
19
13
  const prefix = execSync('npm prefix -g', { encoding: 'utf8' }).trim();
20
- if (os.platform() === 'win32') {
21
- searchPaths.push(prefix);
22
- } else {
23
- searchPaths.push(path.join(prefix, 'bin'));
24
- }
14
+ return path.join(prefix, 'bin');
25
15
  } catch (e) {
26
- // npm not available
27
- }
28
-
29
- // 2. GOPATH/bin
30
- if (process.env.GOPATH) {
31
- searchPaths.push(path.join(process.env.GOPATH, 'bin'));
16
+ // Fallback to ~/.local/bin
17
+ return path.join(os.homedir(), '.local', 'bin');
32
18
  }
19
+ }
33
20
 
34
- // 3. HOME/go/bin (default GOPATH)
35
- searchPaths.push(path.join(os.homedir(), 'go', 'bin'));
36
-
37
- // 4. ~/.local/bin
38
- searchPaths.push(path.join(os.homedir(), '.local', 'bin'));
39
-
40
- // 5. Search in PATH
41
- try {
42
- const whichCmd = os.platform() === 'win32' ? 'where' : 'which';
43
- const result = execSync(`${whichCmd} ${binaryName}`, { encoding: 'utf8' }).trim();
44
- if (result) {
45
- return result.split('\n')[0].trim();
46
- }
47
- } catch (e) {
48
- // Not found in PATH
49
- }
21
+ const BINARY_DIR = getBinaryDir();
50
22
 
51
- // Return first existing path or first search path as default
52
- for (const searchPath of searchPaths) {
53
- const binaryPath = path.join(searchPath, binaryName);
54
- if (fs.existsSync(binaryPath)) {
55
- return binaryPath;
56
- }
57
- }
23
+ function getBinaryName() {
24
+ return os.platform() === 'win32' ? 'agent-notify.exe' : 'agent-notify';
25
+ }
58
26
 
59
- // Fallback to npm global bin
60
- return path.join(searchPaths[0] || path.join(os.homedir(), '.local', 'bin'), binaryName);
27
+ function getBinaryPath() {
28
+ return path.join(BINARY_DIR, getBinaryName());
61
29
  }
62
30
 
63
31
  function run() {
@@ -65,11 +33,7 @@ function run() {
65
33
 
66
34
  if (!fs.existsSync(binaryPath)) {
67
35
  console.error(`Binary not found at ${binaryPath}`);
68
- console.error('');
69
- console.error('Please install agent-notify first:');
70
- console.error(' npx agent-notify # auto-download');
71
- console.error(' or');
72
- console.error(' go install github.com/hellolib/agent-notify/cmd/agent-notify@latest');
36
+ console.error('Please run: npm install or npx @hellolib/agent-notify');
73
37
  process.exit(1);
74
38
  }
75
39
 
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "agent-notify",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
+ "agentnotifyversion": "0.2.4",
4
5
  "description": "Agent notification tool with Feishu integration",
5
6
  "bin": {
6
7
  "agent-notify": "./bin/agent-notify.js"
@@ -7,9 +7,43 @@ const path = require('path');
7
7
  const os = require('os');
8
8
  const { execSync } = require('child_process');
9
9
 
10
- const PACKAGE_VERSION = require('../package.json').version;
10
+ const PACKAGE_VERSION = require('../package.json').agentnotifyversion;
11
11
  const GITHUB_REPO = 'hellolib/agent-notify';
12
12
 
13
+ // Possible locations where agent-notify might be installed
14
+ function getCommonBinaryDirs() {
15
+ const dirs = [];
16
+ const binaryName = os.platform() === 'win32' ? 'agent-notify.exe' : 'agent-notify';
17
+
18
+ // GOPATH/bin
19
+ if (process.env.GOPATH) {
20
+ dirs.push(path.join(process.env.GOPATH, 'bin'));
21
+ }
22
+ // GOBIN
23
+ if (process.env.GOBIN) {
24
+ dirs.push(process.env.GOBIN);
25
+ }
26
+ // Default Go bin directory
27
+ dirs.push(path.join(os.homedir(), 'go', 'bin'));
28
+ // ~/.local/bin
29
+ dirs.push(path.join(os.homedir(), '.local', 'bin'));
30
+ // /usr/local/bin
31
+ dirs.push('/usr/local/bin');
32
+
33
+ return dirs;
34
+ }
35
+
36
+ function findExistingBinary(binaryName) {
37
+ const dirs = getCommonBinaryDirs();
38
+ for (const dir of dirs) {
39
+ const binaryPath = path.join(dir, binaryName);
40
+ if (fs.existsSync(binaryPath)) {
41
+ return binaryPath;
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+
13
47
  function getBinaryDir() {
14
48
  // Try to get npm global bin directory
15
49
  // npm bin -g is deprecated, use npm prefix -g instead
@@ -108,7 +142,7 @@ async function install() {
108
142
  const localBinaryName = getLocalBinaryName();
109
143
  const binaryPath = path.join(BINARY_DIR, localBinaryName);
110
144
 
111
- // Check if binary already exists with correct version
145
+ // Check if binary already exists in npm bin directory with correct version
112
146
  if (fs.existsSync(binaryPath)) {
113
147
  const installedVersion = getInstalledVersion(binaryPath);
114
148
  if (installedVersion === PACKAGE_VERSION) {
@@ -116,6 +150,34 @@ async function install() {
116
150
  return;
117
151
  }
118
152
  console.log(`Updating binary from v${installedVersion || 'unknown'} to v${PACKAGE_VERSION}...`);
153
+ } else {
154
+ // Check if binary exists in other common directories
155
+ const existingBinary = findExistingBinary(localBinaryName);
156
+ if (existingBinary) {
157
+ const existingVersion = getInstalledVersion(existingBinary);
158
+ if (existingVersion === PACKAGE_VERSION) {
159
+ // Same version, copy/link to npm bin directory
160
+ console.log(`Found existing binary at ${existingBinary} (v${PACKAGE_VERSION})`);
161
+ console.log(`Copying to npm bin directory: ${binaryPath}`);
162
+
163
+ if (!fs.existsSync(BINARY_DIR)) {
164
+ fs.mkdirSync(BINARY_DIR, { recursive: true });
165
+ }
166
+
167
+ fs.copyFileSync(existingBinary, binaryPath);
168
+ if (os.platform() !== 'win32') {
169
+ fs.chmodSync(binaryPath, 0o755);
170
+ }
171
+ console.log('Done!');
172
+ return;
173
+ } else if (existingVersion) {
174
+ console.log(`Found existing binary at ${existingBinary} (v${existingVersion}), but need v${PACKAGE_VERSION}`);
175
+ console.log('Installing new version to npm bin directory...');
176
+ } else {
177
+ console.log(`Found existing binary at ${existingBinary}, but version unknown`);
178
+ console.log('Installing to npm bin directory...');
179
+ }
180
+ }
119
181
  }
120
182
 
121
183
  // Create binary directory