travel-agent-cli 0.2.2 → 0.2.3
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/package.json +1 -1
- package/scripts/postinstall.js +69 -27
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
3
|
* npm 安装后执行的脚本
|
|
4
|
-
*
|
|
4
|
+
* 创建虚拟环境并安装 Python 依赖
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const { execSync } = require('child_process');
|
|
@@ -10,12 +10,13 @@ const path = require('path');
|
|
|
10
10
|
|
|
11
11
|
const packagePath = path.join(__dirname, '..');
|
|
12
12
|
const pythonDir = path.join(packagePath, 'python');
|
|
13
|
+
const venvPath = path.join(pythonDir, 'venv');
|
|
13
14
|
|
|
14
15
|
console.log('╭───────────────────────────────────────────────────────────────╮');
|
|
15
16
|
console.log('│ travel-agent-cli 正在安装 Python 依赖... │');
|
|
16
17
|
console.log('╰───────────────────────────────────────────────────────────────╯\n');
|
|
17
18
|
|
|
18
|
-
//
|
|
19
|
+
// 查找 Python
|
|
19
20
|
function findPython() {
|
|
20
21
|
try {
|
|
21
22
|
execSync('python3 --version', { stdio: 'ignore' });
|
|
@@ -30,48 +31,89 @@ function findPython() {
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
//
|
|
34
|
-
function
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
console.error(' Linux: sudo apt install python3.10');
|
|
42
|
-
console.error(' Windows: https://www.python.org/downloads/');
|
|
43
|
-
return false;
|
|
34
|
+
// 获取虚拟环境的 Python 路径
|
|
35
|
+
function getVenvPython() {
|
|
36
|
+
const paths = [
|
|
37
|
+
path.join(venvPath, 'bin', 'python'),
|
|
38
|
+
path.join(venvPath, 'Scripts', 'python.exe'),
|
|
39
|
+
];
|
|
40
|
+
for (const p of paths) {
|
|
41
|
+
if (fs.existsSync(p)) return p;
|
|
44
42
|
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
// 创建虚拟环境
|
|
47
|
+
function createVenv(pythonCmd) {
|
|
48
|
+
if (fs.existsSync(venvPath)) {
|
|
49
|
+
console.log('✓ 虚拟环境:已存在');
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
console.
|
|
53
|
+
console.log('正在创建虚拟环境...');
|
|
54
|
+
try {
|
|
55
|
+
execSync(`${pythonCmd} -m venv "${venvPath}"`, { stdio: 'inherit' });
|
|
56
|
+
console.log('✓ 虚拟环境创建完成\n');
|
|
57
|
+
return true;
|
|
58
|
+
} catch (e) {
|
|
59
|
+
console.error('✗ 虚拟环境创建失败');
|
|
52
60
|
return false;
|
|
53
61
|
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 安装依赖
|
|
65
|
+
function installDependencies(venvPython) {
|
|
66
|
+
console.log(`使用虚拟环境:${venvPython}`);
|
|
67
|
+
console.log('正在安装 Python 依赖...\n');
|
|
54
68
|
|
|
55
69
|
try {
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
70
|
+
// 先升级 pip
|
|
71
|
+
execSync(`${venvPython} -m pip install --upgrade pip`, { stdio: 'ignore' });
|
|
72
|
+
|
|
73
|
+
// 安装项目依赖
|
|
74
|
+
execSync(`${venvPython} -m pip install -e "${pythonDir}"`, { stdio: 'inherit' });
|
|
75
|
+
|
|
62
76
|
console.log('\n✓ Python 依赖安装完成\n');
|
|
63
77
|
return true;
|
|
64
78
|
} catch (e) {
|
|
65
79
|
console.error('\n⚠ Python 依赖安装失败');
|
|
66
|
-
console.error('\n手动安装:');
|
|
67
|
-
console.error(` ${pythonCmd} -m pip install -e "${pythonDir}"`);
|
|
68
80
|
return false;
|
|
69
81
|
}
|
|
70
82
|
}
|
|
71
83
|
|
|
72
84
|
// 主函数
|
|
73
85
|
function main() {
|
|
74
|
-
const
|
|
86
|
+
const pythonCmd = findPython();
|
|
87
|
+
|
|
88
|
+
if (!pythonCmd) {
|
|
89
|
+
console.error('\n⚠ 警告:未找到 Python');
|
|
90
|
+
console.error('请先安装 Python 3.10+');
|
|
91
|
+
console.error(' macOS: brew install python@3.10');
|
|
92
|
+
console.error(' Linux: sudo apt install python3.10');
|
|
93
|
+
console.error(' Windows: https://www.python.org/downloads/');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log(`使用 Python: ${pythonCmd}\n`);
|
|
98
|
+
|
|
99
|
+
// 创建虚拟环境
|
|
100
|
+
if (!createVenv(pythonCmd)) {
|
|
101
|
+
console.error('\n请手动创建虚拟环境并安装依赖:');
|
|
102
|
+
console.error(` ${pythonCmd} -m venv "${venvPath}"`);
|
|
103
|
+
console.error(` source ${path.join(venvPath, 'bin', 'activate')}`);
|
|
104
|
+
console.error(` pip install -e "${pythonDir}"`);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 获取虚拟环境的 Python
|
|
109
|
+
const venvPython = getVenvPython();
|
|
110
|
+
if (!venvPython) {
|
|
111
|
+
console.error('✗ 未找到虚拟环境的 Python');
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 安装依赖
|
|
116
|
+
const success = installDependencies(venvPython);
|
|
75
117
|
|
|
76
118
|
if (success) {
|
|
77
119
|
console.log('╭───────────────────────────────────────────────────────────────╮');
|