hypha-cli 0.1.1 → 0.1.2
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/bin/postinstall.mjs +39 -29
- package/package.json +1 -1
package/bin/postinstall.mjs
CHANGED
|
@@ -3,63 +3,73 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* postinstall script for hypha-cli
|
|
5
5
|
*
|
|
6
|
-
* Detects and removes conflicting Python 'hypha' command
|
|
6
|
+
* Detects and removes conflicting Python 'hypha' command.
|
|
7
7
|
* The Python hypha package installs a 'hypha' console_script for starting
|
|
8
8
|
* the server, but `python -m hypha.server` is the preferred way to do that.
|
|
9
|
-
*
|
|
9
|
+
*
|
|
10
|
+
* npm runs postinstall in a stripped shell (no ~/.zshrc, no conda init, etc),
|
|
11
|
+
* so `which` can't see all PATH entries. We solve this by running `which -a`
|
|
12
|
+
* inside a login shell to get the user's full PATH.
|
|
10
13
|
*/
|
|
11
14
|
|
|
12
15
|
import { execSync } from 'child_process';
|
|
13
|
-
import { readFileSync, unlinkSync } from 'fs';
|
|
16
|
+
import { existsSync, readFileSync, unlinkSync } from 'fs';
|
|
17
|
+
|
|
18
|
+
function isPythonHyphaScript(filePath) {
|
|
19
|
+
try {
|
|
20
|
+
const head = readFileSync(filePath, 'utf-8').slice(0, 500);
|
|
21
|
+
return head.includes('python') && head.includes('from hypha');
|
|
22
|
+
} catch {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
14
26
|
|
|
15
27
|
function findConflictingBinaries() {
|
|
16
|
-
const found =
|
|
28
|
+
const found = new Set();
|
|
17
29
|
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
const output = execSync('which -a hypha 2>/dev/null', { encoding: 'utf-8' }).trim();
|
|
21
|
-
if (output) {
|
|
22
|
-
for (const binPath of output.split('\n')) {
|
|
23
|
-
const p = binPath.trim();
|
|
24
|
-
if (!p) continue;
|
|
30
|
+
// Detect user's shell
|
|
31
|
+
const shell = process.env.SHELL || '/bin/sh';
|
|
25
32
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
// Run `which -a hypha` in a login shell to get full PATH (conda, pyenv, etc.)
|
|
34
|
+
for (const cmd of [
|
|
35
|
+
`${shell} -l -c 'which -a hypha' 2>/dev/null`, // login shell (loads ~/.zshrc, conda init, etc.)
|
|
36
|
+
'which -a hypha 2>/dev/null', // plain fallback
|
|
37
|
+
]) {
|
|
38
|
+
try {
|
|
39
|
+
const output = execSync(cmd, { encoding: 'utf-8', timeout: 10000 }).trim();
|
|
40
|
+
for (const line of output.split('\n')) {
|
|
41
|
+
const p = line.trim();
|
|
42
|
+
if (p && existsSync(p) && isPythonHyphaScript(p)) {
|
|
43
|
+
found.add(p);
|
|
34
44
|
}
|
|
35
45
|
}
|
|
46
|
+
} catch {
|
|
47
|
+
// Shell or which not available
|
|
36
48
|
}
|
|
37
|
-
} catch {
|
|
38
|
-
// 'which' not found or no results — OK
|
|
39
49
|
}
|
|
40
50
|
|
|
41
|
-
return found;
|
|
51
|
+
return [...found];
|
|
42
52
|
}
|
|
43
53
|
|
|
44
54
|
function main() {
|
|
45
55
|
const conflicts = findConflictingBinaries();
|
|
46
|
-
|
|
47
56
|
if (conflicts.length === 0) return;
|
|
48
57
|
|
|
49
58
|
for (const binPath of conflicts) {
|
|
50
59
|
try {
|
|
51
60
|
unlinkSync(binPath);
|
|
52
|
-
console.log(`hypha-cli: removed conflicting Python 'hypha'
|
|
53
|
-
console.log(` (use 'python -m hypha.server' to start the Hypha server)`);
|
|
61
|
+
console.log(`hypha-cli: removed conflicting Python 'hypha' at ${binPath}`);
|
|
54
62
|
} catch (err) {
|
|
55
63
|
if (err.code === 'EACCES') {
|
|
56
|
-
console.log(`hypha-cli:
|
|
57
|
-
console.log(` remove it manually:
|
|
58
|
-
console.log(` (use 'python -m hypha.server' to start the Hypha server)`);
|
|
64
|
+
console.log(`hypha-cli: conflicting Python 'hypha' at ${binPath}`);
|
|
65
|
+
console.log(` remove it manually: rm ${binPath}`);
|
|
59
66
|
}
|
|
60
|
-
// Other errors — silently skip
|
|
61
67
|
}
|
|
62
68
|
}
|
|
69
|
+
|
|
70
|
+
if (conflicts.length > 0) {
|
|
71
|
+
console.log(` (use 'python -m hypha.server' to start the Hypha server instead)`);
|
|
72
|
+
}
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
main();
|