|
| 1 | +#!/usr/bin/env node |
| 2 | +import inquirer from 'inquirer'; |
| 3 | +import { createFromTemplate, removeFile, loadToolConfig, saveToolConfig, runCommand, recopyTemplates } from './utils.js'; |
| 4 | + |
| 5 | +const main = async () => { |
| 6 | + const config = await loadToolConfig(); |
| 7 | + |
| 8 | + const { action } = await inquirer.prompt([{ |
| 9 | + type: 'list', |
| 10 | + name: 'action', |
| 11 | + message: 'What do you want to do?', |
| 12 | + choices: [ |
| 13 | + 'Init Nextjs', |
| 14 | + 'Run Nextjs', |
| 15 | + 'Add Page', |
| 16 | + 'Add Component', |
| 17 | + 'Add Server Action', |
| 18 | + 'Edit Config', |
| 19 | + 'Make Templates Local', |
| 20 | + 'Exit' |
| 21 | + ] |
| 22 | + }]); |
| 23 | + |
| 24 | + if (action === 'Init Nextjs') { |
| 25 | + const { path } = await inquirer.prompt([ |
| 26 | + { |
| 27 | + type: "input", |
| 28 | + name: "path", |
| 29 | + message: "Where?", |
| 30 | + default: "." |
| 31 | + } |
| 32 | + ]); |
| 33 | + const { packageManager } = await loadToolConfig(); |
| 34 | + |
| 35 | + if(process.stdin.isTTY) { |
| 36 | + process.stdin.setRawMode(false); |
| 37 | + } |
| 38 | + try { |
| 39 | + await runCommand(packageManager, ['create', 'next-app@latest', path]); |
| 40 | + } catch (error) { |
| 41 | + console.error('An error occurred:', error); |
| 42 | + } |
| 43 | + |
| 44 | + } else if (action === 'Run Nextjs') { |
| 45 | + const { packageManager } = await loadToolConfig(); |
| 46 | + await runCommand(packageManager, ['run', 'dev']); |
| 47 | + |
| 48 | + } else if (action === 'Add Component') { |
| 49 | + const { name, type } = await inquirer.prompt([ |
| 50 | + { type: 'input', name: 'name', message: 'Component name:' }, |
| 51 | + { |
| 52 | + type: 'list', |
| 53 | + name: 'type', |
| 54 | + message: 'Component type:', |
| 55 | + choices: ['client', 'server'] |
| 56 | + } |
| 57 | + ]); |
| 58 | + |
| 59 | + const dir = config.componentPath.replace('{{type}}', type); |
| 60 | + const outPath = `${dir}/${name}.tsx`; |
| 61 | + |
| 62 | + if(type == "server") { |
| 63 | + await createFromTemplate('server_component', outPath, { name }); |
| 64 | + } else { |
| 65 | + await createFromTemplate('client_component', outPath, { name }); |
| 66 | + } |
| 67 | + |
| 68 | + } else if (action === 'Add Page') { |
| 69 | + const { route } = await inquirer.prompt([ |
| 70 | + { type: 'input', name: 'route', message: 'Route path (e.g. /about):' } |
| 71 | + ]); |
| 72 | + const routeName = route.replace(/^\//, '') || 'index'; |
| 73 | + const dir = config.pagePath.replace('{{route}}', routeName); |
| 74 | + const outPath = `${dir}/page.tsx`; |
| 75 | + await createFromTemplate('page', outPath, { route }); |
| 76 | + |
| 77 | + } else if (action === 'Add Server Action') { |
| 78 | + const { name } = await inquirer.prompt([ |
| 79 | + { type: 'input', name: 'name', message: 'Action name:' } |
| 80 | + ]); |
| 81 | + const outPath = `${config.actionPath}/${name}.ts`; |
| 82 | + await createFromTemplate('action', outPath, { name }); |
| 83 | + |
| 84 | + } else if (action === 'Make Templates Local') { |
| 85 | + recopyTemplates(); |
| 86 | + |
| 87 | + } else if (action === 'Edit Config') { |
| 88 | + const answers = await inquirer.prompt([ |
| 89 | + { type: 'input', name: 'componentPath', message: 'Component path:', default: config.componentPath }, |
| 90 | + { type: 'input', name: 'pagePath', message: 'Page path:', default: config.pagePath }, |
| 91 | + { type: 'input', name: 'actionPath', message: 'Server Action path:', default: config.actionPath }, |
| 92 | + { type: 'list', name: 'style', message: 'Style system:', choices: ['css-module', 'tailwind', 'none'], default: config.style }, |
| 93 | + { type: 'list', name: 'packageManager', message: 'Package manager:', choices: ['npm', 'yarn', 'pnpm', 'bun', 'deno'], default: config.packageManager } |
| 94 | + ]); |
| 95 | + await saveToolConfig(answers); |
| 96 | + } |
| 97 | + |
| 98 | + process.exit(0); |
| 99 | +}; |
| 100 | + |
| 101 | +main(); |
0 commit comments