From 1ecb38dfa0b0ea3f6a8a3babf7852b569a2f33fa Mon Sep 17 00:00:00 2001 From: kgricour Date: Fri, 17 Nov 2023 13:57:16 +0100 Subject: [PATCH] remove cli from polyfire-js --- cmd/createApplication/index.ts | 57 ---------- cmd/index.ts | 195 --------------------------------- cmd/utils/index.ts | 68 ------------ package.json | 10 +- tsconfig.json | 2 +- 5 files changed, 5 insertions(+), 327 deletions(-) delete mode 100644 cmd/createApplication/index.ts delete mode 100644 cmd/index.ts delete mode 100644 cmd/utils/index.ts diff --git a/cmd/createApplication/index.ts b/cmd/createApplication/index.ts deleted file mode 100644 index 0c2bc61..0000000 --- a/cmd/createApplication/index.ts +++ /dev/null @@ -1,57 +0,0 @@ -import inquirer, { InputQuestion, ListQuestion } from "inquirer"; -import { Template, TEMPLATE_DETAILS } from ".."; -import { cloneRepository, createEnvironmentFile, cleanOptions } from "../utils"; - -export type CustomQuestion = InputQuestion | ListQuestion; - -function mergeAndCleanOptions( - cliOptions: Record, - userResponses: Record, -): Record { - const mergedOptions = { ...cliOptions, ...userResponses }; - delete mergedOptions.template; - return mergedOptions; -} - -export default async function createApplication( - template: Template, - appName: string, - cliOptions: Record, - additionalPrompts: CustomQuestion[], -): Promise { - const standardPrompts: CustomQuestion[] = [ - { - type: "input", - name: "project", - message: "Enter your project alias (obtain one at https://app.polyfire.com) :", - when: () => !cliOptions?.project, - }, - ]; - - const allPrompts = [...standardPrompts, ...additionalPrompts]; - - try { - const userResponses = await inquirer.prompt(allPrompts); - - const sanitizedResponses = cleanOptions(userResponses); - const finalTemplate = template || sanitizedResponses.template; - - if (!finalTemplate || !TEMPLATE_DETAILS[finalTemplate]) { - throw new Error(`Invalid or missing template: ${finalTemplate}`); - } - - const finalOptions = mergeAndCleanOptions(cliOptions, sanitizedResponses); - const templateConfig = TEMPLATE_DETAILS[finalTemplate]; - - await cloneRepository(templateConfig.repositoryUrl, appName); - await createEnvironmentFile(appName, finalOptions, templateConfig.envVariablePrefix); - - console.info( - `Application setup complete! To get started, run:\n\n cd ${appName};\n npm install;\n npm run dev;\n`, - ); - } catch (error: unknown) { - console.error("An error occurred:", error instanceof Error ? error.message : error); - } - - return true; -} diff --git a/cmd/index.ts b/cmd/index.ts deleted file mode 100644 index ab26a55..0000000 --- a/cmd/index.ts +++ /dev/null @@ -1,195 +0,0 @@ -import { program } from "commander"; -import inquirer from "inquirer"; -import createApplication, { CustomQuestion } from "./createApplication"; - -enum ProjectType { - CHAT = "chat", - AGENT = "agent", -} - -type TemplateName = `${ProjectType.CHAT | ProjectType.AGENT}-${string}${string}`; - -const CHAT_REACT_VITE: TemplateName = "chat-react-vite"; -const CHAT_NEXTJS: TemplateName = "chat-nextjs"; -const AGENT_REACT: TemplateName = "agent-react"; - -const knownTemplates: TemplateName[] = [CHAT_REACT_VITE, CHAT_NEXTJS, AGENT_REACT]; - -export type Template = (typeof knownTemplates)[number]; - -type TemplateDetails = { - repositoryUrl: string; - envVariablePrefix: string; -}; - -const CREATE_CHATBOT = "Create chatbot"; -const CREATE_AGENT = "Create agent"; - -export const TEMPLATE_DETAILS: Record = { - [CHAT_REACT_VITE]: { - repositoryUrl: "https://github.com/polyfire-ai/polyfire-chat-react-boilerplate.git", - envVariablePrefix: "VITE_", - }, - [CHAT_NEXTJS]: { - repositoryUrl: "https://github.com/polyfire-ai/polyfire-chat-nextjs-boilerplate.git", - envVariablePrefix: "NEXT_PUBLIC_", - }, - [AGENT_REACT]: { - repositoryUrl: "https://github.com/polyfire-ai/polyfire-use-agent-boilerplate", - envVariablePrefix: "REACT_APP_", - }, -}; - -const questions = ( - options: Record, - template?: string, -): Record => ({ - chat: [ - { - type: "input", - name: "botname", - message: "Enter the name of the bot", - default: "polyfire-bot", - when: () => !options?.botname, - }, - { - type: "list", - name: "template", - message: "Choose a template", - choices: knownTemplates.filter((t) => t.startsWith(ProjectType.CHAT)), - when: () => !template, - }, - ], - agent: [ - { - type: "list", - name: "template", - message: "Choose a template", - choices: knownTemplates.filter((t) => t.startsWith(ProjectType.AGENT)), - when: () => !template, - }, - ], -}); - -async function handleCommand({ - appName, - template, - ...options -}: { - appName: string; - template?: Template; - botname?: string; - project?: string; -}): Promise { - const answer = await inquirer.prompt([ - { - type: "list", - name: "action", - message: "What do you want to build?", - choices: [CREATE_CHATBOT, CREATE_AGENT, "Quit"], - when: () => !template, - }, - ]); - - if (template && !knownTemplates.includes(template)) { - console.error(`Unknown template: ${template}`); - return; - } else if (!template && !answer.action) { - console.error("No template selected"); - return; - } - - let actionType: string = answer.action; - - if (template) { - if (template.startsWith(ProjectType.CHAT)) { - actionType = ProjectType.CHAT; - } else if (template.startsWith(ProjectType.AGENT)) { - actionType = ProjectType.AGENT; - } - } else if (answer.action === CREATE_CHATBOT) { - actionType = ProjectType.CHAT; - } else if (answer.action === CREATE_AGENT) { - actionType = ProjectType.AGENT; - } - - const additionalQuestions = questions(options, template)[actionType]; - - switch (actionType) { - case ProjectType.CHAT: - case ProjectType.AGENT: - await createApplication(template as Template, appName, options, additionalQuestions); - break; - case "Quit": - console.info("Bye!"); - return; - default: - console.log("Invalid choice"); - break; - } -} - -function displayHelpMessage() { - console.info("Please specify the project directory:"); - console.info(" create-polyfire-app \n"); - console.info("For example:"); - console.info(" create-polyfire-app my-polyfire-app\n"); - console.info("Run 'create-polyfire-app --help' to see all options."); -} - -function groupTemplatesByType(templates: TemplateName[]) { - return templates.reduce>((groupedTemplates, template) => { - const [type] = template.split("-"); - if (!groupedTemplates[type]) { - groupedTemplates[type] = []; - } - groupedTemplates[type].push(template); - return groupedTemplates; - }, {}); -} - -function displayGroupedTemplates(groupedTemplates: Record) { - Object.entries(groupedTemplates).forEach(([type, templates]) => { - console.info(`${type.toUpperCase()}:\n`); - templates.forEach((template, index) => { - console.info(` ${index + 1}. ${template}`); - }); - console.info("\n"); - }); -} - -function configureCLI() { - program - .name("create-polyfire-app") - .description("A tool to create new applications with specified templates.") - .argument("", "The name of the new application") - .option("--template