-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
262 lines (227 loc) · 9.21 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
import * as p from '@clack/prompts';
import { setTimeout } from 'node:timers/promises';
// import { fileURLToPath } from 'node:url';
import color from 'picocolors';
import shell from 'shelljs';
import path from 'node:path';
import fs from 'node:fs';
// Configuration constants
const CONFIG = {
SKIP_FILES: ['node_modules', 'build', 'dist'],
FRONTEND_TEMPLATES: ['reactjs-ts', 'reactjs-js', 'nextjs-ts', 'nextjs-js'],
FULLSTACK_TEMPLATES: ['laranext-ts', 'laravel-filament'],
TEMPLATE_OPTIONS: [
{ value: 'reactjs-ts', label: 'React.js (Tailwind + TypeScript)' },
{ value: 'reactjs-js', label: 'React.js (Tailwind + JavaScript)' },
{ value: 'nextjs-ts', label: 'Next.js App Router (Tailwind + TypeScript)' },
{ value: 'nextjs-js', label: 'Next.js App Router (Tailwind + JavaScript)' },
{ value: 'laranext-ts', label: 'Laravel Breeze API w/ Next.js App Router (Tailwind + TypeScript)' },
{ value: 'laravel-filament', label: 'Laravel + Filament' },
],
PACKAGE_MANAGERS: [
{ value: 'npm', label: 'NPM' },
{ value: 'yarn', label: 'Yarn' },
{ value: 'bun', label: 'Bun' },
{ value: 'pnpm', label: 'PNPM' },
]
};
// Utility functions
const getOperatingSystem = () => {
const platform = process.platform;
const osMap = {
darwin: 'mac',
win32: 'windows',
linux: 'linux'
};
return osMap[platform] || 'unknown';
};
const validateProjectPath = (value) => {
if (!value) return 'Please enter a path.';
if (value[0] !== '.') return 'Please enter a relative path.';
return;
};
const validateProjectDirectory = (dirPath) => {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
return true;
}
if (fs.readdirSync(dirPath).length > 0) {
p.log.error('Directory is not empty! Please create a project in an empty directory.');
process.exit(1);
}
return true;
};
// Template handling
// const replicateTemplates = async (templatePath, projectPath) => {
// try {
// const templateFiles = fs.readdirSync(templatePath)
// .filter(name => !CONFIG.SKIP_FILES.includes(name));
// templateFiles.forEach(name => {
// const originPath = path.join(templatePath, name);
// const destinationPath = path.join(projectPath, name);
// const stats = fs.statSync(originPath);
// if (stats.isFile()) {
// fs.writeFileSync(destinationPath, fs.readFileSync(originPath, 'utf8'));
// } else if (stats.isDirectory()) {
// if (!fs.existsSync(destinationPath)) {
// fs.mkdirSync(destinationPath);
// }
// replicateTemplates(originPath, destinationPath);
// }
// });
// return true;
// } catch (error) {
// p.log.error(`Failed to replicate templates: ${error.message}`);
// process.exit(1);
// }
// };
// Project setup functions
const setupFrontendProject = async (projectPath, templateType) => {
try {
shell.cd(projectPath);
const os = getOperatingSystem();
const silentFlag = os === 'windows' ? '> nul 2>&1' : '> /dev/null 2>&1';
const result = shell.exec(`npx degit nuflakbrr/frontend-template#${templateType} . --silent ${silentFlag}`);
if (result.code !== 0) {
p.log.error('Failed to setup frontend project');
process.exit(1);
}
return true;
} catch (error) {
p.log.error(`Failed to setup frontend project: ${error.message}`);
process.exit(1);
}
};
const setupFullstackProject = async (projectPath, templateType) => {
try {
shell.cd(projectPath);
const os = getOperatingSystem();
const silentFlag = os === 'windows' ? '> nul 2>&1' : '> /dev/null 2>&1';
const result = shell.exec(`npx degit nuflakbrr/fullstack-template#${templateType} . --silent ${silentFlag}`);
if (result.code !== 0) {
p.log.error('Failed to setup fullstack project');
process.exit(1);
}
return true;
} catch (error) {
p.log.error(`Failed to setup fullstack project: ${error.message}`);
process.exit(1);
}
};
const installDependencies = async (originalPath, projectName, spinner) => {
try {
const packageManager = await p.select({
message: `Pick a package manager to install dependencies for "${projectName}"`,
initialValue: 'npm',
options: CONFIG.PACKAGE_MANAGERS,
});
if (packageManager) {
spinner.start(`📦 Installing dependencies using ${packageManager}...`);
await setTimeout(2500);
shell.cd(originalPath);
const os = getOperatingSystem();
const silentRedirect = os === 'windows' ? '2>NUL' : '2>/dev/null';
const commands = {
npm: `npm install --silent ${silentRedirect}`,
yarn: `yarn install --silent ${silentRedirect}`,
bun: `bun install --silent --no-summary ${silentRedirect}`,
pnpm: `pnpm install --silent ${silentRedirect}`
};
const installCommand = commands[packageManager];
if (!installCommand) {
p.log.error(`Unsupported package manager: ${packageManager}`);
}
const result = shell.exec(installCommand);
if (result.code !== 0) {
spinner.stop(`Failed to install dependencies using ${packageManager}`);
if (result.stderr) {
console.error(`Error details: ${result.stderr}`);
}
process.exit(1);
}
return packageManager;
}
return null;
} catch (error) {
p.log.error(`Failed to install dependencies: ${error.message}`);
process.exit(1);
}
};
const displayNextSteps = (projectPath, packageManager, install = false) => {
const nextSteps = install
? `cd ${projectPath}\n${packageManager} run dev\n\n${color.underline(color.cyan('Happy Coding!'))}`
: `cd ${projectPath}\n${packageManager} install\n${packageManager} run dev\n\n${color.underline(color.cyan('Happy Coding!'))}`;
const contact = `Have a Problems? Report to ${color.underline(color.cyan('https://github.com/nuflakbrr/bikinproject/issues'))}`;
p.note(nextSteps, 'Next steps.');
p.outro(contact);
};
// Main application flow
async function main() {
try {
console.clear();
await setTimeout(1000);
p.intro(`${color.bgCyan(color.black(' create-bikinproject-app '))}`);
const project = await p.group(
{
path: () => p.text({
message: 'Where should we create your project?',
placeholder: './your-project',
validate: validateProjectPath,
}),
type: ({ results }) => p.select({
message: `Pick a starter project type within "${results.path}"`,
initialValue: 'react-ts-template',
options: CONFIG.TEMPLATE_OPTIONS,
}),
},
{
onCancel: () => {
p.cancel('Operation cancelled.');
process.exit(1);
},
}
);
if (!project) return;
const spinner = p.spinner();
const projectPath = path.join(process.cwd(), project.path);
// const templatePath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'templates', project.type);
try {
spinner.start('⏳ Creating project...');
await setTimeout(2500);
validateProjectDirectory(project.path);
if (CONFIG.FRONTEND_TEMPLATES.includes(project.type)) {
await setupFrontendProject(project.path, project.type);
} else if (CONFIG.FULLSTACK_TEMPLATES.includes(project.type)) {
await setupFullstackProject(project.path, project.type);
}
spinner.stop('✅ Project created successfully!');
if (fs.existsSync(path.join(projectPath, 'package.json'))) {
const install = await p.confirm({
message: `Do you want to install dependencies for ${project.path}?`,
initialValue: true,
});
if (install) {
const packageManager = await installDependencies(projectPath, project.path, spinner);
if (packageManager) {
spinner.stop('✅ Dependencies installed successfully!');
p.log.step('🎉 Project ready to use!');
displayNextSteps(project.path, packageManager, true);
return;
}
}
}
displayNextSteps(project.path, 'npm');
} catch (error) {
spinner.stop(`❌ Error: ${error.message}`);
process.exit(1);
}
} catch (error) {
p.log.error(`Fatal error: ${error.message}`);
process.exit(1);
}
}
main().catch((error) => {
p.log.error(`Unhandled error: ${error.message}`);
process.exit(1);
});