From 1eed7034892d4bb852601f9bf647591c8fadc283 Mon Sep 17 00:00:00 2001 From: INeedJobToStartWork Date: Thu, 4 Jul 2024 13:34:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20=20Chore(process.env=20def?= =?UTF-8?q?inition,select,config):=20change=20cli=20select=20structure,=20?= =?UTF-8?q?fix=20process.env,config=20validator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/myplop/src/cli/category.ts | 66 ------------------- packages/myplop/src/cli/config.ts | 2 +- packages/myplop/src/cli/default.ts | 8 +-- packages/myplop/src/cli/profile.ts | 2 +- packages/myplop/src/cli/select/index.ts | 47 +++++++++++++ .../myplop/src/cli/select/options/config.ts | 27 ++++++++ .../myplop/src/cli/select/options/profile.ts | 44 +++++++++++++ packages/myplop/src/function/config.ts | 11 +--- packages/myplop/src/index.ts | 7 +- 9 files changed, 128 insertions(+), 86 deletions(-) delete mode 100644 packages/myplop/src/cli/category.ts create mode 100644 packages/myplop/src/cli/select/index.ts create mode 100644 packages/myplop/src/cli/select/options/config.ts create mode 100644 packages/myplop/src/cli/select/options/profile.ts diff --git a/packages/myplop/src/cli/category.ts b/packages/myplop/src/cli/category.ts deleted file mode 100644 index 5c7e15f..0000000 --- a/packages/myplop/src/cli/category.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { program } from "commander"; -import * as prompter from "@clack/prompts"; -import chalk from "chalk"; -import logging from "@/utils/logging"; -import { join } from "path"; -import { config } from "@/function/config"; -import { scanDir } from "@/function"; - -program - .command("category") - .description("Select category") - .option("-D, --DEBUG", "DEBUG MODE") - .action(({ DEBUG }) => { - process.env.DEBUG = DEBUG ? "TRUE" : "FALSE"; - - const [state, setState] = config(); - prompter - .group({ - intro: () => { - prompter.intro(chalk.bgCyan(" Select Category ")); - }, - currentlyChecked: () => { - prompter.note(`Currently selected Category:\n${state.category}`, chalk.bgBlue(" Info ")); - }, - selectedCategory: async () => { - if (state.config === null) { - prompter.cancel("You need to select config first."); - process.exit(1); - } - if (state.profile === null) { - prompter.cancel("You need to select profile first."); - process.exit(1); - } - logging.debug("Category path: ", process.env.CONFIGPATH); - - const pathToScan = join(process.env.CONFIGPATH, "config", state.config, "profiles", state.profile); - const Categorys = scanDir(pathToScan, "category"); - - logging.debug("Found Categorys:", Categorys); - const selected = await prompter.select({ - message: "Select Category", - initialValue: state.category, - options: Categorys.map(({ name }) => ({ label: name, value: name })) - }); - - logging.debug("Selected Category: ", selected); - if (prompter.isCancel(selected) || !selected) { - prompter.cancel("Canceled!"); - process.exit(0); - } - return selected; - }, - selecting: ({ results }) => { - if (results.selectedCategory === state.category) return void 0; - setState(e => ({ ...e, category: results.selectedCategory! })); - logging.debug("Category state: ", state); - }, - outro: () => { - prompter.outro(chalk.bgGreen(" Category Selected! ")); - } - }) - .catch(e => { - logging.error(e); - process.exit(1); - }); - }); diff --git a/packages/myplop/src/cli/config.ts b/packages/myplop/src/cli/config.ts index 47de0c2..02c499c 100644 --- a/packages/myplop/src/cli/config.ts +++ b/packages/myplop/src/cli/config.ts @@ -37,7 +37,7 @@ program }, selecting: ({ results }) => { if (results.selectedConfig === state.config) return; - setState(e => ({ ...e, config: results.selectedConfig!, profile: null, category: null })); + setState(e => ({ ...e, config: results.selectedConfig!, profile: void 0 })); logging.debug("Config state: ", state); }, outro: () => { diff --git a/packages/myplop/src/cli/default.ts b/packages/myplop/src/cli/default.ts index e76a9c5..89b3d3e 100644 --- a/packages/myplop/src/cli/default.ts +++ b/packages/myplop/src/cli/default.ts @@ -13,18 +13,14 @@ program.action(() => { prompter.intro(chalk.bgCyan(" MyPlop ")); }, selectAction: () => { - if (state.config === null) { + if (state.config === void 0) { prompter.cancel("You need to select config first."); process.exit(1); } - if (state.profile === null) { + if (state.profile === void 0) { prompter.cancel("You need to select profile first."); process.exit(1); } - if (state.category === null) { - prompter.cancel("You need to select category first."); - process.exit(1); - } } }, { diff --git a/packages/myplop/src/cli/profile.ts b/packages/myplop/src/cli/profile.ts index 999e581..91cea33 100644 --- a/packages/myplop/src/cli/profile.ts +++ b/packages/myplop/src/cli/profile.ts @@ -23,7 +23,7 @@ program prompter.note(`Currently selected profile:\n${state.profile}`, chalk.bgBlue(" Info ")); }, selectedprofile: async () => { - if (state.config === null) { + if (state.config === void 0) { prompter.cancel("You need to select config first."); process.exit(1); } diff --git a/packages/myplop/src/cli/select/index.ts b/packages/myplop/src/cli/select/index.ts new file mode 100644 index 0000000..778be38 --- /dev/null +++ b/packages/myplop/src/cli/select/index.ts @@ -0,0 +1,47 @@ +import { program } from "commander"; +import * as prompter from "@clack/prompts"; +import chalk from "chalk"; +import logging from "@/utils/logging"; + +program + .command("select") + .description("Select config properties") + .option("-D, --DEBUG", "output extra debugging", false) + .action(({ DEBUG }) => { + process.env.DEBUG = `${DEBUG}`.toUpperCase(); + + prompter + .group( + { + intro: () => { + prompter.intro(chalk.bgCyan(" Select ")); + }, + choice: async () => + prompter.select({ + message: "What do you want to select?", + initialValue: "app", + options: [ + { label: "Config", value: "config.ts"}, + { label: "Profile", value: "profile.ts" } + ] + }), + loadModule: async ({ results }) => { + try { + await import(`./options/${results.choice}`); + } catch (err) { + logging.error("Module not found - probably file with option doesnt exist.", err); + process.exit(1); + } + } + }, + { + onCancel: () => { + console.log(chalk.red("Canceled.")); + process.exit(0); + } + } + ) + .catch(e => { + throw e; + }); + }); diff --git a/packages/myplop/src/cli/select/options/config.ts b/packages/myplop/src/cli/select/options/config.ts new file mode 100644 index 0000000..186ebf0 --- /dev/null +++ b/packages/myplop/src/cli/select/options/config.ts @@ -0,0 +1,27 @@ +import * as prompter from "@clack/prompts"; +import chalk from "chalk"; +import { resolve } from "path"; +import { copyFiles, existFilePrompt } from "@/function"; + +prompter + .group( + { + intro: () => { + prompter.intro(chalk.bgCyan(" App installation ")); + }, + // configFinder: async () => {}, + // userChoose: async () => {}, + outro: () => { + prompter.outro(chalk.bgGreen(" App has been installed! ")); + } + }, + { + onCancel: () => { + console.log(chalk.bgRed("Canceled")); + process.exit(0); + } + } + ) + .catch(e => { + throw e; + }); diff --git a/packages/myplop/src/cli/select/options/profile.ts b/packages/myplop/src/cli/select/options/profile.ts new file mode 100644 index 0000000..f0cd721 --- /dev/null +++ b/packages/myplop/src/cli/select/options/profile.ts @@ -0,0 +1,44 @@ +import * as prompter from "@clack/prompts"; +import chalk from "chalk"; +import { resolve } from "path"; +import { copyFiles, existFilePrompt } from "@/function"; + +prompter + .group( + { + ConfirmProcess: async () => { + prompter.note(`Initialize app at: \n'${chalk.blue(process.env.CONFIGPATH)}'`, chalk.bgGreen(" ACTIONS ")); + const continueAction = await prompter.confirm({ message: "Do you want to continue?" }); + if (prompter.isCancel(continueAction) || !continueAction) { + prompter.cancel("Canceled."); + process.exit(0); + } + }, + Installation: async () => { + const destination = process.env.CONFIGPATH!; + const templatePath = resolve(process.env.PACKAGEPATH!, "./templates/app/installation/"); + + const InstallingProcess = prompter.spinner(); + InstallingProcess.start("App is installing..."); + await existFilePrompt(destination); + const [data, isError] = copyFiles(templatePath, destination, true); + if (isError) { + InstallingProcess.stop(`❌ Error: ${data.message?.user}`, 1); + process.exit(1); + } + InstallingProcess.stop("✅", 0); + }, + outro: async () => { + prompter.outro(chalk.bgGreen(" App has been installed! ")); + } + }, + { + onCancel: () => { + console.log(chalk.bgRed("Canceled")); + process.exit(0); + } + } + ) + .catch(e => { + throw e; + }); diff --git a/packages/myplop/src/function/config.ts b/packages/myplop/src/function/config.ts index 3ae5485..10273a5 100644 --- a/packages/myplop/src/function/config.ts +++ b/packages/myplop/src/function/config.ts @@ -5,17 +5,10 @@ import { readFileSync, writeFileSync } from "fs"; import { join } from "path"; import { z } from "zod"; -// let readConfig = ; -// readConfig.current.config = results.selectedConfig; -// writeFileSync(join(process.env.CONFIGPATH, "settings.json"), JSON.stringify(readConfig)); -// return [read, write]; -// }; -// Copilot write here function which will read and edit config file. export const configScheme = z.object({ - config: z.string().or(z.null()), - profile: z.string().or(z.null()), - category: z.string().or(z.null()) + config: z.string().or(z.undefined()), + profile: z.string().or(z.undefined()) }); export const config = () => { diff --git a/packages/myplop/src/index.ts b/packages/myplop/src/index.ts index 1e73d8b..fedfe4f 100644 --- a/packages/myplop/src/index.ts +++ b/packages/myplop/src/index.ts @@ -18,15 +18,16 @@ if (process.env.OS != "windows") { process.exit(1); } +// @ts-expect-error - ProcessEnv is default defined but we want to override it + declare global { module NodeJS { - // @ts-expect-error - ProcessEnv is default defined but we want to override it - export type ProcessEnv = Record & { + export interface ProcessEnv extends Record { OS: typeof checkSystem; PACKAGEPATH: string; CONFIGPATH: string; USERPATH: string; - }; + } } }