@balcnix/fast-deploy 0.0.0-stage → 1.0.1
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/cli.js +285 -0
- package/package.json +16 -4
- package/README.md +0 -3
package/bin/cli.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
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
|
+
const chalk = require('chalk');
|
|
9
|
+
const ora = require('ora');
|
|
10
|
+
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
const command = args[0];
|
|
13
|
+
const cwd = process.cwd();
|
|
14
|
+
const isWin = process.platform === 'win32';
|
|
15
|
+
|
|
16
|
+
if (command !== 'deploy') {
|
|
17
|
+
console.log(chalk.red('\n✖ Unknown command. ') + chalk.white('Usage: ') + chalk.cyan('balcnix deploy\n'));
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Run shell commands silently
|
|
22
|
+
function runCmd(cmd) {
|
|
23
|
+
execSync(cmd, { cwd, stdio: 'pipe' });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Detect whether python3 or python is available on the machine
|
|
27
|
+
function getSystemPython() {
|
|
28
|
+
try {
|
|
29
|
+
execSync('python3 --version', { stdio: 'pipe' });
|
|
30
|
+
return 'python3';
|
|
31
|
+
} catch {
|
|
32
|
+
try {
|
|
33
|
+
execSync('python --version', { stdio: 'pipe' });
|
|
34
|
+
return 'python';
|
|
35
|
+
} catch {
|
|
36
|
+
throw new Error('Python is not installed on this system.');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Wait for Enter key press
|
|
42
|
+
function waitForEnter() {
|
|
43
|
+
return new Promise((resolve) => {
|
|
44
|
+
const rl = readline.createInterface({
|
|
45
|
+
input: process.stdin,
|
|
46
|
+
output: process.stdout,
|
|
47
|
+
});
|
|
48
|
+
rl.question(
|
|
49
|
+
chalk.bgGreen.black.bold('\n PRESS ENTER ') + chalk.green(' to launch project console... '),
|
|
50
|
+
() => {
|
|
51
|
+
rl.close();
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Scan .js / .ts files for external npm packages & ESM syntax
|
|
59
|
+
function scanNodeProject(dir) {
|
|
60
|
+
const deps = new Set();
|
|
61
|
+
let usesESM = false;
|
|
62
|
+
const ignoreDirs = new Set(['node_modules', '.git', 'dist', 'build', 'venv', '.venv']);
|
|
63
|
+
|
|
64
|
+
function walk(currentDir) {
|
|
65
|
+
for (const item of fs.readdirSync(currentDir)) {
|
|
66
|
+
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
67
|
+
const fullPath = path.join(currentDir, item);
|
|
68
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
69
|
+
walk(fullPath);
|
|
70
|
+
} else if (item.endsWith('.js') || item.endsWith('.ts')) {
|
|
71
|
+
const content = fs.readFileSync(fullPath, 'utf8');
|
|
72
|
+
if (/^\s*(?:import\s+|export\s+)/m.test(content)) {
|
|
73
|
+
usesESM = true;
|
|
74
|
+
}
|
|
75
|
+
const regex = /(?:require\(|from\s+|import\s+)['"]([^'"./][^'"]*)['"]/g;
|
|
76
|
+
let match;
|
|
77
|
+
while ((match = regex.exec(content)) !== null) {
|
|
78
|
+
let pkg = match[1];
|
|
79
|
+
pkg = pkg.startsWith('@') ? pkg.split('/').slice(0, 2).join('/') : pkg.split('/')[0];
|
|
80
|
+
const clean = pkg.replace(/^node:/, '');
|
|
81
|
+
if (!builtinModules.includes(clean)) deps.add(pkg);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
walk(dir);
|
|
87
|
+
return { deps: Array.from(deps), usesESM };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Scan .py files for external pip packages
|
|
91
|
+
function scanPythonDeps(dir) {
|
|
92
|
+
const deps = new Set();
|
|
93
|
+
const stdLib = new Set([
|
|
94
|
+
'os', 'sys', 'json', 'math', 'time', 'datetime', 're', 'random', 'pathlib',
|
|
95
|
+
'subprocess', 'threading', 'asyncio', 'typing', 'collections', 'itertools',
|
|
96
|
+
'functools', 'urllib', 'http', 'sqlite3', 'csv', 'logging', 'unittest',
|
|
97
|
+
'shutil', 'glob', 'hashlib', 'hmac', 'base64', 'uuid', 'socket', 'ssl',
|
|
98
|
+
'argparse', 'configparser', 'io', 'tempfile', 'traceback', 'inspect'
|
|
99
|
+
]);
|
|
100
|
+
const ignoreDirs = new Set(['venv', '.venv', '__pycache__', '.git', 'node_modules']);
|
|
101
|
+
|
|
102
|
+
function walk(currentDir) {
|
|
103
|
+
for (const item of fs.readdirSync(currentDir)) {
|
|
104
|
+
if (ignoreDirs.has(item) || item.startsWith('.')) continue;
|
|
105
|
+
const fullPath = path.join(currentDir, item);
|
|
106
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
107
|
+
walk(fullPath);
|
|
108
|
+
} else if (item.endsWith('.py')) {
|
|
109
|
+
const content = fs.readFileSync(fullPath, 'utf8');
|
|
110
|
+
const regex = /^\s*(?:import|from)\s+([a-zA-Z0-9_]+)/gm;
|
|
111
|
+
let match;
|
|
112
|
+
while ((match = regex.exec(content)) !== null) {
|
|
113
|
+
const mod = match[1];
|
|
114
|
+
if (!stdLib.has(mod)) deps.add(mod);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
walk(dir);
|
|
120
|
+
return Array.from(deps);
|
|
121
|
+
}
|
|
122
|
+
async function deploy() {
|
|
123
|
+
const startTime = Date.now();
|
|
124
|
+
console.clear();
|
|
125
|
+
console.log(chalk.bold.white('\n> balcnix deploy ') + chalk.gray('v1.0.1\n'));
|
|
126
|
+
|
|
127
|
+
const spinner = ora({ text: 'Fetching & scanning project files...', color: 'cyan' }).start();
|
|
128
|
+
await new Promise((r) => setTimeout(r, 350));
|
|
129
|
+
|
|
130
|
+
const files = fs.readdirSync(cwd);
|
|
131
|
+
spinner.succeed(chalk.gray(`Scanned ${files.length} items in ${path.basename(cwd)}`));
|
|
132
|
+
|
|
133
|
+
let stack = '';
|
|
134
|
+
let startCmd = '';
|
|
135
|
+
let pkgCount = 0;
|
|
136
|
+
let envNote = 'default';
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
// ================= NODE.JS =================
|
|
140
|
+
if (files.includes('package.json') || files.some((f) => f.endsWith('.js') || f.endsWith('.ts'))) {
|
|
141
|
+
stack = 'Node.js';
|
|
142
|
+
const entryFile =
|
|
143
|
+
files.find((f) => ['index.js', 'app.js', 'server.js', 'main.js'].includes(f)) ||
|
|
144
|
+
files.find((f) => f.endsWith('.js'));
|
|
145
|
+
|
|
146
|
+
if (!files.includes('package.json')) {
|
|
147
|
+
spinner.start('No package.json found. Scanning imports & building...');
|
|
148
|
+
const { deps: detected, usesESM } = scanNodeProject(cwd);
|
|
149
|
+
pkgCount = detected.length;
|
|
150
|
+
|
|
151
|
+
runCmd('npm init -y');
|
|
152
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
153
|
+
const pkgData = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
154
|
+
if (entryFile) {
|
|
155
|
+
pkgData.main = entryFile;
|
|
156
|
+
pkgData.scripts = { start: `node ${entryFile}` };
|
|
157
|
+
}
|
|
158
|
+
if (usesESM) {
|
|
159
|
+
pkgData.type = 'module';
|
|
160
|
+
envNote = 'ES Modules';
|
|
161
|
+
}
|
|
162
|
+
fs.writeFileSync(pkgPath, JSON.stringify(pkgData, null, 2));
|
|
163
|
+
spinner.succeed(chalk.green(`Built package.json (${pkgCount} dependencies detected)`));
|
|
164
|
+
|
|
165
|
+
if (detected.length > 0) {
|
|
166
|
+
spinner.start(`Downloading packages: ${detected.join(', ')}...`);
|
|
167
|
+
runCmd(`npm install ${detected.join(' ')}`);
|
|
168
|
+
spinner.succeed(chalk.green('All Node.js dependencies installed'));
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
spinner.start('Downloading dependencies from package.json...');
|
|
172
|
+
runCmd('npm install');
|
|
173
|
+
const pkgData = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
|
|
174
|
+
pkgCount = Object.keys(pkgData.dependencies || {}).length;
|
|
175
|
+
spinner.succeed(chalk.green('Dependencies installed'));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const finalPkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
|
|
179
|
+
startCmd =
|
|
180
|
+
finalPkg.scripts && finalPkg.scripts.start
|
|
181
|
+
? 'npm start'
|
|
182
|
+
: `node ${finalPkg.main || entryFile || 'index.js'}`;
|
|
183
|
+
|
|
184
|
+
// ================= PYTHON =================
|
|
185
|
+
} else if (files.includes('requirements.txt') || files.some((f) => f.endsWith('.py'))) {
|
|
186
|
+
stack = 'Python';
|
|
187
|
+
const sysPython = getSystemPython();
|
|
188
|
+
const pyEntry =
|
|
189
|
+
files.find((f) => ['main.py', 'app.py', 'run.py', 'ai.py'].includes(f)) ||
|
|
190
|
+
files.find((f) => f.endsWith('.py'));
|
|
191
|
+
|
|
192
|
+
if (!files.includes('requirements.txt')) {
|
|
193
|
+
spinner.start('No requirements.txt found. Scanning imports & building...');
|
|
194
|
+
const detected = scanPythonDeps(cwd);
|
|
195
|
+
pkgCount = detected.length;
|
|
196
|
+
fs.writeFileSync(path.join(cwd, 'requirements.txt'), detected.join('\n'));
|
|
197
|
+
spinner.succeed(chalk.green(`Built requirements.txt (${pkgCount} packages detected)`));
|
|
198
|
+
} else {
|
|
199
|
+
const reqs = fs.readFileSync(path.join(cwd, 'requirements.txt'), 'utf8')
|
|
200
|
+
.split('\n')
|
|
201
|
+
.map((l) => l.trim())
|
|
202
|
+
.filter((l) => l && !l.startsWith('#'));
|
|
203
|
+
pkgCount = reqs.length;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Create/use isolated .venv so Fedora/Ubuntu never throws externally-managed-environment error
|
|
207
|
+
const venvDir = path.join(cwd, '.venv');
|
|
208
|
+
const venvPython = isWin
|
|
209
|
+
? path.join(venvDir, 'Scripts', 'python.exe')
|
|
210
|
+
: path.join(venvDir, 'bin', 'python');
|
|
211
|
+
const venvPip = isWin
|
|
212
|
+
? path.join(venvDir, 'Scripts', 'pip.exe')
|
|
213
|
+
: path.join(venvDir, 'bin', 'pip');
|
|
214
|
+
|
|
215
|
+
if (pkgCount > 0) {
|
|
216
|
+
if (!fs.existsSync(venvDir)) {
|
|
217
|
+
spinner.start('Creating isolated Python virtual environment (.venv)...');
|
|
218
|
+
runCmd(`${sysPython} -m venv .venv`);
|
|
219
|
+
spinner.succeed(chalk.green('Virtual environment ready (.venv)'));
|
|
220
|
+
}
|
|
221
|
+
spinner.start(`Installing ${pkgCount} Python package(s)...`);
|
|
222
|
+
runCmd(`"${venvPip}" install -r requirements.txt`);
|
|
223
|
+
spinner.succeed(chalk.green('Python dependencies installed'));
|
|
224
|
+
envNote = '.venv isolated';
|
|
225
|
+
startCmd = `"${venvPython}" ${pyEntry}`;
|
|
226
|
+
} else {
|
|
227
|
+
envNote = sysPython;
|
|
228
|
+
startCmd = `${sysPython} ${pyEntry}`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ================= RUST =================
|
|
232
|
+
} else if (files.includes('Cargo.toml') || files.some((f) => f.endsWith('.rs'))) {
|
|
233
|
+
stack = 'Rust';
|
|
234
|
+
if (!files.includes('Cargo.toml')) {
|
|
235
|
+
spinner.start('No Cargo.toml found. Initializing Rust project...');
|
|
236
|
+
runCmd('cargo init');
|
|
237
|
+
spinner.succeed(chalk.green('Generated Cargo.toml'));
|
|
238
|
+
}
|
|
239
|
+
spinner.start('Fetching crates & compiling Rust...');
|
|
240
|
+
runCmd('cargo build');
|
|
241
|
+
spinner.succeed(chalk.green('Rust build complete'));
|
|
242
|
+
envNote = 'cargo';
|
|
243
|
+
startCmd = 'cargo run';
|
|
244
|
+
|
|
245
|
+
// ================= UNSUPPORTED =================
|
|
246
|
+
} else {
|
|
247
|
+
throw new Error('No supported project files (.js, .ts, .py, .rs) found in this directory.');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
251
|
+
|
|
252
|
+
// Sleek Dashboard Summary
|
|
253
|
+
console.log(chalk.green.bold(`\n> Ready in ${elapsed}s → `) + chalk.white(path.basename(cwd)));
|
|
254
|
+
console.log(chalk.gray('──────────────────────────────────────────────'));
|
|
255
|
+
console.log(`${chalk.gray('stack')} ${chalk.cyan(stack)} ${chalk.gray(`(${envNote})`)}`);
|
|
256
|
+
console.log(`${chalk.gray('packages')} ${chalk.white(pkgCount + ' synced')}`);
|
|
257
|
+
console.log(`${chalk.gray('command')} ${chalk.yellow(path.basename(startCmd.replace(/"/g, '')))}`);
|
|
258
|
+
console.log(`${chalk.gray('status')} ${chalk.green('100% ready')}`);
|
|
259
|
+
console.log(chalk.gray('──────────────────────────────────────────────'));
|
|
260
|
+
|
|
261
|
+
// Wait for Enter, then drop into live project console
|
|
262
|
+
await waitForEnter();
|
|
263
|
+
console.clear();
|
|
264
|
+
console.log(chalk.gray(`Launching project in ${cwd}...\n`));
|
|
265
|
+
|
|
266
|
+
const child = spawn(startCmd, { cwd, stdio: 'inherit', shell: true });
|
|
267
|
+
child.on('error', (err) => {
|
|
268
|
+
console.error(chalk.red.bold('\n✖ Process Error: ') + err.message);
|
|
269
|
+
});
|
|
270
|
+
child.on('exit', (code) => {
|
|
271
|
+
const color = code === 0 ? chalk.gray : chalk.red;
|
|
272
|
+
console.log(color(`\n[balcnix] Process exited with code ${code ?? 0}\n`));
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
} catch (err) {
|
|
276
|
+
spinner.fail(chalk.red.bold('Deployment failed'));
|
|
277
|
+
console.error(chalk.bgRed.white.bold('\n ERROR ') + ' ' + chalk.red(err.message));
|
|
278
|
+
if (err.stderr && err.stderr.length > 0) {
|
|
279
|
+
console.error(chalk.gray('\nDetails:\n' + err.stderr.toString().trim()));
|
|
280
|
+
}
|
|
281
|
+
process.exit(1);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
deploy();
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@balcnix/fast-deploy",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Fast universal dependency builder, installer, and runner",
|
|
5
|
+
"main": "bin/cli.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"balcnix": "bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"preferGlobal": true,
|
|
10
|
+
"files": [
|
|
11
|
+
"bin/",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"chalk": "^4.1.2",
|
|
16
|
+
"ora": "^5.4.1"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/README.md
DELETED