-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·196 lines (166 loc) · 6.03 KB
/
index.js
File metadata and controls
executable file
·196 lines (166 loc) · 6.03 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
import inquirer from "inquirer";
import chalk from "chalk";
import gradient from "gradient-string";
import figlet from "figlet";
import ora from "ora";
import boxen from "boxen";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { createProject } from "./commands/scaffold.js";
import { loginCommand } from "./commands/login.js";
import { gatherCustomConfig } from "./prompts/index.js";
import { isPromptCancellation } from "./utils/shared.js";
import { askStackQuestions } from "./prompts/stack/stack.js";
import { askProjectName } from "./prompts/user/projectName.js";
import { showBanner } from "./prompts/stable/showBanner.js";
import { getStackMeta } from "./prompts/stable/getStackMeta.js";
import { askRuntimeEnvironment } from "./prompts/stack/runtime.js";
import { askPackageManager } from "./prompts/common/askPackageManager.js";
import { showVersion } from "./prompts/info/showVersion.js";
import { showHelp } from "./prompts/info/showHelp.js";
import { formatElapsed } from "./prompts/info/formatElapsed.js";
import { showSummaryBox } from "./prompts/info/summary.js";
import { parseArgs } from "./prompts/stable/parseArgs.js";
import { detectPackageManager } from "./prompts/stable/detectPackageManager.js";
const orange = chalk.hex("#FF6200");
const quickTemplates = {
"mern-js": { stack: "mern", language: "javascript" },
"mern-ts": { stack: "mern", language: "typescript" }
};
const isVerbose = process.argv.includes("--verbose");
async function main() {
const args = parseArgs();
// Handle version flag
if (args.includes('--version') || args.includes('-v')) {
showVersion();
process.exit(0);
}
// Handle help flag
if (args.includes('--help') || args.includes('-h')) {
showHelp();
process.exit(0);
}
if (args.includes('login')) {
await loginCommand();
process.exit(0);
}
showBanner();
let projectName = args.find(arg => !arg.startsWith('--') && !arg.startsWith('-'));
let packageManager = detectPackageManager();
let config;
const quickKey = args[0];
let isQuick = false;
let quickConfig = null;
if (quickTemplates[quickKey]) {
isQuick = true;
quickConfig = quickTemplates[quickKey];
}
try {
if (isQuick) {
// QUICK MODE (mern-js / mern-ts)
console.log(chalk.green(`⚡ Using quick template: ${quickKey}`));
projectName = args[1] || (await askProjectName());
packageManager = (await askPackageManager()).packageManager;
const runtimeAnswers = await askRuntimeEnvironment();
config = {
stack: quickConfig.stack, // always mern
language: quickConfig.language, // js or ts
projectName,
packageManager,
runtime: runtimeAnswers.runtime
};
} else {
// NORMAL MODE
if (!projectName) {
projectName = await askProjectName();
}
const stackAnswers = await askStackQuestions();
if (stackAnswers.stack === "custom") {
// ── Custom Stack Flow ──
console.log(chalk.gray("\n── Customise your tech stack ──\n"));
const customConfig = await gatherCustomConfig();
packageManager = (await askPackageManager()).packageManager;
config = { stack: "custom", ...customConfig, projectName, packageManager };
} else {
// ── Preset Stack Flow ──
const runtimeAnswers = await askRuntimeEnvironment();
packageManager = (await askPackageManager()).packageManager;
config = { ...stackAnswers, ...runtimeAnswers, projectName, packageManager };
}
}
if (config.stack !== "custom") {
const { backend: stackBackend } = getStackMeta(config.stack);
if (!stackBackend) {
console.log(chalk.yellow("⚠️ Note: This stack is frontend-only — no backend server will be created."));
}
}
// Ask whether to install dependencies (handled in main script)
const { installDeps } = await inquirer.prompt([
{
type: "confirm",
name: "installDeps",
message: "Do you want to install dependencies?",
default: true,
},
]);
// Ask whether to initialize a git repo
const { initGit } = await inquirer.prompt([
{
type: "confirm",
name: "initGit",
message: "Initialize a git repository?",
default: true,
},
]);
// --- Scaffold with spinner + timing ---
const startTime = Date.now();
const scaffoldSpinner = ora({
text: chalk.yellow("Scaffolding your project…"),
spinner: "dots12",
}).start();
try {
await createProject(projectName, config, installDeps);
const elapsed = Date.now() - startTime;
scaffoldSpinner.succeed(
chalk.green(`Project scaffolded in ${formatElapsed(elapsed)}`)
);
} catch (err) {
scaffoldSpinner.fail(chalk.red("Scaffolding failed"));
throw err;
}
// --- Git init ---
if (initGit) {
const projectPath = path.join(process.cwd(), projectName);
try {
const { execSync } = await import("child_process");
execSync("git init", { cwd: projectPath, stdio: "ignore" });
execSync("git add .", { cwd: projectPath, stdio: "ignore" });
execSync('git commit -m "Initial commit"', { cwd: projectPath, stdio: "ignore" });
console.log(chalk.green("\n🎉 Git repository initialized with initial commit."));
} catch {
console.log(chalk.yellow("\n⚠️ Could not initialize git — you can run 'git init' manually."));
}
}
// --- Summary box ---
const totalElapsed = Date.now() - startTime;
showSummaryBox({
projectName,
config,
installedDeps: installDeps,
elapsed: totalElapsed,
});
} catch (err) {
// Graceful cancellation (Ctrl+C during any prompt)
if (isPromptCancellation(err)) {
console.log(chalk.yellow("\n👋 Cancelled — see you next time!\n"));
process.exit(0);
}
console.log(chalk.red("❌ Error:"), err.message);
if (isVerbose && err.stack) {
console.log(chalk.gray(err.stack));
}
process.exit(1);
}
}
main();