Skip to content
Merged
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
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
"globby": "^16.1.0",
"jsonc-parser": "^3.3.1",
"ky": "^1.14.2",
"lodash.kebabcase": "^4.1.1",
"p-wait-for": "^6.0.0",
"zod": "^4.3.5"
},
"devDependencies": {
"@stylistic/eslint-plugin": "^5.6.1",
"@types/ejs": "^3.1.5",
"@types/lodash.kebabcase": "^4.1.9",
"@types/node": "^22.10.5",
"@typescript-eslint/eslint-plugin": "^8.51.0",
"@typescript-eslint/parser": "^8.51.0",
Expand Down
96 changes: 96 additions & 0 deletions src/cli/commands/project/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { resolve } from "node:path";
import { Command } from "commander";
import { log, group, text, select } from "@clack/prompts";
import type { Option } from "@clack/prompts";
import chalk from "chalk";
import kebabCase from "lodash.kebabcase";
import { loadProjectEnv } from "@core/config.js";
import { createProjectFiles, listTemplates } from "@core/project/index.js";
import type { Template } from "@core/project/index.js";
import { runTask, printBanner, onPromptCancel } from "../../utils/index.js";

async function create(): Promise<void> {
printBanner();

// Load .env.local from project root (if in a project)
await loadProjectEnv();

const templates = await listTemplates();
const templateOptions: Array<Option<Template>> = templates.map((t) => ({
value: t,
label: t.name,
hint: t.description,
}));

const { template, name, description, projectPath } = await group(
{
template: () =>
select({
message: "Select a project template",
options: templateOptions,
}),
name: () =>
text({
message: "What is the name of your project?",
placeholder: "my-app-backend",
validate: (value) => {
if (!value || value.trim().length === 0) {
return "Project name is required";
}
},
}),
description: () =>
text({
message: "Project description (optional)",
placeholder: "A brief description of your project",
}),
projectPath: async ({ results }) => {
const suggestedPath = `./${kebabCase(results.name)}`;
return text({
message: "Where should we create the base44 folder?",
placeholder: suggestedPath,
initialValue: suggestedPath,
});
},
},
{
onCancel: onPromptCancel,
}
);

const resolvedPath = resolve(projectPath as string);

// Create the project
await runTask(
"Creating project...",
async () => {
return await createProjectFiles({
name: name.trim(),
description: description ? description.trim() : undefined,
path: resolvedPath,
template,
});
},
{
successMessage: "Project created successfully",
errorMessage: "Failed to create project",
}
);

log.success(`Project ${chalk.bold(name)} has been initialized!`);
}

export const createCommand = new Command("create")
.description("Create a new Base44 project")
.action(async () => {
try {
await create();
} catch (e) {
if (e instanceof Error) {
log.error(e.stack ?? e.message);
} else {
log.error(String(e));
}
process.exit(1);
}
});
75 changes: 0 additions & 75 deletions src/cli/commands/project/init.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { whoamiCommand } from "./commands/auth/whoami.js";
import { logoutCommand } from "./commands/auth/logout.js";
import { showProjectCommand } from "./commands/project/show-project.js";
import { entitiesPushCommand } from "./commands/entities/push.js";
import { initCommand } from "./commands/project/init.js";
import { createCommand } from "./commands/project/create.js";
import packageJson from "../../package.json";

const program = new Command();
Expand All @@ -24,7 +24,7 @@ program.addCommand(whoamiCommand);
program.addCommand(logoutCommand);

// Register project commands
program.addCommand(initCommand);
program.addCommand(createCommand);
program.addCommand(showProjectCommand);

// Register entities commands
Expand Down
26 changes: 7 additions & 19 deletions src/cli/utils/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { text, isCancel, cancel } from "@clack/prompts";
import type { TextOptions } from "@clack/prompts";
import { cancel } from "@clack/prompts";

/**
* Handles prompt cancellation by exiting gracefully.
* Standard onCancel handler for prompt groups.
* Exits the process gracefully when the user cancels.
*/
function handleCancel<T>(value: T | symbol): asserts value is T {
if (isCancel(value)) {
cancel("Operation cancelled.");
process.exit(0);
}
}

/**
* Wrapper around @clack/prompts text() that handles cancellation automatically.
* Returns the string value directly, exits process if cancelled.
*/
export async function textPrompt(options: TextOptions): Promise<string> {
const value = await text(options);
handleCancel(value);
return value;
}
export const onPromptCancel = () => {
cancel("Operation cancelled.");
process.exit(0);
};
11 changes: 10 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { join } from "node:path";
import { dirname, join } from "node:path";
import { homedir } from "node:os";
import { fileURLToPath } from "node:url";
import { config } from "dotenv";
import { findProjectRoot } from "./project/index.js";

// After bundling, import.meta.url points to dist/cli/index.js
// Templates are copied to dist/cli/templates/
const __dirname = dirname(fileURLToPath(import.meta.url));

// Static constants
export const PROJECT_SUBDIR = "base44";
export const FUNCTION_CONFIG_FILE = "function.jsonc";
Expand All @@ -17,6 +22,10 @@ export function getAuthFilePath() {
return join(getBase44Dir(), "auth", "auth.json");
}

export function getTemplatesDir() {
return join(__dirname, "templates");
}

export function getProjectConfigPatterns() {
return [
`${PROJECT_SUBDIR}/config.jsonc`,
Expand Down
48 changes: 48 additions & 0 deletions src/core/project/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { globby } from "globby";
import { getProjectConfigPatterns } from "../config.js";
import { createProject } from "./api.js";
import { renderTemplate } from "./template.js";
import type { Template } from "./schema.js";

export interface CreateProjectOptions {
name: string;
description?: string;
path: string;
template: Template;
}

export interface CreateProjectResult {
projectDir: string;
}

export async function createProjectFiles(
options: CreateProjectOptions
): Promise<CreateProjectResult> {
const { name, description, path: basePath, template } = options;

// Check if project already exists
const existingConfigs = await globby(getProjectConfigPatterns(), {
cwd: basePath,
absolute: true,
});

if (existingConfigs.length > 0) {
throw new Error(
`A Base44 project already exists at ${existingConfigs[0]}. Please choose a different location.`
);
}

// Create the project via API to get the app ID
const { projectId } = await createProject(name, description);

// Render the template to the destination path
await renderTemplate(template, basePath, {
name,
description,
projectId,
});

return {
projectDir: basePath,
};
}
3 changes: 2 additions & 1 deletion src/core/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type * from "./baseResource.js";
export * from "./config.js";
export * from "./schema.js";
export * from "./api.js";
export * from "./init.js";
export * from "./create.js";
export * from "./template.js";
Loading