Skip to content

Commit

Permalink
Merge pull request #3 from palico-ai/fix-template
Browse files Browse the repository at this point in the history
FIX: Made it simplier and more clear to get started
  • Loading branch information
shikdernyc authored Feb 13, 2024
2 parents e576ae4 + 3e661d6 commit 1dc3595
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
54 changes: 34 additions & 20 deletions src/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import * as path from 'path'
import * as chalk from 'chalk'
import { copyDirectory } from '../utils/copy'
import * as path from "path";
import * as chalk from "chalk";
import { copyDirectory } from "../utils/copy";
import { createFile } from "../utils/create_file";
import { runCommands } from "../utils/run_command";

const ENV_FILE_CONTENT = `
# Add your OpenAI API key
OPENAI_API_KEY=""
# Add the model you want to use
OPENAI_MODEL="gpt-3.5-turbo-0125"
`;

export const InitHandler = async (projectName: string) => {
console.log(projectName)
if(!projectName || projectName.length === 0) {
throw new Error('Project name is required')
if (!projectName || projectName.length === 0) {
throw new Error("Project name is required");
}
console.log(`Initializing ${projectName}...`)
const templateDirectory = path.join(__dirname, '..', '..', 'templates', 'base')
const destinationDirectory = path.join(process.cwd(), projectName)
await copyDirectory(templateDirectory, destinationDirectory)
console.log(`Initializing ${projectName}...`);
const templateDirectory = path.join(
__dirname,
"..",
"..",
"templates",
"base"
);
const destinationDirectory = path.join(process.cwd(), projectName);
await copyDirectory(templateDirectory, destinationDirectory);
await runCommands([`cd ${projectName} && npm install`]);
await createFile(path.join(destinationDirectory, ".env"), ENV_FILE_CONTENT);
const nextSteps = [
`cd ${projectName}`,
"npm install",
"Update LLM Model Config in src/index.ts",
"Run 'palico-cli dev' to start a local server"
]
console.log(chalk.green('Project initialized!'))
console.log(chalk.blue('Next Steps:'))
`Navigate to the project directory: ${chalk.greenBright(`cd ${projectName}`)}`,
`Update ${chalk.greenBright(".env")} with your OpenAI API key and model`,
`Run ${chalk.greenBright("npm start")} to start the application`,
];
console.log(chalk.green("Project initialized!"));
console.log(chalk.blue("Next Steps:"));
nextSteps.forEach((step, index) => {
console.log(chalk.blue(`${index + 1}. ${step}`))
})
}
console.log(chalk.blue(`${index + 1}. ${step}`));
});
};
5 changes: 5 additions & 0 deletions src/utils/create_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as fs from 'fs';

export const createFile = async (path: string, content?: string) => {
await fs.promises.writeFile(path, content ?? '', 'utf-8');
}
13 changes: 13 additions & 0 deletions src/utils/run_command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { promisify } from 'util'
import { exec } from 'child_process'

export const runCommands = async (commands: string[]): Promise<void> => {
const execAsync = promisify(exec)
for (const command of commands) {
const { stdout, stderr } = await execAsync(command)
console.log(stdout)
if (stderr) {
throw new Error(stderr)
}
}
}
3 changes: 2 additions & 1 deletion templates/base/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
palico.out
.DS_Store
.DS_Store
.env
3 changes: 2 additions & 1 deletion templates/base/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ services:
environment:
- DB_URL=postgres://root:root@postgres_db:5432/root
- PORT=8000
- OPENAI_API_KEY=<your-openai-api-key>
- OPENAI_API_KEY=${OPENAI_API_KEY}
- OPENAI_MODEL=${OPENAI_MODEL}
4 changes: 2 additions & 2 deletions templates/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ dotenv.config();

const openaiApiKey = process.env.OPENAI_API_KEY;

if (!openaiApiKey) {
throw new Error("OpenAI API Key not found");
if (!openaiApiKey || openaiApiKey === "") {
throw new Error("OpenAI API Key not found. Please set OPENAI_API_KEY in your environment.");
}

const model = process.env.OPENAI_MODEL ?? "gpt-3.5-turbo-0125";
Expand Down

0 comments on commit 1dc3595

Please sign in to comment.