@balcnix/fast-deploy 1.0.2 → 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 +529 -108
- 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,163 +1,584 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { execSync, spawn } = require('child_process');
|
|
6
|
+
const { builtinModules } = require('module');
|
|
7
|
+
const readline = require('readline');
|
|
8
|
+
|
|
9
|
+
const args = process.argv.slice(2);
|
|
10
|
+
const command = args[0];
|
|
11
|
+
const cwd = process.cwd();
|
|
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
|
+
}
|
|
62
|
+
|
|
63
|
+
if (command !== 'deploy') {
|
|
64
|
+
console.log(style.red(`\n✖ Unknown command: ${command}`));
|
|
65
|
+
printHelp();
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
|
|
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 });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getSystemPython() {
|
|
132
|
+
const candidates = isWin ? ['py -3', 'python', 'python3'] : ['python3', 'python'];
|
|
133
|
+
for (const cmd of candidates) {
|
|
134
|
+
try {
|
|
135
|
+
runCmd(`${cmd} --version`);
|
|
136
|
+
return cmd;
|
|
137
|
+
} catch {
|
|
138
|
+
// try next
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
throw new Error('Python is not installed on this system. Install Python 3 and try again.');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function waitForEnter() {
|
|
145
|
+
if (!process.stdin.isTTY) return Promise.resolve();
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
const rl = readline.createInterface({
|
|
148
|
+
input: process.stdin,
|
|
149
|
+
output: process.stdout,
|
|
150
|
+
});
|
|
151
|
+
rl.question(
|
|
152
|
+
style.bgGreenBlack('\n PRESS ENTER ') + style.green(' to launch project console... '),
|
|
153
|
+
() => {
|
|
154
|
+
rl.close();
|
|
155
|
+
resolve();
|
|
156
|
+
}
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
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
|
+
|
|
177
|
+
function scanNodeProject(dir) {
|
|
178
|
+
const deps = new Set();
|
|
179
|
+
const importedLocals = new Set();
|
|
180
|
+
const rootJsFiles = [];
|
|
181
|
+
let usesESM = false;
|
|
182
|
+
const ignoreDirs = new Set(['node_modules', '.git', 'dist', 'build', 'venv', '.venv', '__pycache__', '.next', 'coverage', '.turbo', 'out']);
|
|
183
|
+
|
|
184
|
+
function walk(currentDir, isRoot = false) {
|
|
185
|
+
for (const item of safeReadDir(currentDir)) {
|
|
186
|
+
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
187
|
+
const fullPath = path.join(currentDir, item);
|
|
188
|
+
if (isDirectory(fullPath)) {
|
|
189
|
+
walk(fullPath, false);
|
|
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);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
walk(dir, true);
|
|
234
|
+
|
|
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;
|
|
238
|
+
|
|
239
|
+
for (const file of rootJsFiles) {
|
|
240
|
+
let score = 0;
|
|
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
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!importedLocals.has(base)) score += 50;
|
|
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;
|
|
255
|
+
|
|
256
|
+
if (score > bestScore) {
|
|
257
|
+
bestScore = score;
|
|
258
|
+
bestEntry = file.name;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return { deps: Array.from(deps).sort(), usesESM, entryFile: bestEntry };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function scanPythonProject(dir) {
|
|
266
|
+
const rawImports = new Set();
|
|
267
|
+
const localModules = new Set();
|
|
268
|
+
const importedLocals = new Set();
|
|
269
|
+
const rootPyFiles = [];
|
|
270
|
+
|
|
271
|
+
const pypiMap = {
|
|
272
|
+
discord: 'discord.py',
|
|
273
|
+
PIL: 'Pillow',
|
|
274
|
+
cv2: 'opencv-python',
|
|
275
|
+
yaml: 'PyYAML',
|
|
276
|
+
bs4: 'beautifulsoup4',
|
|
277
|
+
dotenv: 'python-dotenv',
|
|
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',
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const stdLib = new Set([
|
|
288
|
+
'__future__', 'os', 'sys', 'json', 'math', 'time', 'datetime', 're', 'random',
|
|
289
|
+
'pathlib', 'subprocess', 'threading', 'asyncio', 'typing', 'collections',
|
|
290
|
+
'itertools', 'functools', 'urllib', 'http', 'sqlite3', 'csv', 'logging',
|
|
291
|
+
'unittest', 'shutil', 'glob', 'hashlib', 'hmac', 'base64', 'uuid', 'socket',
|
|
292
|
+
'ssl', 'argparse', 'configparser', 'io', 'tempfile', 'traceback', 'inspect',
|
|
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'
|
|
297
|
+
]);
|
|
298
|
+
|
|
299
|
+
const ignoreDirs = new Set(['venv', '.venv', '__pycache__', '.git', 'node_modules', 'dist', 'build', '.mypy_cache', '.pytest_cache', '.tox']);
|
|
300
|
+
|
|
301
|
+
function walk(currentDir, isRoot = false) {
|
|
302
|
+
for (const item of safeReadDir(currentDir)) {
|
|
303
|
+
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
304
|
+
const fullPath = path.join(currentDir, item);
|
|
305
|
+
if (isDirectory(fullPath)) {
|
|
306
|
+
localModules.add(item);
|
|
307
|
+
walk(fullPath, false);
|
|
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);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
walk(dir, true);
|
|
334
|
+
|
|
335
|
+
const externalDeps = [];
|
|
336
|
+
for (const mod of rawImports) {
|
|
337
|
+
if (!stdLib.has(mod) && !localModules.has(mod)) {
|
|
338
|
+
externalDeps.push(pypiMap[mod] || mod);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
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;
|
|
345
|
+
|
|
346
|
+
for (const file of rootPyFiles) {
|
|
347
|
+
let score = 0;
|
|
348
|
+
let content = '';
|
|
349
|
+
try {
|
|
350
|
+
content = fs.readFileSync(file.fullPath, 'utf8');
|
|
351
|
+
} catch {
|
|
352
|
+
content = '';
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (!importedLocals.has(file.modName)) score += 60;
|
|
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;
|
|
359
|
+
|
|
360
|
+
let localImportCount = 0;
|
|
361
|
+
for (const loc of localModules) {
|
|
362
|
+
if (loc !== file.modName && new RegExp(`^\\s*(?:from|import)\\s+${loc}\\b`, 'm').test(content)) {
|
|
363
|
+
localImportCount++;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
score += Math.min(localImportCount, 5) * 4;
|
|
367
|
+
if (['__init__.py', 'setup.py', 'conftest.py'].includes(file.name)) score -= 100;
|
|
368
|
+
|
|
369
|
+
if (score > bestScore) {
|
|
370
|
+
bestScore = score;
|
|
371
|
+
bestEntry = file.name;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
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
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
1
393
|
async function deploy() {
|
|
2
394
|
const startTime = Date.now();
|
|
3
395
|
console.clear();
|
|
4
|
-
console.log(
|
|
396
|
+
console.log(style.bold(style.white('\n> balcnix deploy ')) + style.gray(`v${getVersion()}\n`));
|
|
5
397
|
|
|
6
|
-
const spinner =
|
|
7
|
-
await new Promise((r) => setTimeout(r,
|
|
398
|
+
const spinner = makeSpinner().start('Fetching & scanning project files...');
|
|
399
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
8
400
|
|
|
9
|
-
const files =
|
|
10
|
-
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}`));
|
|
11
403
|
|
|
12
404
|
let stack = '';
|
|
13
405
|
let startCmd = '';
|
|
14
406
|
let entryDisplay = '';
|
|
15
407
|
let pkgCount = 0;
|
|
16
408
|
let envNote = 'default';
|
|
409
|
+
let detectedPkgs = [];
|
|
17
410
|
|
|
18
411
|
try {
|
|
19
|
-
|
|
20
|
-
|
|
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) {
|
|
21
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
|
+
|
|
22
424
|
const { deps: detected, usesESM, entryFile } = scanNodeProject(cwd);
|
|
23
425
|
entryDisplay = entryFile;
|
|
426
|
+
detectedPkgs = detected;
|
|
24
427
|
|
|
25
|
-
if (!
|
|
428
|
+
if (!hasPackageJson) {
|
|
26
429
|
spinner.start('No package.json found. Scanning imports & building...');
|
|
27
430
|
pkgCount = detected.length;
|
|
28
431
|
|
|
29
432
|
runCmd('npm init -y');
|
|
30
433
|
const pkgPath = path.join(cwd, 'package.json');
|
|
31
434
|
const pkgData = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
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'}` };
|
|
36
439
|
if (usesESM) {
|
|
37
440
|
pkgData.type = 'module';
|
|
38
441
|
envNote = 'ES Modules';
|
|
442
|
+
} else {
|
|
443
|
+
envNote = 'CommonJS';
|
|
39
444
|
}
|
|
40
445
|
fs.writeFileSync(pkgPath, JSON.stringify(pkgData, null, 2));
|
|
41
|
-
spinner.succeed(
|
|
446
|
+
spinner.succeed(style.green(`Built package.json (${pkgCount} dependencies detected)`));
|
|
42
447
|
|
|
43
448
|
if (detected.length > 0) {
|
|
44
|
-
spinner.start(`Downloading packages: ${detected
|
|
45
|
-
runCmd(`npm install ${detected.join(' ')}`);
|
|
46
|
-
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'));
|
|
47
454
|
}
|
|
48
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
|
+
|
|
49
467
|
spinner.start('Downloading dependencies from package.json...');
|
|
50
468
|
runCmd('npm install');
|
|
51
|
-
const
|
|
52
|
-
pkgCount = Object.keys(
|
|
53
|
-
|
|
54
|
-
|
|
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'));
|
|
55
474
|
}
|
|
56
475
|
|
|
57
476
|
const finalPkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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) {
|
|
65
486
|
stack = 'Python';
|
|
66
|
-
const sysPython = getSystemPython();
|
|
67
487
|
const { deps: detected, entryFile } = scanPythonProject(cwd);
|
|
68
488
|
entryDisplay = entryFile;
|
|
489
|
+
detectedPkgs = detected;
|
|
69
490
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
.filter((l) => l && !l.startsWith('#'));
|
|
80
|
-
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`);
|
|
81
500
|
}
|
|
82
501
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const venvPip = isWin
|
|
88
|
-
? path.join(venvDir, 'Scripts', 'pip.exe')
|
|
89
|
-
: path.join(venvDir, 'bin', 'pip');
|
|
90
|
-
|
|
91
|
-
if (pkgCount > 0) {
|
|
92
|
-
if (!fs.existsSync(venvDir)) {
|
|
93
|
-
spinner.start('Creating isolated Python virtual environment (.venv)...');
|
|
94
|
-
runCmd(`${sysPython} -m venv .venv`);
|
|
95
|
-
spinner.succeed(chalk.green('Virtual environment ready (.venv)'));
|
|
96
|
-
}
|
|
97
|
-
spinner.start(`Installing ${pkgCount} Python package(s)...`);
|
|
98
|
-
runCmd(`"${venvPip}" install -r requirements.txt`);
|
|
99
|
-
spinner.succeed(chalk.green('Python dependencies installed'));
|
|
100
|
-
envNote = '.venv isolated';
|
|
101
|
-
startCmd = `"${venvPython}" ${entryFile}`;
|
|
102
|
-
} else {
|
|
103
|
-
envNote = sysPython;
|
|
104
|
-
startCmd = `${sysPython} ${entryFile}`;
|
|
502
|
+
try {
|
|
503
|
+
runCmd(`"${venvPython}" -m pip install --upgrade pip setuptools wheel`);
|
|
504
|
+
} catch {
|
|
505
|
+
// non-fatal
|
|
105
506
|
}
|
|
106
507
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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)'));
|
|
115
528
|
}
|
|
116
|
-
spinner.start('Fetching crates & compiling Rust...');
|
|
117
|
-
runCmd('cargo build');
|
|
118
|
-
spinner.succeed(chalk.green('Rust build complete'));
|
|
119
|
-
envNote = 'cargo';
|
|
120
|
-
startCmd = 'cargo run';
|
|
121
529
|
|
|
122
|
-
|
|
530
|
+
startCmd = `"${venvPython}" "${entryFile}"`;
|
|
123
531
|
} else {
|
|
124
|
-
|
|
532
|
+
spinner.fail(style.red('No recognizable project files found (package.json, .js/.ts, .py, or requirements.txt).'));
|
|
533
|
+
process.exit(1);
|
|
125
534
|
}
|
|
126
|
-
|
|
127
|
-
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
128
|
-
|
|
129
|
-
// Sleek Dashboard Summary
|
|
130
|
-
console.log(chalk.green.bold(`\n> Ready in ${elapsed}s → `) + chalk.white(path.basename(cwd)));
|
|
131
|
-
console.log(chalk.gray('──────────────────────────────────────────────'));
|
|
132
|
-
console.log(`${chalk.gray('stack')} ${chalk.cyan(stack)} ${chalk.gray(`(${envNote})`)}`);
|
|
133
|
-
console.log(`${chalk.gray('entry')} ${chalk.magenta(entryDisplay)}`);
|
|
134
|
-
console.log(`${chalk.gray('packages')} ${chalk.white(pkgCount + ' synced')}`);
|
|
135
|
-
console.log(`${chalk.gray('status')} ${chalk.green('100% ready')}`);
|
|
136
|
-
console.log(chalk.gray('──────────────────────────────────────────────'));
|
|
137
|
-
|
|
138
|
-
// Wait for Enter, then drop into live project console
|
|
139
|
-
await waitForEnter();
|
|
140
|
-
console.clear();
|
|
141
|
-
console.log(chalk.gray(`Launching ${entryDisplay} in ${cwd}...\n`));
|
|
142
|
-
|
|
143
|
-
const child = spawn(startCmd, { cwd, stdio: 'inherit', shell: true });
|
|
144
|
-
child.on('error', (err) => {
|
|
145
|
-
console.error(chalk.red.bold('\n✖ Process Error: ') + err.message);
|
|
146
|
-
});
|
|
147
|
-
child.on('exit', (code) => {
|
|
148
|
-
const color = code === 0 ? chalk.gray : chalk.red;
|
|
149
|
-
console.log(color(`\n[balcnix] Process exited with code ${code ?? 0}\n`));
|
|
150
|
-
});
|
|
151
|
-
|
|
152
535
|
} catch (err) {
|
|
153
|
-
spinner.fail(
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
console.error(chalk.gray('\nDetails:\n' + err.stderr.toString().trim()));
|
|
157
|
-
}
|
|
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));
|
|
158
539
|
process.exit(1);
|
|
159
540
|
}
|
|
541
|
+
|
|
542
|
+
const timeTaken = ((Date.now() - startTime) / 1000).toFixed(2);
|
|
543
|
+
|
|
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('-----------------------------------------'));
|
|
556
|
+
|
|
557
|
+
if (noRun) {
|
|
558
|
+
console.log(style.green('\n✔ Install complete (--no-run).'));
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
await waitForEnter();
|
|
563
|
+
|
|
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}`));
|
|
575
|
+
process.exit(1);
|
|
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
|
+
});
|
|
160
582
|
}
|
|
161
583
|
|
|
162
584
|
deploy();
|
|
163
|
-
|
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
|
}
|