-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (90 loc) · 3.46 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
import inquirer from 'inquirer';
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
// Use fileURLToPath for `__dirname` equivalent in ES Modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export async function run(projectName) {
const isCurrentDir = projectName === '.';
const targetDir = isCurrentDir
? process.cwd()
: path.join(process.cwd(), projectName);
if (!isCurrentDir && fs.existsSync(targetDir)) {
console.log(chalk.red(`Folder "${projectName}" already exists.`));
return;
}
// Step 1: Choose Language
const { language } = await inquirer.prompt([
{
type: 'list',
name: 'language',
message: 'Choose the project language:',
choices: ['JavaScript', 'TypeScript'],
},
]);
// Step 2: Choose Database (Optional)
const { database } = await inquirer.prompt([
{
type: 'list',
name: 'database',
message: 'Choose a database for your project:',
choices: [
{ name: 'NoSQL (MongoDB with Mongoose)', value: 'mongoose' },
{
name: 'SQL (PostgreSQL or MySQL with Sequelize/TypeORM)',
value: 'sql',
},
{ name: 'No database', value: 'none' },
],
},
]);
// Determine the correct template folder
let templateFolder = language.toLowerCase(); // "javascript" or "typescript"
if (database === 'mongoose') {
templateFolder += '-mongoose';
} else if (database === 'sql') {
templateFolder += language === 'JavaScript' ? '-sequelize' : '-typeorm';
}
// Define the source template directory based on user choice
const templateDir = path.join(__dirname, 'templates', templateFolder);
if (!fs.existsSync(templateDir)) {
console.log(
chalk.red(
`Template for ${language} with ${database} does not exist.`,
),
);
return;
}
try {
// Copy all files and folders from the selected template directory
fs.copySync(templateDir, targetDir);
console.log(chalk.green(`Project created at ${targetDir}`));
// Step 3: Initialize Git (Optional)
const { initGit } = await inquirer.prompt([
{
type: 'confirm',
name: 'initGit',
message: 'Initialize a git repository?',
default: false,
},
]);
if (initGit) {
execSync('git init', { cwd: targetDir, stdio: 'inherit' });
console.log(chalk.green('Git repository initialized.'));
}
console.log(chalk.green.bold(`\nProject Setup Complete! 🎉\n`));
console.log(chalk.yellowBright(`Next Steps:`));
console.log(chalk.blue(`Navigate to your project folder:`));
console.log(chalk.cyan(` cd ${isCurrentDir ? '.' : projectName}`));
console.log(chalk.blue(`Install dependencies:`));
console.log(chalk.cyan(` npm install`));
console.log(chalk.blue(`Start the development server:`));
console.log(chalk.cyan(` npm run dev\n`));
console.log(chalk.magenta.bold(`Happy Coding! 🚀`));
} catch (error) {
console.error(chalk.red('Error copying template files:'), error);
}
}