|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import inquirer from 'inquirer'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
3 | 5 | import { |
4 | | - createFromTemplate, removeFile, |
5 | | - loadToolConfig, saveToolConfig, |
6 | | - runCommand, recopyTemplates, |
7 | | - move, cacheConfigFile, restoreConfigFile, |
8 | | - writeFile, copyFile, |
9 | | - exists |
| 6 | + createFromTemplate, removeFile, |
| 7 | + loadToolConfig, saveToolConfig, |
| 8 | + runCommand, recopyTemplates, |
| 9 | + move, cacheConfigFile, restoreConfigFile, |
| 10 | + writeFile, copyFile, copyDir, |
| 11 | + exists, addBetterAuth_ClientAndSever |
10 | 12 | } from './utils.js'; |
11 | 13 |
|
12 | 14 | var currentDir = "."; |
13 | 15 |
|
14 | 16 | const main = async () => { |
15 | | - const config = await loadToolConfig(); |
| 17 | + const config = await loadToolConfig(); |
| 18 | + |
| 19 | + const { action } = await inquirer.prompt([{ |
| 20 | + type: 'list', |
| 21 | + name: 'action', |
| 22 | + message: 'What do you want to do?', |
| 23 | + choices: [ |
| 24 | + 'Init Nextjs', |
| 25 | + 'Run Nextjs', |
| 26 | + 'Add Page', |
| 27 | + 'Add Component', |
| 28 | + 'Add Better-Auth', |
| 29 | + 'Add Server Action', |
| 30 | + 'Edit Config', |
| 31 | + 'Add My TSConfig', |
| 32 | + 'Add My Hooks', |
| 33 | + 'Add My Components', |
| 34 | + 'Make Templates Local', |
| 35 | + 'Exit' |
| 36 | + ] |
| 37 | + }]); |
| 38 | + |
| 39 | + if (action == 'Init Nextjs') { |
| 40 | + const { path } = await inquirer.prompt([ |
| 41 | + { |
| 42 | + type: "input", |
| 43 | + name: "path", |
| 44 | + message: "Where?", |
| 45 | + default: "." |
| 46 | + } |
| 47 | + ]); |
16 | 48 |
|
17 | | - const { action } = await inquirer.prompt([{ |
| 49 | + |
| 50 | + |
| 51 | + let content = ""; |
| 52 | + if(path == "." || path == "./.") { |
| 53 | + if(exists("./nextcli.config.json")) { |
| 54 | + content = cacheConfigFile(); |
| 55 | + console.log(content); |
| 56 | + removeFile("./nextcli.config.json") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const { packageManager } = await loadToolConfig(); |
| 61 | + |
| 62 | + if(process.stdin.isTTY) { |
| 63 | + process.stdin.setRawMode(false); |
| 64 | + } |
| 65 | + try { |
| 66 | + await runCommand(packageManager, ['create', 'next-app@latest', path]); |
| 67 | + } catch (error) { |
| 68 | + console.error('An error occurred:', error); |
| 69 | + } |
| 70 | + |
| 71 | + console.log("Current Dir:\t", currentDir); |
| 72 | + let toWrite = (content != "") ? content : await loadToolConfig(); |
| 73 | + await writeFile(currentDir + "/nextcli.config.json", toWrite); |
| 74 | + |
| 75 | + if(path != ".") { |
| 76 | + move("nextcli.config.json", path); |
| 77 | + // Change to the newly created directory |
| 78 | + process.chdir(path); |
| 79 | + console.log(`✅ Changed directory to: ${path}`); |
| 80 | + } |
| 81 | + |
| 82 | + } else if (action == 'Run Nextjs') { |
| 83 | + const { packageManager } = await loadToolConfig(); |
| 84 | + await runCommand(packageManager, ['run', 'dev']); |
| 85 | + |
| 86 | + } else if (action == 'Add Component') { |
| 87 | + const { name, type } = await inquirer.prompt([ |
| 88 | + { type: 'input', name: 'name', message: 'Component name:' }, |
| 89 | + { |
18 | 90 | type: 'list', |
19 | | - name: 'action', |
20 | | - message: 'What do you want to do?', |
21 | | - choices: [ |
22 | | - 'Init Nextjs', |
23 | | - 'Run Nextjs', |
24 | | - 'Add Page', |
25 | | - 'Add Component', |
26 | | - 'Add Better-Auth', |
27 | | - 'Add Server Action', |
28 | | - 'Edit Config', |
29 | | - 'Make Templates Local', |
30 | | - 'Exit' |
31 | | - ] |
32 | | - }]); |
33 | | - |
34 | | - if (action === 'Init Nextjs') { |
35 | | - const { path } = await inquirer.prompt([ |
36 | | - { |
37 | | - type: "input", |
38 | | - name: "path", |
39 | | - message: "Where?", |
40 | | - default: "." |
41 | | - } |
42 | | - ]); |
43 | | - |
44 | | - |
45 | | - |
46 | | - let content = ""; |
47 | | - if(path == "." || path == "./.") { |
48 | | - if(exists("./nextcli.config.json")) { |
49 | | - content = cacheConfigFile(); |
50 | | - console.log(content); |
51 | | - removeFile("./nextcli.config.json") |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - const { packageManager } = await loadToolConfig(); |
56 | | - |
57 | | - if(process.stdin.isTTY) { |
58 | | - process.stdin.setRawMode(false); |
59 | | - } |
60 | | - try { |
61 | | - await runCommand(packageManager, ['create', 'next-app@latest', path]); |
62 | | - } catch (error) { |
63 | | - console.error('An error occurred:', error); |
64 | | - } |
65 | | - |
66 | | - console.log("Current Dir:\t", currentDir); |
67 | | - let toWrite = (content != "") ? content : await loadToolConfig(); |
68 | | - await writeFile(currentDir + "/nextcli.config.json", toWrite); |
69 | | - |
70 | | - if(path != ".") { |
71 | | - move("nextcli.config.json", path); |
72 | | - // Change to the newly created directory |
73 | | - process.chdir(path); |
74 | | - console.log(`✅ Changed directory to: ${path}`); |
75 | | - } |
76 | | - |
77 | | - } else if (action === 'Run Nextjs') { |
78 | | - const { packageManager } = await loadToolConfig(); |
79 | | - await runCommand(packageManager, ['run', 'dev']); |
80 | | - |
81 | | - } else if (action === 'Add Component') { |
82 | | - const { name, type } = await inquirer.prompt([ |
83 | | - { type: 'input', name: 'name', message: 'Component name:' }, |
84 | | - { |
85 | | - type: 'list', |
86 | | - name: 'type', |
87 | | - message: 'Component type:', |
88 | | - choices: ['client', 'server'] |
89 | | - } |
90 | | - ]); |
91 | | - |
92 | | - const dir = config.componentPath.replace('{{type}}', type); |
93 | | - const outPath = `${dir}/${name}.tsx`; |
94 | | - |
95 | | - if(type == "server") { |
96 | | - await createFromTemplate('server_component', outPath, { name }); |
97 | | - } else { |
98 | | - await createFromTemplate('client_component', outPath, { name }); |
99 | | - } |
100 | | - |
101 | | - } else if (action === 'Add Page') { |
102 | | - const { route } = await inquirer.prompt([ |
103 | | - { type: 'input', name: 'route', message: 'Route path (e.g. /about):' } |
104 | | - ]); |
105 | | - const routeName = route.replace(/^\//, '') || 'index'; |
106 | | - const dir = config.pagePath.replace('{{route}}', routeName); |
107 | | - const outPath = `${dir}/page.tsx`; |
108 | | - await createFromTemplate('page', outPath, { route }); |
109 | | - |
110 | | - } else if (action === "Add Better-Auth") { |
111 | | - addBetterAuth_ClientAndSever() |
112 | | - |
113 | | - } else if (action === 'Add Server Action') { |
114 | | - const { name } = await inquirer.prompt([ |
115 | | - { type: 'input', name: 'name', message: 'Action name:' } |
116 | | - ]); |
117 | | - const outPath = `${config.actionPath}/${name}.ts`; |
118 | | - await createFromTemplate('action', outPath, { name }); |
119 | | - |
120 | | - } else if (action === 'Make Templates Local') { |
121 | | - recopyTemplates(); |
122 | | - |
123 | | - } else if (action === 'Edit Config') { |
124 | | - const answers = await inquirer.prompt([ |
125 | | - { type: 'input', name: 'componentPath', message: 'Component path:', default: config.componentPath }, |
126 | | - { type: 'input', name: 'pagePath', message: 'Page path:', default: config.pagePath }, |
127 | | - { type: 'input', name: 'actionPath', message: 'Server Action path:', default: config.actionPath }, |
128 | | - { type: 'list', name: 'style', message: 'Style system:', choices: ['css-module', 'tailwind', 'none'], default: config.style }, |
129 | | - { type: 'list', name: 'packageManager', message: 'Package manager:', choices: ['npm', 'yarn', 'pnpm', 'bun', 'deno'], default: config.packageManager } |
130 | | - ]); |
131 | | - await saveToolConfig(answers); |
| 91 | + name: 'type', |
| 92 | + message: 'Component type:', |
| 93 | + choices: ['client', 'server'] |
| 94 | + } |
| 95 | + ]); |
| 96 | + |
| 97 | + const dir = config.componentPath.replace('{{type}}', type); |
| 98 | + const outPath = `${dir}/${name}.tsx`; |
| 99 | + |
| 100 | + if(type == "server") { |
| 101 | + await createFromTemplate('server_component', outPath, { name }); |
| 102 | + } else { |
| 103 | + await createFromTemplate('client_component', outPath, { name }); |
132 | 104 | } |
133 | 105 |
|
134 | | - process.exit(0); |
| 106 | + } else if (action == 'Add Page') { |
| 107 | + const { route } = await inquirer.prompt([ |
| 108 | + { type: 'input', name: 'route', message: 'Route path (e.g. /about):' } |
| 109 | + ]); |
| 110 | + const routeName = route.replace(/^\//, '') || 'index'; |
| 111 | + const dir = config.pagePath.replace('{{route}}', routeName); |
| 112 | + const outPath = `${dir}/page.tsx`; |
| 113 | + await createFromTemplate('page', outPath, { route }); |
| 114 | + |
| 115 | + } else if (action == "Add Better-Auth") { |
| 116 | + await addBetterAuth_ClientAndSever() |
| 117 | + |
| 118 | + } else if (action == 'Add Server Action') { |
| 119 | + const { name } = await inquirer.prompt([ |
| 120 | + { type: 'input', name: 'name', message: 'Action name:' } |
| 121 | + ]); |
| 122 | + const outPath = `${config.actionPath}/${name}.ts`; |
| 123 | + await createFromTemplate('action', outPath, { name }); |
| 124 | + |
| 125 | + } else if (action == 'Make Templates Local') { |
| 126 | + recopyTemplates(); |
| 127 | + |
| 128 | + } else if (action == 'Edit Config') { |
| 129 | + const answers = await inquirer.prompt([ |
| 130 | + { type: 'input', name: 'componentPath', message: 'Component path:', default: config.componentPath }, |
| 131 | + { type: 'input', name: 'pagePath', message: 'Page path:', default: config.pagePath }, |
| 132 | + { type: 'input', name: 'actionPath', message: 'Server Action path:', default: config.actionPath }, |
| 133 | + { type: 'list', name: 'style', message: 'Style system:', choices: ['css-module', 'tailwind', 'none'], default: config.style }, |
| 134 | + { type: 'list', name: 'packageManager', message: 'Package manager:', choices: ['npm', 'yarn', 'pnpm', 'bun', 'deno'], default: config.packageManager } |
| 135 | + ]); |
| 136 | + await saveToolConfig(answers); |
| 137 | + |
| 138 | + /* |
| 139 | + 'Add My TSConfig', |
| 140 | + 'Add My Hooks', |
| 141 | + 'Add My Components', |
| 142 | + */ |
| 143 | + } else if (action == 'Add My TSConfig') { |
| 144 | + createFromTemplate('tsconfig', currentDir+"/tsconfig.json"); |
| 145 | + |
| 146 | + } else if (action == 'Add My Hooks') { |
| 147 | + const __filename = fileURLToPath(import.meta.url); |
| 148 | + const __dirname = path.dirname(__filename); |
| 149 | + copyDir(path.join(__dirname, 'templates/hooks'), currentDir+"/src/hooks"); |
| 150 | + |
| 151 | + } else if (action == 'Add My Components') { |
| 152 | + const __filename = fileURLToPath(import.meta.url); |
| 153 | + const __dirname = path.dirname(__filename); |
| 154 | + copyDir(path.join(__dirname, 'templates/components'), currentDir+"/src/components"); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + process.exit(0); |
135 | 159 | }; |
136 | 160 |
|
137 | 161 | main(); |
0 commit comments