From 8ed033b3a3d64febd03b93fbca1cd571d6a46d39 Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Sat, 27 Apr 2024 19:33:53 -0700 Subject: [PATCH] feat: add external provider scaffold --- src/help/form.ts | 3 +- src/providers/cli.ts | 6 +- src/providers/external/external.provider.ts | 68 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/providers/external/external.provider.ts diff --git a/src/help/form.ts b/src/help/form.ts index 3e689e3..1c15272 100644 --- a/src/help/form.ts +++ b/src/help/form.ts @@ -8,7 +8,7 @@ const helpForm = async (): Promise => { chalk.green("Alias"), chalk.green("Description"), ], - colWidths: [15, 10, 60], + colWidths: [15, 15, 60], }); table.push( @@ -26,6 +26,7 @@ const helpForm = async (): Promise => { ["dto", "g d", "Generate a dto"], ["entity", "g e", "Generate an entity"], ["provider", "g p", "Generate a provider"], + ["provider external", "a provider", "Generate an external provider"], ["module", "g mo", "Generate a module"], ["middleware", "g mi", "Generate a middleware"], ); diff --git a/src/providers/cli.ts b/src/providers/cli.ts index 4c84a41..329b7f7 100644 --- a/src/providers/cli.ts +++ b/src/providers/cli.ts @@ -1,4 +1,5 @@ import { Argv, CommandModule } from "yargs"; +import { externalProvider } from "./external/external.provider"; import { prismaProvider } from "./prisma/prisma.provider"; // eslint-disable-next-line @typescript-eslint/ban-types @@ -12,9 +13,10 @@ const generateProviders = (): CommandModule => { builder: (yargs: Argv): Argv => { yargs .positional("provider", { - choices: ["prisma"] as const, + choices: ["prisma", "provider"] as const, describe: "The provider to add to the project", type: "string", + alias: "p", }) .option("library-version", { describe: "The library version to install", @@ -34,6 +36,8 @@ const generateProviders = (): CommandModule => { handler: async ({ provider, libraryVersion, providerVersion }) => { if (provider === "prisma") { await prismaProvider(libraryVersion, providerVersion); + } else if (provider === "provider") { + await externalProvider(); } }, }; diff --git a/src/providers/external/external.provider.ts b/src/providers/external/external.provider.ts new file mode 100644 index 0000000..5c78691 --- /dev/null +++ b/src/providers/external/external.provider.ts @@ -0,0 +1,68 @@ +import chalk from "chalk"; +import degit from "degit"; +import inquirer from "inquirer"; +import { centerText } from "../../utils/center-text"; +import { printError } from "../../utils/cli-ui"; + +async function printInfo(providerName: string): Promise { + console.log("\n"); + console.log( + "🐎 Provider", + chalk.green(providerName), + "created successfully!", + ); + console.log("🤙 Run the following commands to start the provider:\n"); + + console.log(chalk.bold.gray(`$ cd ${providerName}`)); + + console.log("\n"); + console.log(chalk.bold.green(centerText("Happy coding!"))); + console.log( + chalk.bold.gray( + centerText("Please consider donating to support the project.\n"), + ), + ); + console.log( + chalk.bold.white( + centerText("💖 Sponsor: https://github.com/sponsors/expressots"), + ), + ); + console.log("\n"); +} + +interface IExternalProvider { + providerName: string; +} + +const externalProvider = async (): Promise => { + return new Promise(async (resolve, reject) => { + const providerInfo = await inquirer.prompt([ + { + type: "input", + name: "providerName", + message: "Type the name of your provider:", + default: "expressots-provider", + transformer: (input: string) => { + return chalk.yellow(chalk.bold(input)); + }, + }, + ]); + + try { + const emitter = degit(`expressots/expressots-provider-template`); + await emitter.clone(providerInfo.providerName); + printInfo(providerInfo.providerName); + + resolve(); + } catch (err: any) { + console.log("\n"); + printError( + "Project already exists or Folder is not empty", + "generate-external-provider", + ); + reject(err); + } + }); +}; + +export { externalProvider };