-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
70 lines (58 loc) · 2.02 KB
/
cli.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
#!/usr/bin/env node
import fs from 'fs/promises';
import path from 'path';
import { input, confirm } from '@inquirer/prompts';
import chalk from 'chalk';
async function createComponent(baseDir, componentName, isTS) {
const componentDir = path.join(baseDir, componentName);
const extension = isTS ? "tsx" : "jsx";
const filePath = path.join(componentDir, `page.${extension}`);
const capitalizedName = componentName.charAt(0).toUpperCase() + componentName.slice(1);
const content = `
export default function ${capitalizedName}() {
return (
<div>
<h1>${capitalizedName}</h1>
</div>
);
}
`.trim();
try {
await fs.mkdir(componentDir, { recursive: true });
await fs.writeFile(filePath, content);
console.log(chalk.green(`Component ${componentName} created successfully.`));
} catch (error) {
console.error(chalk.red(`Error creating component: ${error.message}`));
throw error;
}
}
async function main() {
const args = process.argv.slice(2);
if (args.length === 0 || !['create', 'c'].includes(args[0]) || !['page', 'p'].includes(args[1])) {
console.log(chalk.yellow("Usage: nxt create page OR nxt c p"));
process.exit(1);
}
try {
let isUsingSrc, isUsingTS, componentName;
const useDefaults = args[2] === '-y';
if (useDefaults) {
isUsingSrc = true;
isUsingTS = true;
componentName = args[3];
} else {
isUsingSrc = await confirm({ message: 'Are you using src folder?', default: true });
isUsingTS = await confirm({ message: 'Are you using TypeScript?', default: true });
componentName = args[3] || await input({ message: 'Enter the component name:' });
}
if (!componentName?.trim()) {
console.log(chalk.red("Component name cannot be empty. Exiting."));
process.exit(1);
}
const baseDir = isUsingSrc ? 'src/app' : 'app';
await createComponent(baseDir, componentName, isUsingTS);
} catch (error) {
console.error(chalk.red("An error occurred:"), error);
process.exit(1);
}
}
main().catch(console.error);