From 249cc73fd5251274e2742aada1a68b4c457c4f8e Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Fri, 29 Mar 2024 15:27:22 -0700 Subject: [PATCH] feat: resource list panel --- package.json | 1 + src/cli.ts | 4 ++-- src/help/cli.ts | 18 ++++++++++++++++++ src/help/form.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/help/index.ts | 0 src/info/form.ts | 46 ++++++++++++++++++++++++++++++++++------------ 6 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 src/help/cli.ts create mode 100644 src/help/form.ts create mode 100644 src/help/index.ts diff --git a/package.json b/package.json index a55129d..6ac121f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@expressots/boost-ts": "1.1.1", "chalk-animation": "2.0.3", "cli-progress": "3.11.2", + "cli-table3": "^0.6.4", "degit": "2.8.4", "glob": "10.2.6", "inquirer": "8.0.0", diff --git a/src/cli.ts b/src/cli.ts index cfaf9eb..8873c20 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,12 +4,11 @@ import yargs from "yargs"; import { hideBin } from "yargs/helpers"; import { runCommandModule } from "./commands/project.commands"; import { generateProject } from "./generate"; +import { helpCommand } from "./help/cli"; import { infoProject } from "./info"; import { createProject } from "./new"; import { generateProviders } from "./providers"; -export const CLI_VERSION = "1.3.4"; - console.log(`\n[🐎 Expressots]\n`); yargs(hideBin(process.argv)) @@ -19,6 +18,7 @@ yargs(hideBin(process.argv)) .command(generateProviders()) .command(generateProject()) .command(infoProject()) + .command(helpCommand()) .example("$0 new expressots-demo", "Create interactively") .example("$0 new expressots-demo -d ./", "Create interactively with path") .example("$0 new expressots-demo -p yarn -t opinionated", "Create silently") diff --git a/src/help/cli.ts b/src/help/cli.ts new file mode 100644 index 0000000..489ce58 --- /dev/null +++ b/src/help/cli.ts @@ -0,0 +1,18 @@ +import { CommandModule } from "yargs"; +import { helpForm } from "./form"; + +// eslint-disable-next-line @typescript-eslint/ban-types +type CommandModuleArgs = {}; + +const helpCommand = (): CommandModule => { + return { + command: "resources", + describe: "Resource list", + aliases: ["r"], + handler: async () => { + await helpForm(); + }, + }; +}; + +export { helpCommand }; diff --git a/src/help/form.ts b/src/help/form.ts new file mode 100644 index 0000000..3e689e3 --- /dev/null +++ b/src/help/form.ts @@ -0,0 +1,46 @@ +import chalk from "chalk"; +import CliTable3 from "cli-table3"; + +const helpForm = async (): Promise => { + const table = new CliTable3({ + head: [ + chalk.green("Name"), + chalk.green("Alias"), + chalk.green("Description"), + ], + colWidths: [15, 10, 60], + }); + + table.push( + ["new project", "new", "Generate a new project"], + ["info", "i", "Provides project information"], + ["resources", "r", "Displays cli commands and resources"], + ["help", "h", "Show command help"], + [ + "service", + "g s", + "Generate a service [controller, usecase, dto, module]", + ], + ["controller", "g c", "Generate a controller"], + ["usecase", "g u", "Generate a usecase"], + ["dto", "g d", "Generate a dto"], + ["entity", "g e", "Generate an entity"], + ["provider", "g p", "Generate a provider"], + ["module", "g mo", "Generate a module"], + ["middleware", "g mi", "Generate a middleware"], + ); + console.log( + chalk.bold.white("ExpressoTS:", `${chalk.green("Resources List")}`), + ); + console.log(chalk.whiteBright(table.toString())); + console.log( + chalk.bold.white( + `📝 More info: ${chalk.green( + "https://doc.expresso-ts.com/docs/category/cli", + )}`, + ), + ); + console.log("\n"); +}; + +export { helpForm }; diff --git a/src/help/index.ts b/src/help/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/info/form.ts b/src/info/form.ts index 321417d..1e217ba 100644 --- a/src/info/form.ts +++ b/src/info/form.ts @@ -2,8 +2,27 @@ import chalk from "chalk"; import path from "path"; import fs from "fs"; import os from "os"; -import { CLI_VERSION } from "../cli"; import { printError } from "../utils/cli-ui"; +import { exec } from "child_process"; +import util from "util"; + +const execPromisified = util.promisify(exec); + +async function getCLIVersion(): Promise { + try { + const { stdout } = await execPromisified("npm list -g @expressots/cli"); + const version = stdout; + const versionRegex = /@expressots\/cli@(\d+\.\d+\.\d+)/; + const versionMatch: string = version.match(versionRegex)?.[1]; + return versionMatch; + } catch (error) { + printError( + "CLI version not available.", + "Unable to get the CLI version.", + ); + } + return ""; +} function getInfosFromPackage() { try { @@ -16,10 +35,12 @@ function getInfosFromPackage() { const packageJson = JSON.parse(fileContents); console.log(chalk.green("ExpressoTS Project:")); - console.log(chalk.bold(`\tName: ${packageJson.name}`)); - console.log(chalk.bold(`\tDescription: ${packageJson.description}`)); - console.log(chalk.bold(`\tVersion: ${packageJson.version}`)); - console.log(chalk.bold(`\tAuthor: ${packageJson.author}`)); + console.log(chalk.white(`\tName: ${packageJson.name}`)); + console.log(chalk.white(`\tDescription: ${packageJson.description}`)); + console.log(chalk.white(`\tVersion: ${packageJson.version}`)); + console.log(chalk.white(`\tAuthor: ${packageJson.author}`)); + + //getCLIVersion(); } catch (error) { printError( "No project information available.", @@ -28,15 +49,16 @@ function getInfosFromPackage() { } } -const infoForm = async (): Promise => { - console.log(chalk.green("System informations:")); - console.log(chalk.bold(`\tOS Version: ${os.version()}`)); - console.log(chalk.bold(`\tNodeJS version: ${process.version}`)); +const infoForm = (): void => { + const cliVersion = "1.6.0"; //await getCLIVersion(); + getInfosFromPackage(); + + console.log(chalk.green("System information:")); + console.log(chalk.white(`\tOS Version: ${os.version()}`)); + console.log(chalk.white(`\tNodeJS version: ${process.version}`)); console.log(chalk.green("CLI Version:")); - console.log(chalk.bold(`\tCurrent version: v${CLI_VERSION}`)); - - getInfosFromPackage(); + console.log(chalk.white(`\tCurrent version: v${cliVersion}`)); }; export { infoForm };