-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (190 loc) · 7.05 KB
/
index.js
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
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env node
// Importing necessary modules
const { program } = require('commander');
const askQuestion = require('./init'); // Function to initialize questions
const { default: chalk } = require('chalk');
const { default: inquirer } = require('inquirer');
const readline = require('readline'); // Moved here so we can reinitialize it later
const figlet = require("figlet")
printBanner();
function printBanner() {
const msg = "Node - InitDB";
figlet(msg, (err, data) => {
if (err) {
console.log("Figlet error:", err);
return;
}
console.log();
console.log(chalk.cyan.bold(data));
console.log();
console.log(chalk.yellow.bold("🚀 Initialize your Node.js project in seconds!"));
console.log(chalk.green("✨ Choose a database, web framework, and language."));
console.log();
console.log(
chalk.magentaBright("Usage: ") +
chalk.yellow("node-initdb ") +
chalk.white("-m <-m or --mongo | -s or --seque> -e <-e or --express | -f or --fastify | -el or --elysia> -j <-j or --javascript | -t or --typescript> -n <-n or --npm | -ya or --yarn | -b or --bun | -pn or --pnpm>")
);
console.log();
console.log(chalk.blue("Examples:"));
console.log(chalk.cyan(" node-initdb -m -e -t -n") + chalk.gray(" # MongoDB + Express + TypeScript + npm"));
console.log(chalk.cyan(" node-initdb -s -f -j -b") + chalk.gray(" # Sequelize + Fastify + JavaScript + bun"));
console.log();
console.log();
console.log();
console.log(chalk.yellow.bold("🚀 Add Module In Your Project!"));
console.log(chalk.green("📂 Generates controllers, models, routes, auth & file uploads."));
console.log();
console.log(
chalk.magentaBright("Usage: ") +
chalk.yellow("node-add ") +
chalk.white(`"<module-name>"`) + chalk.gray(` -m <-m or --mongo | -s or --seque> -e <-e or --express | -f or --fastify | -el or --elysia> -j <-j or --javascript | -t or --typescript>`)
);
console.log();
console.log(chalk.blue("Examples:"));
console.log(chalk.cyan(` node-add "user" -m -e -t`) + chalk.gray(" user module # MongoDB + Express + TypeScript"));
console.log(chalk.cyan(` node-add "auth" -s -f -j`) + chalk.gray(" auth module # Sequelize + Fastify + JavaScript"));
console.log();
});
}
async function Language(options) {
if (!options.javascript && !options.typescript) {
const { language } = await inquirer.prompt([
{
name: "language",
type: "list",
message: "Choose a language:",
choices: [
{ name: chalk.yellow("JavaScript"), value: "javascript" },
{ name: chalk.blue("TypeScript"), value: "typescript" }
]
}
]);
options[language] = true;
}
}
async function DB(options) {
if (!options.mongo && !options.seque) {
const { database } = await inquirer.prompt([
{
name: "database",
type: "list",
message: "Choose a database:",
choices: [
{ name: chalk.green("MongoDB"), value: "mongo" },
{ name: chalk.red("Sequelize"), value: "seque" }
]
}
]);
options[database] = true;
}
}
async function FrameWork(options) {
if (!options.express && !options.fastify && !options.elysia) {
const { framework } = await inquirer.prompt([
{
name: "framework",
type: "list",
message: "Choose a framework:",
choices: [
{ name: chalk.cyan("Express"), value: "express" },
{ name: chalk.magenta("Fastify"), value: "fastify" },
{ name: chalk.white("Elysia"), value: "elysia" }
]
}
]);
options[framework] = true;
}
}
async function selectPackageManager(options) {
if (!options.npm && !options.bun && !options.yarn && !options.pnpm) {
const { pm } = await inquirer.prompt([
{
name: "pm",
type: "list",
message: "Choose your package manager:",
choices: [
{ name: "npm", value: "npm" },
{ name: "bun", value: "bun" },
{ name: "yarn", value: "yarn" },
{ name: "pnpm", value: "pnpm" }
]
}
]);
options[pm] = true;
}
}
async function askDefaultStructure(options) {
// Only ask if the yes flag wasn't provided
if (!options.yes) {
const { useDefault } = await inquirer.prompt([
{
name: "useDefault",
type: "confirm",
message: "Do you want to use the default structure (skip interactive project setup)?",
default: false
}
]);
// If the user confirms, mark options.yes true
if (useDefault) {
options.yes = true;
}
}
}
// Setting up the CLI tool with commander
program
.version('1.0.0')
.description('A CLI tool to create a folder structure for Node.js projects')
.option('-m, --mongo', 'SetUp Initializing For MongoDB')
.option('-s, --seque', 'SetUp Initializing For Sequelize')
.option('-y, --yes', 'Create default structure')
.option('-e, --express', 'SetUp Initializing For express js')
.option('-f, --fastify', 'SetUp Initializing For fastify js')
.option('-el, --elysia', 'SetUp Initializing For elysia js')
.option('-j, --javascript', 'SetUp Initializing For javascript')
.option('-t, --typescript', 'SetUp Initializing For typescript')
.option('-n, --npm', "Use npm as the package manager")
.option('-b, --bun', "Use bun as the package manager")
.option('-ya,--yarn', "Use yarn as the package manager")
.option('-pn, --pnpm', "Use pnpm as the package manager")
.option('-h', 'Show All Details')
.action((options) => {
if (options.h) {
return;
}
setTimeout(async () => {
console.log(chalk.cyan("\n🔧 Initializing project setup...\n"));
// Ask for missing selections
await Language(options);
await DB(options);
await FrameWork(options);
await selectPackageManager(options);
if (options.express && options.fastify && options.elysia) {
console.error('Please choose only one option: either --express or --fastify or --elysia, not all.');
process.exit(1);
}
if (options.javascript && options.typescript) {
console.error('Please choose only one option: either --javascript or --typescript, not both.');
process.exit(1);
}
const selectedOptions = [options.npm, options.yarn, options.pnpm, options.bun].filter(Boolean);
if (selectedOptions.length > 1) {
console.error('Please choose only one option: either --npm or --bun or --yarn or --pnpm, not more than one.');
process.exit(1);
}
// Ask about default structure if -y wasn't provided already
await askDefaultStructure(options);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Existing conditions for mongo/seque options and default structure (--yes)
if (options.yes) {
askQuestion(9, options, rl); // Start asking initial questions with default structure
} else {
askQuestion(0, options, rl); // Start asking initial questions
}
}, 200);
});
// Parse command line arguments
program.parse(process.argv);