Skip to content

Commit

Permalink
♻️ Refactor(@cli/select): merge cli commands config and profile t…
Browse files Browse the repository at this point in the history
…o `select` command
  • Loading branch information
INeedJobToStartWork committed Jul 4, 2024
1 parent 1eed703 commit 56528c4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 164 deletions.
50 changes: 0 additions & 50 deletions packages/myplop/src/cli/config.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/myplop/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export * from "./default";
export * from "./init";
export * from "./version";
export * from "./config";
export * from "./profile";
export * from "./category";
export * from "./select";
57 changes: 0 additions & 57 deletions packages/myplop/src/cli/profile.ts

This file was deleted.

51 changes: 33 additions & 18 deletions packages/myplop/src/cli/select/options/config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import { program } from "commander";
import * as prompter from "@clack/prompts";
import chalk from "chalk";
import { resolve } from "path";
import { copyFiles, existFilePrompt } from "@/function";
import logging from "@/utils/logging";
import { join } from "path";
import { config } from "@/function/config";
import { scanDir } from "@/function";

const [state, setState] = config();
prompter
.group(
{
intro: () => {
prompter.intro(chalk.bgCyan(" App installation "));
},
// configFinder: async () => {},
// userChoose: async () => {},
outro: () => {
prompter.outro(chalk.bgGreen(" App has been installed! "));
}
.group({
intro: () => {
prompter.intro(chalk.bgCyan(" Select config "));
},
{
onCancel: () => {
console.log(chalk.bgRed("Canceled"));
process.exit(0);
}
currentlyChecked: () => {
prompter.note(`Currently selected config:\n${state.config}`, chalk.bgBlue(" Info "));
},
selectedConfig: async () => {
logging.debug("Config path: ", process.env.CONFIGPATH);
const pathToScan = join(process.env.CONFIGPATH, "./config");
const configs = scanDir(pathToScan, "config");
logging.debug("Found Configs:", configs);
const selected = await prompter.select({
message: "Select config",
initialValue: state.config,
options: configs.map(({ name }) => ({ label: name, value: name }))
});
logging.debug("Selected config: ", selected);
return selected;
},
selecting: ({ results }) => {
if (results.selectedConfig === state.config) return;
setState(e => ({ ...e, config: results.selectedConfig!, profile: void 0 }));
logging.debug("Config state: ", state);
},
outro: () => {
prompter.outro(chalk.bgGreen(" Config Selected! "));
}
)
})
.catch(e => {
throw e;
});
73 changes: 39 additions & 34 deletions packages/myplop/src/cli/select/options/profile.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
import { program } from "commander";
import * as prompter from "@clack/prompts";
import chalk from "chalk";
import { resolve } from "path";
import { copyFiles, existFilePrompt } from "@/function";
import logging from "@/utils/logging";
import { join } from "path";
import { config } from "@/function/config";
import { scanDir } from "@/function";

const [state, setState] = config();
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! "));
}
.group({
intro: () => {
prompter.intro(chalk.bgCyan(" Select profile "));
},
{
onCancel: () => {
console.log(chalk.bgRed("Canceled"));
process.exit(0);
currentlyChecked: () => {
prompter.note(`Currently selected profile:\n${state.profile}`, chalk.bgBlue(" Info "));
},
selectedprofile: async () => {
if (state.config === void 0) {
prompter.cancel("You need to select config first.");
process.exit(1);
}
logging.debug("profile path: ", process.env.CONFIGPATH);

const pathToScan = join(process.env.CONFIGPATH, "config", state.config, "profiles");
const profiles = scanDir(pathToScan, "profile");

logging.debug("Found Profiles:", profiles);
const selected = await prompter.select({
message: "Select profile",
initialValue: state.profile,
options: profiles.map(({ name }) => ({ label: name, value: name }))
});
logging.debug("Selected profile: ", selected);

return selected;
},
selecting: ({ results }) => {
if (results.selectedprofile === state.profile) return void 0;
setState(e => ({ ...e, profile: results.selectedprofile! }));
logging.debug("Profile state: ", state);
},
outro: () => {
prompter.outro(chalk.bgGreen(" Profile Selected! "));
}
)
})
.catch(e => {
throw e;
});
2 changes: 0 additions & 2 deletions packages/myplop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ 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 {
export interface ProcessEnv extends Record<string, unknown> {
Expand Down

0 comments on commit 56528c4

Please sign in to comment.