Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@insforge/cli",
"version": "0.1.21",
"version": "0.1.26",
"description": "InsForge CLI - Command line tool for InsForge platform",
"type": "module",
"bin": {
Expand Down
108 changes: 105 additions & 3 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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';
Expand All @@ -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' },
],
});
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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 },
);

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) {
Copy link
Member

Choose a reason for hiding this comment

The 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...');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call insforge db query {sql} instead of REST API?

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(() => {});
}
}

33 changes: 33 additions & 0 deletions src/lib/env.ts
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 [];
}
Loading