Repository navigation
Expand file tree
/
Copy pathsetup.js
More file actions
199 lines (175 loc) · 5.66 KB
/
Copy pathsetup.js
File metadata and controls
199 lines (175 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
/**
* AMT Chat — unified setup script (Python + Node).
*
* Usage: node setup.js
*
* What it does:
* 1. Checks Node.js version (18+)
* 2. Checks Python 3.12+ (py launcher / PATH / per-user install)
* 3. Runs python setup.py → creates backend/.venv + installs requirements
* 4. Runs npm install → installs Electron + electron-builder
* 5. Prints next steps (build:py, build:win)
*
* Idempotent: safe to re-run. Skips steps that already succeeded.
*/
'use strict';
const { execSync, spawnSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const ROOT = path.resolve(__dirname);
// ---------- helpers ----------
function log(msg) {
console.log(msg);
}
function step(label) {
console.log(`\n==> ${label}`);
}
function fail(msg) {
console.error(`\n[ОШИБКА] ${msg}`);
process.exit(1);
}
function run(cmd, args = [], opts = {}) {
const r = spawnSync(cmd, args, {
cwd: opts.cwd || ROOT,
stdio: "inherit",
shell: process.platform === "win32",
env: { ...process.env, ...(opts.env || {}) },
});
if (r.error && r.error.code !== "ENOENT") {
throw r.error;
}
return r.status === 0;
}
/**
* Find a Python 3.12+ interpreter.
* Tries: py -3 launcher, python on PATH, per-user install location.
* @returns {string|null} command to run python (e.g. "py -3" or path to python.exe)
*/
function findPython() {
const candidates = [];
// 1) py launcher (Windows)
if (process.platform === "win32") {
candidates.push(["py", "-3"]);
candidates.push(["python", "-3"]);
}
// 2) python on PATH
candidates.push(["python"]);
candidates.push(["python3"]);
for (const cmd of candidates) {
try {
const r = spawnSync(cmd[0], [...cmd.slice(1), "-c", "import sys; print('.'.join(map(str, sys.version_info[:2])))"], {
cwd: ROOT,
encoding: "utf8",
shell: process.platform === "win32",
});
if (r.status === 0) {
const ver = (r.stdout || "").trim();
const parts = ver.split(".");
if (parts.length >= 2) {
const major = parseInt(parts[0], 10);
const minor = parseInt(parts[1], 10);
if (major === 3 && minor >= 12) {
return cmd.join(" ");
}
}
}
} catch {
// candidate not found
}
}
// 3) Per-user install location (Windows, after winget without PATH refresh)
if (process.platform === "win32" && process.env.LOCALAPPDATA) {
const localPy = path.join(process.env.LOCALAPPDATA, "Programs", "Python", "Python312", "python.exe");
if (fs.existsSync(localPy)) {
try {
const r = spawnSync(localPy, ["-c", "import sys; print('.'.join(map(str, sys.version_info[:2])))"], {
cwd: ROOT,
encoding: "utf8",
});
if (r.status === 0) {
const ver = (r.stdout || "").trim();
const parts = ver.split(".");
if (parts.length >= 2 && parseInt(parts[0]) === 3 && parseInt(parts[1]) >= 12) {
return localPy;
}
}
} catch {
// ignore
}
}
}
return null;
}
/**
* Check Node.js version.
* @returns {boolean}
*/
function checkNode() {
const nodeVer = process.versions.node;
const major = parseInt(nodeVer.split(".")[0], 10);
log(`Node.js ${nodeVer}`);
if (major < 18) {
fail("Node.js 18+ required. Install from https://nodejs.org");
}
return true;
}
/**
* Find and verify Python 3.12+.
* @returns {string} python command
*/
function checkPython() {
const py = findPython();
if (!py) {
fail(
"Python 3.12+ not found. Install from https://www.python.org/downloads/ " +
"(or on Windows: winget install Python.Python.3.12) and re-run."
);
}
log(`Python found: ${py}`);
return py;
}
function main() {
log("AMT Chat — setup\n");
// Step 1: Node.js
step("1/3 — Node.js check");
checkNode();
// Step 2: Python + venv
step("2/3 — Python 3.12+ + venv");
const py = checkPython();
const setupPy = path.join(ROOT, "setup.py");
if (!fs.existsSync(setupPy)) {
fail(`setup.py not found at ${setupPy}`);
}
// Run: python setup.py (creates backend/.venv, installs requirements)
// Parse the command: could be "py -3" (2 args) or a full path (1 arg)
const pyParts = py.split(" ");
const ok = run(pyParts[0], [...pyParts.slice(1), setupPy]);
if (!ok) {
fail("python setup.py failed. Check that Python 3.12+ is installed and re-run.");
}
log("✓ Python venv + requirements installed");
// Step 3: npm install
step("3/3 — npm install (Electron + electron-builder)");
const okNpm = run("npm", ["install"]);
if (!okNpm) {
fail("npm install failed. Check network / npm registry and re-run.");
}
log("✓ Node dependencies installed");
// Done
console.log(`
─────────────────────────────────────────────────────────────
AMT Chat setup complete
What was done:
• Python venv: backend/.venv (requirements.txt installed)
• Node modules: node_modules/ (Electron + electron-builder)
Next steps:
dev: npm start (runs electron . — dev mode)
build: npm run build:py (PyInstaller → dist/amt-backend/)
npm run build:win (electron-builder → release/ NSIS)
node scripts/build.js all (both, one command)
Or run the full pipeline:
powershell scripts\\bootstrap_and_build.ps1
─────────────────────────────────────────────────────────────`);
}
main();