-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·432 lines (387 loc) · 15.5 KB
/
index.js
File metadata and controls
executable file
·432 lines (387 loc) · 15.5 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env node
import { Command } from 'commander';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { generateProject } from './src/generate.js';
import { addAction } from './src/commands/add.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const pkg = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf-8'));
const TEMPLATES = {
portfolio: {
label: 'Portfolio Website',
variants: ['Minimal Dark', 'Creative Agency', 'Developer Folio', 'Photography', 'Resume Style'],
sidebarOpts: false,
},
ecommerce: {
label: 'E-Commerce Store',
variants: ['Fashion Store', 'Electronics Shop', 'Food & Grocery', 'Digital Products', 'Multi-Vendor'],
sidebarOpts: true,
},
school: {
label: 'School Management',
variants: ['University Portal', 'K-12 System', 'Online Academy', 'Training Platform', 'LMS Dashboard'],
sidebarOpts: true,
},
saas: {
label: 'SaaS Dashboard',
variants: ['Analytics Tool', 'CRM System', 'Project Manager', 'Finance Tracker', 'HR Platform'],
sidebarOpts: true,
},
blog: {
label: 'Blog / Magazine',
variants: ['Tech Blog', 'Lifestyle Mag', 'News Portal', 'Personal Journal', 'Tutorial Site'],
sidebarOpts: true,
},
};
const VALID_TEMPLATES = Object.keys(TEMPLATES);
const VALID_ARCHS = ['nextjs-monolith', 'vite-react', 'nextjs-turborepo'];
const ARCH_ALIASES = { nextjs: 'nextjs-monolith', vite: 'vite-react', turborepo: 'nextjs-turborepo' };
const DESIGNS = [
'Minimal Clean', 'Dark Terminal', 'Glassmorphism', 'Brutalist',
'Soft Pastel', 'Corporate Blue', 'Neon Cyberpunk', 'Earth Tones',
];
function resolveArch(value) {
if (!value) return undefined;
const lower = value.toLowerCase();
if (ARCH_ALIASES[lower]) return ARCH_ALIASES[lower];
if (VALID_ARCHS.includes(lower)) return lower;
return null;
}
function resolveDesign(value) {
if (!value) return undefined;
const lower = value.toLowerCase();
const match = DESIGNS.find((d) => d.toLowerCase() === lower);
return match || null;
}
function printBanner() {
console.clear();
console.log(chalk.green('❯ npx opusify create'));
console.log(chalk.green(' ██████╗ ██████╗ ██╗ ██╗███████╗██╗███████╗██╗ ██╗'));
console.log(chalk.blue(` Welcome to ${chalk.magenta('Opusify')} — The Full-Stack Scaffold Engine v${pkg.version}`));
console.log(chalk.gray(' Generate production-ready apps with one command.\n'));
}
async function createAction(projectName, options) {
printBanner();
// 1. Validate project name if provided as an argument
if (projectName && !/^[a-z0-9-]+$/.test(projectName)) {
console.log(chalk.red(`\n✖ Invalid project name: "${projectName}"`));
console.log(chalk.gray(' Suggested fix: Please use only lowercase letters, numbers, and hyphens (e.g., my-awesome-app).'));
process.exit(1);
}
// Validate flags early
if (options.template && !VALID_TEMPLATES.includes(options.template)) {
console.log(chalk.red(`\n✖ Invalid template: "${options.template}"`));
console.log(chalk.gray(` Valid options: ${VALID_TEMPLATES.join(', ')}`));
process.exit(1);
}
if (options.arch) {
const resolved = resolveArch(options.arch);
if (resolved === null) {
console.log(chalk.red(`\n✖ Invalid architecture: "${options.arch}"`));
console.log(chalk.gray(` Valid options: nextjs, vite, turborepo (or nextjs-monolith, vite-react, nextjs-turborepo)`));
process.exit(1);
}
options.arch = resolved;
}
if (options.design) {
const resolved = resolveDesign(options.design);
if (resolved === null) {
console.log(chalk.red(`\n✖ Invalid design: "${options.design}"`));
console.log(chalk.gray(` Valid options: ${DESIGNS.join(', ')}`));
process.exit(1);
}
options.design = resolved;
}
if (options.nav !== undefined) {
const nav = parseInt(options.nav, 10);
if (isNaN(nav) || nav < 3 || nav > 9) {
console.log(chalk.red('\n✖ Invalid nav count. Must be a number between 3 and 9.'));
process.exit(1);
}
options.nav = nav;
}
// Build the list of prompts, skipping any that were provided via flags
const prompts = [];
const defaults = {
projectName: projectName || 'my-opusify-app',
template: 'portfolio',
variant: null, // resolved after template is known
architecture: 'nextjs-monolith',
design: 'Minimal Clean',
navCount: 5,
includeSidebar: false,
initGit: true,
enableSecurity: true,
};
// If --yes, use all defaults + any provided flags
if (options.yes) {
const template = options.template || defaults.template;
const config = {
projectName: projectName || defaults.projectName,
template,
variant: options.variant || TEMPLATES[template].variants[0],
architecture: options.arch || defaults.architecture,
design: options.design || defaults.design,
navCount: options.nav || defaults.navCount,
includeSidebar: options.sidebar || defaults.includeSidebar,
initGit: options.git !== false,
enableSecurity: defaults.enableSecurity,
noInstall: options.install === false,
verbose: options.verbose || false,
repo: options.repo || 'Ebyte-Lab/opusify-templates',
token: options.token || process.env.OPUSIFY_GITHUB_TOKEN || process.env.GITHUB_TOKEN,
};
console.log(chalk.green('✔ Using defaults (--yes mode)'));
console.log(chalk.gray(` Project: ${config.projectName}`));
console.log(chalk.gray(` Template: ${config.template} / ${config.variant}`));
console.log(chalk.gray(` Architecture: ${config.architecture}`));
console.log(chalk.gray(` Design: ${config.design}`));
console.log('');
await generateProject(config);
return;
}
// Interactive prompts — skip those already provided via flags
if (!projectName) {
prompts.push({
type: 'input',
name: 'projectName',
message: chalk.magenta.bold('What is your project name?'),
default: defaults.projectName,
validate: (input) => {
if (!/^[a-z0-9-]+$/.test(input)) {
return 'Please use only lowercase letters, numbers, and hyphens (e.g., my-awesome-app)';
}
return true;
},
});
}
if (!options.template) {
prompts.push({
type: 'rawlist',
name: 'template',
message: chalk.magenta.bold('Select a project template:'),
choices: [
{ name: 'Portfolio Website', value: 'portfolio' },
{ name: 'E-Commerce Store', value: 'ecommerce' },
{ name: 'School Management', value: 'school' },
{ name: 'SaaS Dashboard', value: 'saas' },
{ name: 'Blog / Magazine', value: 'blog' },
],
});
}
// Variant — skip if provided via flag
if (!options.variant) {
prompts.push({
type: 'rawlist',
name: 'variant',
message: chalk.magenta.bold('Choose a variant style:'),
choices: (answers) => {
const tmpl = options.template || answers.template;
if (!TEMPLATES[tmpl]) return ['Default'];
return TEMPLATES[tmpl].variants;
},
});
}
if (!options.arch) {
prompts.push({
type: 'rawlist',
name: 'architecture',
message: chalk.magenta.bold('Choose architecture:'),
choices: [
{ name: 'Next.js 14 — App Router (Recommended)', value: 'nextjs-monolith' },
{ name: 'Vite + React 18 — SPA', value: 'vite-react' },
{ name: 'Turborepo — Monorepo (Enterprise)', value: 'nextjs-turborepo' },
],
});
}
if (!options.design) {
prompts.push({
type: 'rawlist',
name: 'design',
message: chalk.magenta.bold('Choose design system:'),
choices: DESIGNS,
});
}
if (options.nav === undefined) {
prompts.push({
type: 'number',
name: 'navCount',
message: chalk.cyan.bold('How many navigation links? (3-9)'),
default: defaults.navCount,
validate: (input) => (input >= 3 && input <= 9 ? true : 'Please enter a number between 3 and 9'),
});
}
// Sidebar prompt — only if template supports it and flag not provided
if (options.sidebar === undefined) {
prompts.push({
type: 'confirm',
name: 'includeSidebar',
message: chalk.cyan.bold('Include a sidebar layout?'),
default: false,
when: (answers) => {
const tmpl = options.template || answers.template;
if (!TEMPLATES[tmpl]) return false;
return TEMPLATES[tmpl].sidebarOpts;
},
});
}
if (options.git !== false) {
prompts.push({
type: 'confirm',
name: 'initGit',
message: chalk.cyan.bold('Initialize a new Git repository?'),
default: true,
});
}
// Only ask security prompt in interactive mode (when not all flags provided)
const allFlagsProvided = options.template && options.variant && options.arch && options.design && options.nav !== undefined;
if (!allFlagsProvided) {
prompts.push({
type: 'confirm',
name: 'enableSecurity',
message: chalk.red.bold('Enable Enterprise Security Hardening (Zod env validation & CSP headers)?'),
default: true,
});
}
let answers;
try {
answers = await inquirer.prompt(prompts);
} catch (error) {
if (error.name === 'ExitPromptError' || error.message?.includes('User force closed')) {
console.log(chalk.yellow('\nScaffold cancelled. Goodbye!'));
process.exit(0);
}
throw error;
}
// Merge flags with interactive answers
const config = {
projectName: projectName || answers.projectName,
template: options.template || answers.template,
variant: options.variant || answers.variant,
architecture: options.arch || answers.architecture,
design: options.design || answers.design,
navCount: options.nav || answers.navCount,
includeSidebar: options.sidebar !== undefined ? options.sidebar : (answers.includeSidebar || false),
initGit: options.git === false ? false : (answers.initGit !== undefined ? answers.initGit : true),
enableSecurity: answers.enableSecurity !== undefined ? answers.enableSecurity : true,
noInstall: options.install === false,
verbose: options.verbose || false,
repo: options.repo || 'Ebyte-Lab/opusify-templates',
token: options.token || process.env.OPUSIFY_GITHUB_TOKEN || process.env.GITHUB_TOKEN,
};
// 2. Warn if selecting Vite SPA for E-Commerce (SEO concern)
if (config.template === 'ecommerce' && config.architecture.includes('vite')) {
console.log(chalk.yellow('\n⚠️ WARNING: You selected Vite SPA for an E-Commerce template.'));
console.log(chalk.gray(' Vite generates a purely Client-Side application (No SSR).'));
console.log(chalk.gray(' This severely impacts SEO, which is crucial for E-Commerce stores.'));
console.log(chalk.gray(' Suggested fix: We highly recommend using Next.js App Router instead.'));
// If not in --yes mode, ask for confirmation
if (!options.yes) {
const { proceed } = await inquirer.prompt([{
type: 'confirm',
name: 'proceed',
message: chalk.yellow('Do you still want to proceed with Vite?'),
default: false
}]);
if (!proceed) {
console.log(chalk.yellow('\nScaffold cancelled. Please run the command again.'));
process.exit(0);
}
}
}
console.log('\n' + chalk.green('✔ Configuration collected successfully!'));
await generateProject(config);
}
// Architecture availability map
const ARCHITECTURES = {
'nextjs-monolith': { label: 'Next.js 14 — App Router', available: ['portfolio', 'ecommerce', 'school', 'saas', 'blog'] },
'vite-react': { label: 'Vite + React 18 — SPA', available: ['portfolio', 'ecommerce', 'saas'] },
'nextjs-turborepo': { label: 'Turborepo — Monorepo', available: [] },
};
function listAction(options) {
console.log('');
console.log(chalk.blue.bold(' Opusify — Available Templates'));
console.log(chalk.gray(' ─────────────────────────────────────────────────────────────'));
console.log('');
if (options.template && !TEMPLATES[options.template]) {
console.log(chalk.red(` ✖ Unknown template: "${options.template}"`));
console.log(chalk.gray(` Valid options: ${VALID_TEMPLATES.join(', ')}`));
process.exit(1);
}
const templatesToShow = options.template
? { [options.template]: TEMPLATES[options.template] }
: TEMPLATES;
for (const [key, tmpl] of Object.entries(templatesToShow)) {
console.log(chalk.white.bold(` ${tmpl.label}`) + chalk.gray(` (${key})`));
console.log('');
// Variants
console.log(chalk.cyan(' Variants:'));
for (const variant of tmpl.variants) {
console.log(chalk.white(` • ${variant}`));
}
console.log('');
// Architectures
console.log(chalk.cyan(' Architectures:'));
for (const [archKey, arch] of Object.entries(ARCHITECTURES)) {
const isAvailable = arch.available.includes(key);
if (isAvailable) {
console.log(chalk.green(` ✔ ${arch.label}`) + chalk.gray(` (${archKey})`));
} else {
console.log(chalk.gray(` ○ ${arch.label} — coming soon`));
}
}
console.log('');
// Sidebar support
console.log(chalk.cyan(' Sidebar:') + (tmpl.sidebarOpts ? chalk.green(' supported') : chalk.gray(' not applicable')));
console.log('');
console.log(chalk.gray(' ─────────────────────────────────────────────────────────────'));
console.log('');
}
// Design systems (show once at the bottom)
if (!options.template) {
console.log(chalk.blue.bold(' Design Systems'));
console.log('');
for (const design of DESIGNS) {
console.log(chalk.white(` • ${design}`));
}
console.log('');
}
}
// CLI setup
const program = new Command();
program
.name('opusify')
.description('The Full-Stack Scaffold Engine — Generate production-ready apps with one command.')
.version(pkg.version);
program
.command('create')
.description('Scaffold a new project')
.argument('[project-name]', 'Name of the project to create')
.option('-t, --template <template>', `Project template (${VALID_TEMPLATES.join(', ')})`)
.option('-a, --arch <architecture>', 'Architecture (nextjs, vite, turborepo)')
.option('-d, --design <design>', `Design system (e.g., "Dark Terminal", "Glassmorphism")`)
.option('-v, --variant <variant>', 'Variant style (e.g., "Developer Folio", "Fashion Store")')
.option('-n, --nav <count>', 'Number of navigation links (3-9)')
.option('--sidebar', 'Include a sidebar layout')
.option('--no-git', 'Skip Git initialization')
.option('--no-install', 'Skip npm install')
.option('-r, --repo <repository>', 'Custom GitHub repository (e.g., "username/repo")')
.option('--token <token>', 'GitHub Personal Access Token for private repositories')
.option('-y, --yes', 'Accept all defaults (non-interactive)')
.option('--verbose', 'Show detailed output during generation')
.action(createAction);
program
.command('list')
.description('Show available templates, variants, and architectures')
.option('-t, --template <template>', 'Show details for a specific template')
.action(listAction);
program
.command('add')
.description('Add a feature plugin to an existing Opusify project')
.argument('<plugin>', 'Plugin to add (auth, dark-mode, analytics)')
.action(addAction);
program.parse();