@balcnix/fast-deploy 1.0.3 → 1.0.4
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/README.md +53 -0
- package/bin/cli.js +394 -168
- package/package.json +3 -4
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# balcnix (fast-deploy)
|
|
2
|
+
|
|
3
|
+
A small CLI that scans a project folder, detects its stack (Node.js or Python), installs dependencies, and runs a likely entrypoint.
|
|
4
|
+
|
|
5
|
+
## Install / Run
|
|
6
|
+
|
|
7
|
+
From this repo:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
node bin/cli.js --help
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If installed globally (via npm package/bin):
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
balcnix deploy
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
balcnix deploy # Scan project, install deps, and run entrypoint
|
|
23
|
+
balcnix deploy --no-run # Scan and install only (do not start)
|
|
24
|
+
balcnix --help # Show help
|
|
25
|
+
balcnix --version # Show version
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## What it does
|
|
29
|
+
|
|
30
|
+
### Node.js projects
|
|
31
|
+
- Detects Node projects via `package.json` or JS/TS source files.
|
|
32
|
+
- If no `package.json` exists:
|
|
33
|
+
- scans imports/requires to detect external packages
|
|
34
|
+
- generates `package.json` (including a `start` script)
|
|
35
|
+
- installs detected dependencies
|
|
36
|
+
- If `package.json` exists:
|
|
37
|
+
- installs dependencies from the manifest
|
|
38
|
+
- also installs missing packages detected from source imports (best-effort)
|
|
39
|
+
- Picks a likely entry file (e.g. `index.js`, `server.js`, `app.js`) using heuristics.
|
|
40
|
+
- Runs with `npm start`.
|
|
41
|
+
|
|
42
|
+
### Python projects
|
|
43
|
+
- Detects Python projects via `requirements.txt`, `pyproject.toml`, or `.py` files.
|
|
44
|
+
- Creates a local `venv/` if needed.
|
|
45
|
+
- Installs from `requirements.txt` when present.
|
|
46
|
+
- Otherwise scans imports, maps common module names to PyPI packages, installs them, and generates `requirements.txt`.
|
|
47
|
+
- Picks a likely entry file (e.g. `main.py`, `app.py`, `bot.py`) using heuristics.
|
|
48
|
+
- Runs using the venv Python.
|
|
49
|
+
|
|
50
|
+
## Notes
|
|
51
|
+
- The CLI is designed to work without external npm runtime dependencies.
|
|
52
|
+
- In non-interactive shells, it skips the "press enter to launch" prompt.
|
|
53
|
+
- Use `--no-run` for setup/install-only workflows.
|
package/bin/cli.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
cat << 'EOF' > /mnt/master1/fast-deploy/bin/cli.js
|
|
2
1
|
#!/usr/bin/env node
|
|
3
2
|
|
|
4
3
|
const fs = require('fs');
|
|
@@ -6,45 +5,151 @@ const path = require('path');
|
|
|
6
5
|
const { execSync, spawn } = require('child_process');
|
|
7
6
|
const { builtinModules } = require('module');
|
|
8
7
|
const readline = require('readline');
|
|
9
|
-
const chalk = require('chalk');
|
|
10
|
-
const ora = require('ora');
|
|
11
8
|
|
|
12
9
|
const args = process.argv.slice(2);
|
|
13
10
|
const command = args[0];
|
|
14
11
|
const cwd = process.cwd();
|
|
15
12
|
const isWin = process.platform === 'win32';
|
|
13
|
+
const noRun = args.includes('--no-run') || args.includes('--install-only');
|
|
14
|
+
const wantsHelp = !command || command === 'help' || args.includes('--help') || args.includes('-h');
|
|
15
|
+
const wantsVersion = command === 'version' || args.includes('--version') || args.includes('-v');
|
|
16
|
+
|
|
17
|
+
const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
18
|
+
const c = (code, s) => (useColor ? `\x1b[${code}m${s}\x1b[0m` : String(s));
|
|
19
|
+
const style = {
|
|
20
|
+
red: (s) => c('31', s),
|
|
21
|
+
green: (s) => c('32', s),
|
|
22
|
+
yellow: (s) => c('33', s),
|
|
23
|
+
blue: (s) => c('34', s),
|
|
24
|
+
magenta: (s) => c('35', s),
|
|
25
|
+
cyan: (s) => c('36', s),
|
|
26
|
+
gray: (s) => c('90', s),
|
|
27
|
+
white: (s) => c('37', s),
|
|
28
|
+
bold: (s) => c('1', s),
|
|
29
|
+
bgGreenBlack: (s) => (useColor ? `\x1b[42m\x1b[30m\x1b[1m${s}\x1b[0m` : String(s)),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function printHelp() {
|
|
33
|
+
console.log(`
|
|
34
|
+
${style.bold(style.white('balcnix'))} ${style.gray('- fast universal dependency builder, installer, and runner')}
|
|
35
|
+
|
|
36
|
+
${style.white('Usage:')}
|
|
37
|
+
${style.cyan('balcnix deploy')} Scan project, install deps, and run entrypoint
|
|
38
|
+
${style.cyan('balcnix deploy --no-run')} Scan and install only (do not start)
|
|
39
|
+
${style.cyan('balcnix --help')} Show this help
|
|
40
|
+
${style.cyan('balcnix --version')} Show version
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getVersion() {
|
|
45
|
+
try {
|
|
46
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
|
|
47
|
+
return pkg.version || '0.0.0';
|
|
48
|
+
} catch {
|
|
49
|
+
return '0.0.0';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (wantsVersion) {
|
|
54
|
+
console.log(getVersion());
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (wantsHelp) {
|
|
59
|
+
printHelp();
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
16
62
|
|
|
17
63
|
if (command !== 'deploy') {
|
|
18
|
-
console.log(
|
|
64
|
+
console.log(style.red(`\n✖ Unknown command: ${command}`));
|
|
65
|
+
printHelp();
|
|
19
66
|
process.exit(1);
|
|
20
67
|
}
|
|
21
68
|
|
|
22
|
-
function
|
|
23
|
-
|
|
69
|
+
function makeSpinner() {
|
|
70
|
+
const frames = ['-', '\\', '|', '/'];
|
|
71
|
+
let i = 0;
|
|
72
|
+
let timer = null;
|
|
73
|
+
let text = '';
|
|
74
|
+
let active = false;
|
|
75
|
+
|
|
76
|
+
function render(prefix = '') {
|
|
77
|
+
if (!process.stdout.isTTY) return;
|
|
78
|
+
const line = `${prefix}${text}`;
|
|
79
|
+
process.stdout.write(`\r${line}\x1b[K`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function start(t) {
|
|
83
|
+
text = t || text;
|
|
84
|
+
if (!process.stdout.isTTY) {
|
|
85
|
+
console.log(`- ${text}`);
|
|
86
|
+
active = true;
|
|
87
|
+
return api;
|
|
88
|
+
}
|
|
89
|
+
if (timer) clearInterval(timer);
|
|
90
|
+
active = true;
|
|
91
|
+
render(`${style.cyan(frames[i % frames.length])} `);
|
|
92
|
+
timer = setInterval(() => {
|
|
93
|
+
i += 1;
|
|
94
|
+
render(`${style.cyan(frames[i % frames.length])} `);
|
|
95
|
+
}, 80);
|
|
96
|
+
return api;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function stop(symbol, message) {
|
|
100
|
+
if (timer) clearInterval(timer);
|
|
101
|
+
timer = null;
|
|
102
|
+
active = false;
|
|
103
|
+
const msg = message || text;
|
|
104
|
+
if (process.stdout.isTTY) {
|
|
105
|
+
process.stdout.write(`\r${symbol} ${msg}\x1b[K\n`);
|
|
106
|
+
} else {
|
|
107
|
+
console.log(`${symbol} ${msg}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const api = {
|
|
112
|
+
start,
|
|
113
|
+
set text(v) {
|
|
114
|
+
text = v;
|
|
115
|
+
if (active && process.stdout.isTTY) render(`${style.cyan(frames[i % frames.length])} `);
|
|
116
|
+
},
|
|
117
|
+
get text() {
|
|
118
|
+
return text;
|
|
119
|
+
},
|
|
120
|
+
succeed(msg) { stop(style.green('✔'), msg); },
|
|
121
|
+
fail(msg) { stop(style.red('✖'), msg); },
|
|
122
|
+
info(msg) { stop(style.cyan('i'), msg); },
|
|
123
|
+
};
|
|
124
|
+
return api;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function runCmd(cmd, options = {}) {
|
|
128
|
+
return execSync(cmd, { cwd, stdio: 'pipe', encoding: 'utf8', ...options });
|
|
24
129
|
}
|
|
25
130
|
|
|
26
131
|
function getSystemPython() {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return 'python3';
|
|
30
|
-
} catch {
|
|
132
|
+
const candidates = isWin ? ['py -3', 'python', 'python3'] : ['python3', 'python'];
|
|
133
|
+
for (const cmd of candidates) {
|
|
31
134
|
try {
|
|
32
|
-
|
|
33
|
-
return
|
|
135
|
+
runCmd(`${cmd} --version`);
|
|
136
|
+
return cmd;
|
|
34
137
|
} catch {
|
|
35
|
-
|
|
138
|
+
// try next
|
|
36
139
|
}
|
|
37
140
|
}
|
|
141
|
+
throw new Error('Python is not installed on this system. Install Python 3 and try again.');
|
|
38
142
|
}
|
|
39
143
|
|
|
40
144
|
function waitForEnter() {
|
|
145
|
+
if (!process.stdin.isTTY) return Promise.resolve();
|
|
41
146
|
return new Promise((resolve) => {
|
|
42
147
|
const rl = readline.createInterface({
|
|
43
148
|
input: process.stdin,
|
|
44
149
|
output: process.stdout,
|
|
45
150
|
});
|
|
46
151
|
rl.question(
|
|
47
|
-
|
|
152
|
+
style.bgGreenBlack('\n PRESS ENTER ') + style.green(' to launch project console... '),
|
|
48
153
|
() => {
|
|
49
154
|
rl.close();
|
|
50
155
|
resolve();
|
|
@@ -53,53 +158,100 @@ function waitForEnter() {
|
|
|
53
158
|
});
|
|
54
159
|
}
|
|
55
160
|
|
|
161
|
+
function safeReadDir(dir) {
|
|
162
|
+
try {
|
|
163
|
+
return fs.readdirSync(dir);
|
|
164
|
+
} catch {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function isDirectory(p) {
|
|
170
|
+
try {
|
|
171
|
+
return fs.statSync(p).isDirectory();
|
|
172
|
+
} catch {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
56
177
|
function scanNodeProject(dir) {
|
|
57
178
|
const deps = new Set();
|
|
58
179
|
const importedLocals = new Set();
|
|
59
180
|
const rootJsFiles = [];
|
|
60
181
|
let usesESM = false;
|
|
61
|
-
const ignoreDirs = new Set(['node_modules', '.git', 'dist', 'build', 'venv', '.venv']);
|
|
182
|
+
const ignoreDirs = new Set(['node_modules', '.git', 'dist', 'build', 'venv', '.venv', '__pycache__', '.next', 'coverage', '.turbo', 'out']);
|
|
62
183
|
|
|
63
184
|
function walk(currentDir, isRoot = false) {
|
|
64
|
-
for (const item of
|
|
185
|
+
for (const item of safeReadDir(currentDir)) {
|
|
65
186
|
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
66
187
|
const fullPath = path.join(currentDir, item);
|
|
67
|
-
if (
|
|
188
|
+
if (isDirectory(fullPath)) {
|
|
68
189
|
walk(fullPath, false);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (!/\.(js|mjs|cjs|ts|mts|cts)$/.test(item)) continue;
|
|
193
|
+
|
|
194
|
+
if (isRoot && /\.(js|mjs|cjs)$/.test(item)) {
|
|
195
|
+
rootJsFiles.push({ name: item, fullPath });
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let content = '';
|
|
199
|
+
try {
|
|
200
|
+
content = fs.readFileSync(fullPath, 'utf8');
|
|
201
|
+
} catch {
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (/^\s*(?:import\s+|export\s+)/m.test(content)) usesESM = true;
|
|
206
|
+
|
|
207
|
+
const regex = /(?:require\s*\(\s*|from\s+|import\s+(?:[^'";]+?\s+from\s+)?|import\s*\(\s*)['"]([^'"]+)['"]/g;
|
|
208
|
+
let match;
|
|
209
|
+
while ((match = regex.exec(content)) !== null) {
|
|
210
|
+
const target = match[1];
|
|
211
|
+
if (!target) continue;
|
|
212
|
+
if (target.startsWith('.') || target.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(target)) {
|
|
213
|
+
const base = path.basename(target).replace(/\.(js|mjs|cjs|ts|mts|cts)$/, '');
|
|
214
|
+
if (base) importedLocals.add(base);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
let pkg = target;
|
|
219
|
+
if (target.startsWith('@')) {
|
|
220
|
+
const parts = target.split('/');
|
|
221
|
+
pkg = parts.length >= 2 ? `${parts[0]}/${parts[1]}` : target;
|
|
222
|
+
} else {
|
|
223
|
+
pkg = target.split('/')[0];
|
|
224
|
+
}
|
|
225
|
+
const clean = pkg.replace(/^node:/, '');
|
|
226
|
+
if (!builtinModules.includes(clean) && !builtinModules.includes(`node:${clean}`)) {
|
|
227
|
+
deps.add(pkg);
|
|
86
228
|
}
|
|
87
229
|
}
|
|
88
230
|
}
|
|
89
231
|
}
|
|
232
|
+
|
|
90
233
|
walk(dir, true);
|
|
91
234
|
|
|
92
|
-
|
|
93
|
-
let
|
|
235
|
+
const preferred = ['index.js', 'main.js', 'server.js', 'app.js', 'bot.js', 'start.js'];
|
|
236
|
+
let bestEntry = rootJsFiles.find((f) => preferred.includes(f.name))?.name || rootJsFiles[0]?.name || 'index.js';
|
|
237
|
+
let bestScore = -Infinity;
|
|
94
238
|
|
|
95
239
|
for (const file of rootJsFiles) {
|
|
96
240
|
let score = 0;
|
|
97
|
-
const base = file.name.replace(/\.js$/, '');
|
|
98
|
-
|
|
241
|
+
const base = file.name.replace(/\.(js|mjs|cjs)$/, '');
|
|
242
|
+
let content = '';
|
|
243
|
+
try {
|
|
244
|
+
content = fs.readFileSync(file.fullPath, 'utf8');
|
|
245
|
+
} catch {
|
|
246
|
+
content = '';
|
|
247
|
+
}
|
|
99
248
|
|
|
100
249
|
if (!importedLocals.has(base)) score += 50;
|
|
101
|
-
if (
|
|
102
|
-
if (/\.listen\(|\.login\(|createServer\(|express\(\
|
|
250
|
+
if (preferred.includes(file.name)) score += 30;
|
|
251
|
+
if (/\.listen\s*\(|\.login\s*\(|createServer\s*\(|express\s*\(|fastify\s*\(|startServer\s*\(/.test(content)) score += 25;
|
|
252
|
+
if (/if\s*\(\s*require\.main\s*===\s*module\s*\)|require\.main\s*===\s*module/.test(content)) score += 20;
|
|
253
|
+
if (/process\.argv/.test(content)) score += 5;
|
|
254
|
+
if (/(^|[\\/])(config|utils?|helpers?|lib|types?)\./.test(file.name)) score -= 20;
|
|
103
255
|
|
|
104
256
|
if (score > bestScore) {
|
|
105
257
|
bestScore = score;
|
|
@@ -107,7 +259,7 @@ function scanNodeProject(dir) {
|
|
|
107
259
|
}
|
|
108
260
|
}
|
|
109
261
|
|
|
110
|
-
return { deps: Array.from(deps), usesESM, entryFile: bestEntry };
|
|
262
|
+
return { deps: Array.from(deps).sort(), usesESM, entryFile: bestEntry };
|
|
111
263
|
}
|
|
112
264
|
|
|
113
265
|
function scanPythonProject(dir) {
|
|
@@ -123,7 +275,13 @@ function scanPythonProject(dir) {
|
|
|
123
275
|
yaml: 'PyYAML',
|
|
124
276
|
bs4: 'beautifulsoup4',
|
|
125
277
|
dotenv: 'python-dotenv',
|
|
126
|
-
sklearn: 'scikit-learn'
|
|
278
|
+
sklearn: 'scikit-learn',
|
|
279
|
+
dateutil: 'python-dateutil',
|
|
280
|
+
Crypto: 'pycryptodome',
|
|
281
|
+
googleapiclient: 'google-api-python-client',
|
|
282
|
+
jwt: 'PyJWT',
|
|
283
|
+
serial: 'pyserial',
|
|
284
|
+
usb: 'pyusb',
|
|
127
285
|
};
|
|
128
286
|
|
|
129
287
|
const stdLib = new Set([
|
|
@@ -132,34 +290,46 @@ function scanPythonProject(dir) {
|
|
|
132
290
|
'itertools', 'functools', 'urllib', 'http', 'sqlite3', 'csv', 'logging',
|
|
133
291
|
'unittest', 'shutil', 'glob', 'hashlib', 'hmac', 'base64', 'uuid', 'socket',
|
|
134
292
|
'ssl', 'argparse', 'configparser', 'io', 'tempfile', 'traceback', 'inspect',
|
|
135
|
-
'dataclasses', 'enum', 'abc', 'contextlib', 'copy', 'pprint', 'string', 'textwrap'
|
|
293
|
+
'dataclasses', 'enum', 'abc', 'contextlib', 'copy', 'pprint', 'string', 'textwrap',
|
|
294
|
+
'statistics', 'platform', 'queue', 'signal', 'types', 'warnings', 'zipfile',
|
|
295
|
+
'tarfile', 'getpass', 'secrets', 'decimal', 'fractions', 'html', 'xml', 'email',
|
|
296
|
+
'importlib', 'pkgutil', 'site', 'venv', 'ctypes', 'struct', 'mimetypes', 'builtins'
|
|
136
297
|
]);
|
|
137
298
|
|
|
138
|
-
const ignoreDirs = new Set(['venv', '.venv', '__pycache__', '.git', 'node_modules']);
|
|
299
|
+
const ignoreDirs = new Set(['venv', '.venv', '__pycache__', '.git', 'node_modules', 'dist', 'build', '.mypy_cache', '.pytest_cache', '.tox']);
|
|
139
300
|
|
|
140
301
|
function walk(currentDir, isRoot = false) {
|
|
141
|
-
for (const item of
|
|
302
|
+
for (const item of safeReadDir(currentDir)) {
|
|
142
303
|
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
143
304
|
const fullPath = path.join(currentDir, item);
|
|
144
|
-
if (
|
|
305
|
+
if (isDirectory(fullPath)) {
|
|
145
306
|
localModules.add(item);
|
|
146
307
|
walk(fullPath, false);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (!item.endsWith('.py')) continue;
|
|
311
|
+
|
|
312
|
+
const modName = item.replace(/\.py$/, '');
|
|
313
|
+
localModules.add(modName);
|
|
314
|
+
if (isRoot) rootPyFiles.push({ name: item, modName, fullPath });
|
|
315
|
+
|
|
316
|
+
let content = '';
|
|
317
|
+
try {
|
|
318
|
+
content = fs.readFileSync(fullPath, 'utf8');
|
|
319
|
+
} catch {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const regex = /^\s*(?:from|import)\s+([a-zA-Z0-9_]+)/gm;
|
|
324
|
+
let match;
|
|
325
|
+
while ((match = regex.exec(content)) !== null) {
|
|
326
|
+
const mod = match[1];
|
|
327
|
+
rawImports.add(mod);
|
|
328
|
+
importedLocals.add(mod);
|
|
160
329
|
}
|
|
161
330
|
}
|
|
162
331
|
}
|
|
332
|
+
|
|
163
333
|
walk(dir, true);
|
|
164
334
|
|
|
165
335
|
const externalDeps = [];
|
|
@@ -169,25 +339,32 @@ function scanPythonProject(dir) {
|
|
|
169
339
|
}
|
|
170
340
|
}
|
|
171
341
|
|
|
172
|
-
|
|
173
|
-
let
|
|
342
|
+
const preferred = ['main.py', 'app.py', 'bot.py', 'run.py', 'start.py', 'server.py', 'index.py'];
|
|
343
|
+
let bestEntry = rootPyFiles.find((f) => preferred.includes(f.name))?.name || rootPyFiles[0]?.name || 'main.py';
|
|
344
|
+
let bestScore = -Infinity;
|
|
174
345
|
|
|
175
346
|
for (const file of rootPyFiles) {
|
|
176
347
|
let score = 0;
|
|
177
|
-
|
|
348
|
+
let content = '';
|
|
349
|
+
try {
|
|
350
|
+
content = fs.readFileSync(file.fullPath, 'utf8');
|
|
351
|
+
} catch {
|
|
352
|
+
content = '';
|
|
353
|
+
}
|
|
178
354
|
|
|
179
355
|
if (!importedLocals.has(file.modName)) score += 60;
|
|
180
|
-
if (/if\s+__name__\s*==\s*['"]__main__['"]
|
|
181
|
-
if (/\b(?:
|
|
182
|
-
if (
|
|
356
|
+
if (/if\s+__name__\s*==\s*['"]__main__['"]\s*:/.test(content)) score += 50;
|
|
357
|
+
if (/\b(?:uvicorn\.run|app\.run|bot\.run|client\.run|asyncio\.run|main\s*\()\s*\(/.test(content)) score += 35;
|
|
358
|
+
if (preferred.includes(file.name)) score += 20;
|
|
183
359
|
|
|
184
360
|
let localImportCount = 0;
|
|
185
361
|
for (const loc of localModules) {
|
|
186
|
-
if (loc !== file.modName && new RegExp(
|
|
362
|
+
if (loc !== file.modName && new RegExp(`^\\s*(?:from|import)\\s+${loc}\\b`, 'm').test(content)) {
|
|
187
363
|
localImportCount++;
|
|
188
364
|
}
|
|
189
365
|
}
|
|
190
|
-
score += localImportCount *
|
|
366
|
+
score += Math.min(localImportCount, 5) * 4;
|
|
367
|
+
if (['__init__.py', 'setup.py', 'conftest.py'].includes(file.name)) score -= 100;
|
|
191
368
|
|
|
192
369
|
if (score > bestScore) {
|
|
193
370
|
bestScore = score;
|
|
@@ -195,164 +372,213 @@ function scanPythonProject(dir) {
|
|
|
195
372
|
}
|
|
196
373
|
}
|
|
197
374
|
|
|
198
|
-
return { deps: externalDeps, entryFile: bestEntry };
|
|
375
|
+
return { deps: Array.from(new Set(externalDeps)).sort(), entryFile: bestEntry };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function formatPkgList(pkgs, max = 6) {
|
|
379
|
+
if (!pkgs || pkgs.length === 0) return 'none';
|
|
380
|
+
if (pkgs.length <= max) return pkgs.join(', ');
|
|
381
|
+
return `${pkgs.slice(0, max).join(', ')} +${pkgs.length - max} more`;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function commandExists(cmd) {
|
|
385
|
+
try {
|
|
386
|
+
runCmd(isWin ? `where ${cmd}` : `command -v ${cmd}`);
|
|
387
|
+
return true;
|
|
388
|
+
} catch {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
199
391
|
}
|
|
200
|
-
cat << 'EOF' >> /mnt/master1/fast-deploy/bin/cli.js
|
|
201
392
|
|
|
202
393
|
async function deploy() {
|
|
203
394
|
const startTime = Date.now();
|
|
204
395
|
console.clear();
|
|
205
|
-
console.log(
|
|
396
|
+
console.log(style.bold(style.white('\n> balcnix deploy ')) + style.gray(`v${getVersion()}\n`));
|
|
206
397
|
|
|
207
|
-
const spinner =
|
|
208
|
-
await new Promise((r) => setTimeout(r,
|
|
398
|
+
const spinner = makeSpinner().start('Fetching & scanning project files...');
|
|
399
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
209
400
|
|
|
210
|
-
const files =
|
|
211
|
-
spinner.succeed(
|
|
401
|
+
const files = safeReadDir(cwd).filter((f) => !f.startsWith('.git'));
|
|
402
|
+
spinner.succeed(style.gray(`Scanned ${files.length} items in ${path.basename(cwd) || cwd}`));
|
|
212
403
|
|
|
213
404
|
let stack = '';
|
|
214
405
|
let startCmd = '';
|
|
215
406
|
let entryDisplay = '';
|
|
216
407
|
let pkgCount = 0;
|
|
217
408
|
let envNote = 'default';
|
|
409
|
+
let detectedPkgs = [];
|
|
218
410
|
|
|
219
411
|
try {
|
|
220
|
-
|
|
412
|
+
const hasPackageJson = files.includes('package.json');
|
|
413
|
+
const hasNodeFiles = files.some((f) => /\.(js|mjs|cjs|ts|mts|cts)$/.test(f));
|
|
414
|
+
const hasPyProject = files.includes('requirements.txt') || files.includes('pyproject.toml') || files.some((f) => f.endsWith('.py'));
|
|
415
|
+
|
|
416
|
+
const pickNode = hasPackageJson || (hasNodeFiles && !files.includes('requirements.txt') && !files.includes('pyproject.toml') && !files.some((f) => f.endsWith('.py')));
|
|
417
|
+
|
|
418
|
+
if (pickNode) {
|
|
221
419
|
stack = 'Node.js';
|
|
420
|
+
if (!commandExists('npm')) {
|
|
421
|
+
throw new Error('npm is not installed or not in PATH. Install Node.js/npm and try again.');
|
|
422
|
+
}
|
|
423
|
+
|
|
222
424
|
const { deps: detected, usesESM, entryFile } = scanNodeProject(cwd);
|
|
223
425
|
entryDisplay = entryFile;
|
|
426
|
+
detectedPkgs = detected;
|
|
224
427
|
|
|
225
|
-
if (!
|
|
428
|
+
if (!hasPackageJson) {
|
|
226
429
|
spinner.start('No package.json found. Scanning imports & building...');
|
|
227
430
|
pkgCount = detected.length;
|
|
228
431
|
|
|
229
432
|
runCmd('npm init -y');
|
|
230
433
|
const pkgPath = path.join(cwd, 'package.json');
|
|
231
434
|
const pkgData = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
435
|
+
pkgData.name = pkgData.name || path.basename(cwd).toLowerCase().replace(/[^a-z0-9-_]/g, '-') || 'app';
|
|
436
|
+
pkgData.version = pkgData.version || '1.0.0';
|
|
437
|
+
pkgData.main = entryFile || 'index.js';
|
|
438
|
+
pkgData.scripts = { ...(pkgData.scripts || {}), start: `node ${entryFile || 'index.js'}` };
|
|
236
439
|
if (usesESM) {
|
|
237
440
|
pkgData.type = 'module';
|
|
238
441
|
envNote = 'ES Modules';
|
|
442
|
+
} else {
|
|
443
|
+
envNote = 'CommonJS';
|
|
239
444
|
}
|
|
240
445
|
fs.writeFileSync(pkgPath, JSON.stringify(pkgData, null, 2));
|
|
241
|
-
spinner.succeed(
|
|
446
|
+
spinner.succeed(style.green(`Built package.json (${pkgCount} dependencies detected)`));
|
|
242
447
|
|
|
243
448
|
if (detected.length > 0) {
|
|
244
|
-
spinner.start(`Downloading packages: ${detected
|
|
245
|
-
runCmd(`npm install ${detected.join(' ')}`);
|
|
246
|
-
spinner.succeed(
|
|
449
|
+
spinner.start(`Downloading packages: ${formatPkgList(detected)}...`);
|
|
450
|
+
runCmd(`npm install ${detected.map((p) => `"${p}"`).join(' ')}`);
|
|
451
|
+
spinner.succeed(style.green('All Node.js dependencies installed'));
|
|
452
|
+
} else {
|
|
453
|
+
spinner.succeed(style.green('No external Node.js dependencies detected'));
|
|
247
454
|
}
|
|
248
455
|
} else {
|
|
456
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
457
|
+
const pkgData = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
458
|
+
const declaredDeps = { ...(pkgData.dependencies || {}), ...(pkgData.optionalDependencies || {}) };
|
|
459
|
+
const missingDetected = detected.filter((d) => !declaredDeps[d]);
|
|
460
|
+
|
|
461
|
+
if (missingDetected.length > 0) {
|
|
462
|
+
spinner.start(`Installing missing detected packages: ${formatPkgList(missingDetected)}...`);
|
|
463
|
+
runCmd(`npm install ${missingDetected.map((p) => `"${p}"`).join(' ')}`);
|
|
464
|
+
spinner.succeed(style.green(`Installed ${missingDetected.length} missing package(s)`));
|
|
465
|
+
}
|
|
466
|
+
|
|
249
467
|
spinner.start('Downloading dependencies from package.json...');
|
|
250
468
|
runCmd('npm install');
|
|
251
|
-
const
|
|
252
|
-
pkgCount = Object.keys(
|
|
253
|
-
|
|
254
|
-
|
|
469
|
+
const pkgAfter = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
470
|
+
pkgCount = Object.keys(pkgAfter.dependencies || {}).length + Object.keys(pkgAfter.optionalDependencies || {}).length;
|
|
471
|
+
envNote = pkgAfter.type === 'module' ? 'ES Modules' : 'CommonJS';
|
|
472
|
+
if (pkgAfter.main) entryDisplay = pkgAfter.main;
|
|
473
|
+
spinner.succeed(style.green('Dependencies installed'));
|
|
255
474
|
}
|
|
256
475
|
|
|
257
476
|
const finalPkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
477
|
+
const entry = (finalPkg.scripts && finalPkg.scripts.start && entryDisplay) ? entryDisplay : (entryDisplay || finalPkg.main || 'index.js');
|
|
478
|
+
if (!(finalPkg.scripts && finalPkg.scripts.start)) {
|
|
479
|
+
finalPkg.scripts = { ...(finalPkg.scripts || {}), start: `node ${entry}` };
|
|
480
|
+
if (!finalPkg.main) finalPkg.main = entry;
|
|
481
|
+
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(finalPkg, null, 2));
|
|
482
|
+
}
|
|
483
|
+
startCmd = 'npm start';
|
|
484
|
+
entryDisplay = entry;
|
|
485
|
+
} else if (hasPyProject) {
|
|
264
486
|
stack = 'Python';
|
|
265
|
-
const sysPython = getSystemPython();
|
|
266
487
|
const { deps: detected, entryFile } = scanPythonProject(cwd);
|
|
267
488
|
entryDisplay = entryFile;
|
|
489
|
+
detectedPkgs = detected;
|
|
268
490
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.filter((l) => l && !l.startsWith('#'));
|
|
279
|
-
pkgCount = reqs.length;
|
|
491
|
+
const pyCmd = getSystemPython();
|
|
492
|
+
spinner.start(`Configuring Python environment (${pyCmd})...`);
|
|
493
|
+
|
|
494
|
+
const venvDir = path.join(cwd, 'venv');
|
|
495
|
+
const venvPython = isWin ? path.join(venvDir, 'Scripts', 'python.exe') : path.join(venvDir, 'bin', 'python');
|
|
496
|
+
const venvPip = isWin ? path.join(venvDir, 'Scripts', 'pip.exe') : path.join(venvDir, 'bin', 'pip');
|
|
497
|
+
|
|
498
|
+
if (!fs.existsSync(venvDir)) {
|
|
499
|
+
runCmd(`${pyCmd} -m venv venv`);
|
|
280
500
|
}
|
|
281
501
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const venvPip = isWin
|
|
287
|
-
? path.join(venvDir, 'Scripts', 'pip.exe')
|
|
288
|
-
: path.join(venvDir, 'bin', 'pip');
|
|
289
|
-
|
|
290
|
-
if (pkgCount > 0) {
|
|
291
|
-
if (!fs.existsSync(venvDir)) {
|
|
292
|
-
spinner.start('Creating isolated Python virtual environment (.venv)...');
|
|
293
|
-
runCmd(`${sysPython} -m venv .venv`);
|
|
294
|
-
spinner.succeed(chalk.green('Virtual environment ready (.venv)'));
|
|
295
|
-
}
|
|
296
|
-
spinner.start(`Installing ${pkgCount} Python package(s)...`);
|
|
297
|
-
runCmd(`"${venvPip}" install -r requirements.txt`);
|
|
298
|
-
spinner.succeed(chalk.green('Python dependencies installed'));
|
|
299
|
-
envNote = '.venv isolated';
|
|
300
|
-
startCmd = `"${venvPython}" ${entryFile}`;
|
|
301
|
-
} else {
|
|
302
|
-
envNote = sysPython;
|
|
303
|
-
startCmd = `${sysPython} ${entryFile}`;
|
|
502
|
+
try {
|
|
503
|
+
runCmd(`"${venvPython}" -m pip install --upgrade pip setuptools wheel`);
|
|
504
|
+
} catch {
|
|
505
|
+
// non-fatal
|
|
304
506
|
}
|
|
305
507
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
508
|
+
envNote = 'venv (isolated)';
|
|
509
|
+
|
|
510
|
+
if (files.includes('requirements.txt')) {
|
|
511
|
+
spinner.text = 'Reading requirements.txt & downloading dependencies...';
|
|
512
|
+
runCmd(`"${venvPip}" install -r requirements.txt`);
|
|
513
|
+
const reqs = fs
|
|
514
|
+
.readFileSync(path.join(cwd, 'requirements.txt'), 'utf8')
|
|
515
|
+
.split(/\r?\n/)
|
|
516
|
+
.map((l) => l.trim())
|
|
517
|
+
.filter((l) => l && !l.startsWith('#'));
|
|
518
|
+
pkgCount = reqs.length;
|
|
519
|
+
spinner.succeed(style.green('Python requirements installed'));
|
|
520
|
+
} else if (detected.length > 0) {
|
|
521
|
+
pkgCount = detected.length;
|
|
522
|
+
spinner.text = `Scanning imports & downloading: ${formatPkgList(detected)}...`;
|
|
523
|
+
runCmd(`"${venvPip}" install ${detected.map((p) => `"${p}"`).join(' ')}`);
|
|
524
|
+
fs.writeFileSync(path.join(cwd, 'requirements.txt'), detected.join('\n') + '\n');
|
|
525
|
+
spinner.succeed(style.green('Dependencies installed & requirements.txt generated'));
|
|
526
|
+
} else {
|
|
527
|
+
spinner.succeed(style.green('Python environment ready (no external dependencies detected)'));
|
|
313
528
|
}
|
|
314
|
-
spinner.start('Fetching crates & compiling Rust...');
|
|
315
|
-
runCmd('cargo build');
|
|
316
|
-
spinner.succeed(chalk.green('Rust build complete'));
|
|
317
|
-
envNote = 'cargo';
|
|
318
|
-
startCmd = 'cargo run';
|
|
319
529
|
|
|
530
|
+
startCmd = `"${venvPython}" "${entryFile}"`;
|
|
320
531
|
} else {
|
|
321
|
-
|
|
532
|
+
spinner.fail(style.red('No recognizable project files found (package.json, .js/.ts, .py, or requirements.txt).'));
|
|
533
|
+
process.exit(1);
|
|
322
534
|
}
|
|
535
|
+
} catch (err) {
|
|
536
|
+
spinner.fail(style.red('Build failed.'));
|
|
537
|
+
const msg = (err && err.stderr ? String(err.stderr) : '') || (err && err.message ? err.message : String(err));
|
|
538
|
+
console.error(style.red('\nError log:\n') + msg.trim().slice(-4000));
|
|
539
|
+
process.exit(1);
|
|
540
|
+
}
|
|
323
541
|
|
|
324
|
-
|
|
542
|
+
const timeTaken = ((Date.now() - startTime) / 1000).toFixed(2);
|
|
325
543
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
544
|
+
console.log(style.gray('\n-----------------------------------------'));
|
|
545
|
+
console.log(style.bold(style.white(' BUILD SUCCESSFUL')));
|
|
546
|
+
console.log(style.gray('-----------------------------------------'));
|
|
547
|
+
console.log(` ${style.white('Stack:')} ${style.cyan(stack)}`);
|
|
548
|
+
console.log(` ${style.white('Entrypoint:')} ${style.yellow(entryDisplay || 'n/a')}`);
|
|
549
|
+
console.log(` ${style.white('Packages:')} ${style.yellow(String(pkgCount))}`);
|
|
550
|
+
if (detectedPkgs.length > 0) {
|
|
551
|
+
console.log(` ${style.white('Detected:')} ${style.gray(formatPkgList(detectedPkgs, 8))}`);
|
|
552
|
+
}
|
|
553
|
+
console.log(` ${style.white('Environment:')} ${style.blue(envNote)}`);
|
|
554
|
+
console.log(` ${style.white('Time:')} ${style.magenta(timeTaken + 's')}`);
|
|
555
|
+
console.log(style.gray('-----------------------------------------'));
|
|
333
556
|
|
|
334
|
-
|
|
335
|
-
console.
|
|
336
|
-
|
|
557
|
+
if (noRun) {
|
|
558
|
+
console.log(style.green('\n✔ Install complete (--no-run).'));
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
337
561
|
|
|
338
|
-
|
|
339
|
-
child.on('error', (err) => {
|
|
340
|
-
console.error(chalk.red.bold('\n✖ Process Error: ') + err.message);
|
|
341
|
-
});
|
|
342
|
-
child.on('exit', (code) => {
|
|
343
|
-
const color = code === 0 ? chalk.gray : chalk.red;
|
|
344
|
-
console.log(color(`\n[balcnix] Process exited with code ${code ?? 0}\n`));
|
|
345
|
-
});
|
|
562
|
+
await waitForEnter();
|
|
346
563
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
564
|
+
console.clear();
|
|
565
|
+
console.log(style.gray(`\n> executing: ${startCmd}\n`));
|
|
566
|
+
|
|
567
|
+
const child = spawn(startCmd, {
|
|
568
|
+
cwd,
|
|
569
|
+
stdio: 'inherit',
|
|
570
|
+
shell: true,
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
child.on('error', (e) => {
|
|
574
|
+
console.error(style.red(`\nFailed to start process: ${e.message}`));
|
|
353
575
|
process.exit(1);
|
|
354
|
-
}
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
child.on('close', (code) => {
|
|
579
|
+
console.log(style.gray(`\n[process exited with code ${code ?? 0}]`));
|
|
580
|
+
process.exit(code || 0);
|
|
581
|
+
});
|
|
355
582
|
}
|
|
356
583
|
|
|
357
584
|
deploy();
|
|
358
|
-
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@balcnix/fast-deploy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Fast universal dependency builder, installer, and runner",
|
|
5
5
|
"main": "bin/cli.js",
|
|
6
6
|
"bin": {
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
"bin/",
|
|
12
12
|
"README.md"
|
|
13
13
|
],
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"ora": "^5.4.1"
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=14"
|
|
17
16
|
}
|
|
18
17
|
}
|