-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·161 lines (135 loc) · 4.94 KB
/
cli.js
File metadata and controls
executable file
·161 lines (135 loc) · 4.94 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
#!/usr/bin/env node
import inquirer from 'inquirer';
import path from 'path';
import { fileURLToPath } from 'url';
import {
createFromTemplate, removeFile,
loadToolConfig, saveToolConfig,
runCommand, recopyTemplates,
move, cacheConfigFile, restoreConfigFile,
writeFile, copyFile, copyDir,
exists, addBetterAuth_ClientAndSever
} from './utils.js';
var currentDir = ".";
const main = async () => {
const config = await loadToolConfig();
const { action } = await inquirer.prompt([{
type: 'list',
name: 'action',
message: 'What do you want to do?',
choices: [
'Init Nextjs',
'Run Nextjs',
'Add Page',
'Add Component',
'Add Better-Auth',
'Add Server Action',
'Edit Config',
'Add My TSConfig',
'Add My Hooks',
'Add My Components',
'Make Templates Local',
'Exit'
]
}]);
if (action == 'Init Nextjs') {
const { path } = await inquirer.prompt([
{
type: "input",
name: "path",
message: "Where?",
default: "."
}
]);
let content = "";
if(path == "." || path == "./.") {
if(exists("./nextcli.config.json")) {
content = cacheConfigFile();
console.log(content);
removeFile("./nextcli.config.json")
}
}
const { packageManager } = await loadToolConfig();
if(process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
try {
await runCommand(packageManager, ['create', 'next-app@latest', path]);
} catch (error) {
console.error('An error occurred:', error);
}
console.log("Current Dir:\t", currentDir);
let toWrite = (content != "") ? content : await loadToolConfig();
await writeFile(currentDir + "/nextcli.config.json", toWrite);
if(path != ".") {
move("nextcli.config.json", path);
// Change to the newly created directory
process.chdir(path);
console.log(`✅ Changed directory to: ${path}`);
}
} else if (action == 'Run Nextjs') {
const { packageManager } = await loadToolConfig();
await runCommand(packageManager, ['run', 'dev']);
} else if (action == 'Add Component') {
const { name, type } = await inquirer.prompt([
{ type: 'input', name: 'name', message: 'Component name:' },
{
type: 'list',
name: 'type',
message: 'Component type:',
choices: ['client', 'server']
}
]);
const dir = config.componentPath.replace('{{type}}', type);
const outPath = `${dir}/${name}.tsx`;
if(type == "server") {
await createFromTemplate('server_component', outPath, { name });
} else {
await createFromTemplate('client_component', outPath, { name });
}
} else if (action == 'Add Page') {
const { route } = await inquirer.prompt([
{ type: 'input', name: 'route', message: 'Route path (e.g. /about):' }
]);
const routeName = route.replace(/^\//, '') || 'index';
const dir = config.pagePath.replace('{{route}}', routeName);
const outPath = `${dir}/page.tsx`;
await createFromTemplate('page', outPath, { route });
} else if (action == "Add Better-Auth") {
await addBetterAuth_ClientAndSever()
} else if (action == 'Add Server Action') {
const { name } = await inquirer.prompt([
{ type: 'input', name: 'name', message: 'Action name:' }
]);
const outPath = `${config.actionPath}/${name}.ts`;
await createFromTemplate('action', outPath, { name });
} else if (action == 'Make Templates Local') {
recopyTemplates();
} else if (action == 'Edit Config') {
const answers = await inquirer.prompt([
{ type: 'input', name: 'componentPath', message: 'Component path:', default: config.componentPath },
{ type: 'input', name: 'pagePath', message: 'Page path:', default: config.pagePath },
{ type: 'input', name: 'actionPath', message: 'Server Action path:', default: config.actionPath },
{ type: 'list', name: 'style', message: 'Style system:', choices: ['css-module', 'tailwind', 'none'], default: config.style },
{ type: 'list', name: 'packageManager', message: 'Package manager:', choices: ['npm', 'yarn', 'pnpm', 'bun', 'deno'], default: config.packageManager }
]);
await saveToolConfig(answers);
/*
'Add My TSConfig',
'Add My Hooks',
'Add My Components',
*/
} else if (action == 'Add My TSConfig') {
createFromTemplate('tsconfig', currentDir+"/tsconfig.json");
} else if (action == 'Add My Hooks') {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
copyDir(path.join(__dirname, 'templates/hooks'), currentDir+"/src/hooks");
} else if (action == 'Add My Components') {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
copyDir(path.join(__dirname, 'templates/components'), currentDir+"/src/components");
}
process.exit(0);
};
main();