Skip to content

Commit

Permalink
⚙️ Chore(process.env definition,select,config): change cli select str…
Browse files Browse the repository at this point in the history
…ucture, fix process.env,config validator
  • Loading branch information
INeedJobToStartWork committed Jul 4, 2024
1 parent 5c0049a commit 1eed703
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 86 deletions.
66 changes: 0 additions & 66 deletions packages/myplop/src/cli/category.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/myplop/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand Down
8 changes: 2 additions & 6 deletions packages/myplop/src/cli/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/myplop/src/cli/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
47 changes: 47 additions & 0 deletions packages/myplop/src/cli/select/index.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});
27 changes: 27 additions & 0 deletions packages/myplop/src/cli/select/options/config.ts
Original file line number Diff line number Diff line change
@@ -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;
});
44 changes: 44 additions & 0 deletions packages/myplop/src/cli/select/options/profile.ts
Original file line number Diff line number Diff line change
@@ -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;
});
11 changes: 2 additions & 9 deletions packages/myplop/src/function/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/myplop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> & {
export interface ProcessEnv extends Record<string, unknown> {
OS: typeof checkSystem;
PACKAGEPATH: string;
CONFIGPATH: string;
USERPATH: string;
};
}
}
}

Expand Down

0 comments on commit 1eed703

Please sign in to comment.