-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add chatbot template support #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d2676fe
56c603b
63a66f1
53cd4e8
10fe7e6
cc36a42
a32af9e
e86b608
f4572f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,12 @@ import { | |
| getProject, | ||
| getProjectApiKey, | ||
| } from '../lib/api/platform.js'; | ||
| import { getAnonKey } from '../lib/api/oss.js'; | ||
| import { getAnonKey, ossFetch } from '../lib/api/oss.js'; | ||
| import { getGlobalConfig, saveGlobalConfig, saveProjectConfig, getFrontendUrl } from '../lib/config.js'; | ||
| import { requireAuth } from '../lib/credentials.js'; | ||
| import { handleError, getRootOpts, CLIError } from '../lib/errors.js'; | ||
| import { outputJson } from '../lib/output.js'; | ||
| import { readEnvFile } from '../lib/env.js'; | ||
| import { installCliGlobally, installSkills, reportCliUsage } from '../lib/skills.js'; | ||
| import { deployProject } from './deployments/deploy.js'; | ||
| import type { ProjectConfig } from '../types.js'; | ||
|
|
@@ -59,7 +60,7 @@ export function registerCreateCommand(program: Command): void { | |
| .option('--name <name>', 'Project name') | ||
| .option('--org-id <id>', 'Organization ID') | ||
| .option('--region <region>', 'Deployment region (us-east, us-west, eu-central, ap-southeast)') | ||
| .option('--template <template>', 'Template to use: react, nextjs, or empty') | ||
| .option('--template <template>', 'Template to use: react, nextjs, chatbot, or empty') | ||
| .action(async (opts, cmd) => { | ||
| const { json, apiUrl } = getRootOpts(cmd); | ||
| try { | ||
|
|
@@ -108,7 +109,11 @@ export function registerCreateCommand(program: Command): void { | |
| } | ||
|
|
||
| // 3. Select template | ||
| const validTemplates = ['react', 'nextjs', 'chatbot', 'empty']; | ||
| let template = opts.template as string | undefined; | ||
| if (template && !validTemplates.includes(template)) { | ||
| throw new CLIError(`Invalid template "${template}". Valid options: ${validTemplates.join(', ')}`); | ||
| } | ||
| if (!template) { | ||
| if (json) { | ||
| template = 'empty'; | ||
|
|
@@ -118,6 +123,7 @@ export function registerCreateCommand(program: Command): void { | |
| options: [ | ||
| { value: 'react', label: 'Web app template with React' }, | ||
| { value: 'nextjs', label: 'Web app template with Next.js' }, | ||
| { value: 'chatbot', label: 'AI Chatbot with Next.js' }, | ||
| { value: 'empty', label: 'Empty project' }, | ||
| ], | ||
| }); | ||
|
|
@@ -152,7 +158,9 @@ export function registerCreateCommand(program: Command): void { | |
|
|
||
| // 6. Download template if selected | ||
| const hasTemplate = template !== 'empty'; | ||
| if (hasTemplate) { | ||
| if (template === 'chatbot') { | ||
| await downloadGitHubTemplate('chatbot', projectConfig, json); | ||
| } else if (hasTemplate) { | ||
| await downloadTemplate(template as Framework, projectConfig, projectName, json, apiUrl); | ||
| } | ||
|
|
||
|
|
@@ -186,9 +194,17 @@ export function registerCreateCommand(program: Command): void { | |
|
|
||
| if (!clack.isCancel(shouldDeploy) && shouldDeploy) { | ||
| try { | ||
| // Read env vars from .env.local or .env to pass to deployment | ||
| const envVars = await readEnvFile(process.cwd()); | ||
| const startBody: { envVars?: Array<{ key: string; value: string }> } = {}; | ||
| if (envVars.length > 0) { | ||
| startBody.envVars = envVars; | ||
| } | ||
|
|
||
| const deploySpinner = clack.spinner(); | ||
| const result = await deployProject({ | ||
| sourceDir: process.cwd(), | ||
| startBody, | ||
| spinner: deploySpinner, | ||
| }); | ||
|
|
||
|
|
@@ -291,4 +307,90 @@ async function downloadTemplate( | |
| } | ||
| } | ||
|
|
||
| async function downloadGitHubTemplate( | ||
| templateName: string, | ||
| projectConfig: ProjectConfig, | ||
| json: boolean, | ||
| ): Promise<void> { | ||
| const s = !json ? clack.spinner() : null; | ||
| s?.start(`Downloading ${templateName} template...`); | ||
|
|
||
| const tempDir = path.join(tmpdir(), `insforge-template-${Date.now()}`); | ||
|
|
||
| try { | ||
| await fs.mkdir(tempDir, { recursive: true }); | ||
|
|
||
| // Shallow clone the templates repo | ||
| await execAsync( | ||
| 'git clone --depth 1 https://github.com/InsForge/insforge-templates.git .', | ||
| { cwd: tempDir, maxBuffer: 10 * 1024 * 1024, timeout: 60_000 }, | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const templateDir = path.join(tempDir, templateName); | ||
| const stat = await fs.stat(templateDir).catch(() => null); | ||
| if (!stat?.isDirectory()) { | ||
| throw new Error(`Template "${templateName}" not found in repository`); | ||
| } | ||
|
|
||
| // Copy template files to cwd | ||
| s?.message('Copying template files...'); | ||
| const cwd = process.cwd(); | ||
| await copyDir(templateDir, cwd); | ||
|
|
||
| // Write .env.local from .env.example with InsForge credentials filled in | ||
| const envExamplePath = path.join(cwd, '.env.example'); | ||
| const envExampleExists = await fs.stat(envExamplePath).catch(() => null); | ||
| if (envExampleExists) { | ||
| const anonKey = await getAnonKey(); | ||
| const envExample = await fs.readFile(envExamplePath, 'utf-8'); | ||
| const envContent = envExample.replace( | ||
| /^([A-Z][A-Z0-9_]*=)(.*)$/gm, | ||
| (_, prefix: string, _value: string) => { | ||
| const key = prefix.slice(0, -1); // remove trailing '=' | ||
| if (/INSFORGE.*(URL|BASE_URL)$/.test(key)) return `${prefix}${projectConfig.oss_host}`; | ||
| if (/INSFORGE.*ANON_KEY$/.test(key)) return `${prefix}${anonKey}`; | ||
| if (key === 'NEXT_PUBLIC_APP_URL') return `${prefix}https://${projectConfig.appkey}.insforge.site`; | ||
| return `${prefix}${_value}`; | ||
| }, | ||
| ); | ||
| await fs.writeFile(path.join(cwd, '.env.local'), envContent); | ||
| } | ||
|
|
||
| s?.stop(`${templateName} template downloaded`); | ||
|
|
||
| // Run database migrations if db_int.sql exists | ||
| const migrationPath = path.join(cwd, 'migrations', 'db_int.sql'); | ||
| const migrationExists = await fs.stat(migrationPath).catch(() => null); | ||
| if (migrationExists && !json) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With insforge create --json --template chatbot, the scaffold still includes migrations/db_int.sql, but this block skips it entirely because it is gated on !json. That means the non-interactive/automation path reports success while leaving the chatbot template with an uninitialized schema, so the generated app is broken until the caller notices and runs the SQL manually. |
||
| const runMigration = await clack.confirm({ | ||
| message: 'This template includes a database migration. Apply it now?', | ||
| }); | ||
|
|
||
| if (!clack.isCancel(runMigration) && runMigration) { | ||
| const dbSpinner = clack.spinner(); | ||
| dbSpinner.start('Running database migrations...'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call |
||
| try { | ||
| const sql = await fs.readFile(migrationPath, 'utf-8'); | ||
| await ossFetch('/api/database/advance/rawsql/unrestricted', { | ||
| method: 'POST', | ||
| body: JSON.stringify({ query: sql }), | ||
| }); | ||
| dbSpinner.stop('Database migrations applied'); | ||
| } catch (err) { | ||
| dbSpinner.stop('Database migration failed'); | ||
| clack.log.warn(`Migration failed: ${(err as Error).message}`); | ||
| clack.log.info('You can run the migration manually: insforge db query --unrestricted "$(cat migrations/db_int.sql)"'); | ||
| } | ||
| } | ||
| } | ||
| } catch (err) { | ||
| s?.stop(`${templateName} template download failed`); | ||
| if (!json) { | ||
| clack.log.warn(`Failed to download ${templateName} template: ${(err as Error).message}`); | ||
| clack.log.info('You can manually clone from: https://github.com/InsForge/insforge-templates'); | ||
| } | ||
| } finally { | ||
| await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {}); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as fs from 'node:fs/promises'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| /** | ||
| * Read environment variables from the first env file found in the directory. | ||
| * Priority: .env.local > .env.production > .env | ||
| */ | ||
| export async function readEnvFile(cwd: string): Promise<Array<{ key: string; value: string }>> { | ||
| const candidates = ['.env.local', '.env.production', '.env']; | ||
| for (const name of candidates) { | ||
| const filePath = path.join(cwd, name); | ||
| const exists = await fs.stat(filePath).catch(() => null); | ||
| if (!exists) continue; | ||
|
|
||
| const content = await fs.readFile(filePath, 'utf-8'); | ||
| const vars: Array<{ key: string; value: string }> = []; | ||
| for (const line of content.split('\n')) { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed || trimmed.startsWith('#')) continue; | ||
| const eqIndex = trimmed.indexOf('='); | ||
| if (eqIndex === -1) continue; | ||
| const key = trimmed.slice(0, eqIndex).trim(); | ||
| let value = trimmed.slice(eqIndex + 1).trim(); | ||
| // Strip surrounding quotes | ||
| if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) { | ||
| value = value.slice(1, -1); | ||
| } | ||
| if (key) vars.push({ key, value }); | ||
| } | ||
| return vars; | ||
| } | ||
| return []; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.