98 lines
2.3 KiB
TypeScript
Executable File
98 lines
2.3 KiB
TypeScript
Executable File
#!/usr/bin/env bun
|
|
import minimist from "minimist";
|
|
import path from "path";
|
|
|
|
import { generateEnvTypes } from "../generate/env.generate";
|
|
import checkPort from "./src/port";
|
|
import route from "./src/route";
|
|
import compose from "./src/compose";
|
|
import generateDockerfile from "./src/docker-file";
|
|
|
|
interface CheckPortResult {
|
|
port: number;
|
|
open: boolean;
|
|
}
|
|
|
|
const args = minimist(process.argv.slice(2));
|
|
|
|
const help = `
|
|
g3n [command] [options]
|
|
|
|
Commands:
|
|
env Generate env.d.ts from .env file
|
|
scan-port Scan port range (default 3000-4000)
|
|
route Generate routes.ts from AppRoutes.tsx
|
|
compose Generate compose.yml from name
|
|
Options:
|
|
--env Path ke file .env (default: .env)
|
|
--out Path file output (default: types/env.d.ts)
|
|
--start Port awal scan (default: 3000)
|
|
--end Port akhir scan (default: 4000)
|
|
--host Host/IP target (default: 127.0.0.1)
|
|
|
|
Examples:
|
|
g3n env --env .env.local --out src/types/env.d.ts
|
|
g3n scan-port --start 7700 --end 7800 --host 127.0.0.1
|
|
g3n route
|
|
g3n compose <name>`;
|
|
|
|
(async () => {
|
|
const cmd = args._[0];
|
|
|
|
if (cmd === "env") {
|
|
generateEnvTypes({
|
|
envFilePath: args.env,
|
|
outputDir: args.out ? path.dirname(args.out) : undefined,
|
|
outputFileName: args.out ? path.basename(args.out) : undefined,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (cmd === "scan-port") {
|
|
const start: number = args.start ? parseInt(args.start, 10) : 3000;
|
|
const end: number = args.end ? parseInt(args.end, 10) : 4000;
|
|
const host: string = args.host || "localhost";
|
|
|
|
console.log(`🔍 Scan port ${start}-${end} di host ${host} ...`);
|
|
|
|
const ports: number[] = Array.from(
|
|
{ length: end - start + 1 },
|
|
(_, i) => start + i
|
|
);
|
|
|
|
const results: CheckPortResult[] = await Promise.all(
|
|
ports.map((p) => checkPort(p, host))
|
|
);
|
|
|
|
results.filter((r) => r.open).forEach((r) => {
|
|
console.log(`✅ Port ${r.port} sedang digunakan`);
|
|
});
|
|
|
|
console.log("✅ Selesai");
|
|
return;
|
|
}
|
|
|
|
if (cmd === "route") {
|
|
route();
|
|
return;
|
|
}
|
|
|
|
if (cmd === "compose") {
|
|
if (!args._[1]) {
|
|
console.error("❌ Name is required");
|
|
return;
|
|
}
|
|
compose(args._[1] as string);
|
|
return;
|
|
}
|
|
|
|
if (cmd === "docker-file") {
|
|
generateDockerfile();
|
|
return;
|
|
}
|
|
|
|
console.error(help);
|
|
})();
|
|
|
|
|