Skip to content

Commit

Permalink
🎉 Feat(@cli/default): add default cli commander to load scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
INeedJobToStartWork committed Jul 4, 2024
1 parent 56528c4 commit 27a80e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
53 changes: 50 additions & 3 deletions packages/myplop/src/cli/default.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { program } from "commander";
import * as prompter from "@clack/prompts";
import chalk from "chalk";
import { config } from "@/function";
import { config, scanDir } from "@/function";
import { join } from "path";
import { existsSync, readdirSync } from "fs";
import { spawn } from "node:child_process";
import logging from "@/utils/logging";

program.action(() => {
program.option("-D, --DEBUG", "output extra debugging", false).action(({ DEBUG }) => {
process.env.DEBUG = `${DEBUG}`.toUpperCase();
const [state] = config();

prompter
Expand All @@ -12,7 +17,7 @@ program.action(() => {
intro: () => {
prompter.intro(chalk.bgCyan(" MyPlop "));
},
selectAction: () => {
readFiles: async () => {
if (state.config === void 0) {
prompter.cancel("You need to select config first.");
process.exit(1);
Expand All @@ -21,8 +26,50 @@ program.action(() => {
prompter.cancel("You need to select profile first.");
process.exit(1);
}

let path = join(process.env.CONFIGPATH, "config", state.config, "profiles", state.profile);

async function readDir(path: string): Promise<string> {
logging.debug(path);
const dirContent = readdirSync(path, { withFileTypes: true });
logging.debug(dirContent);

if (dirContent.length === 0) {
prompter.cancel("Not found any Script");
process.exit(1);
}

const plopfilePath = join(path, "plopfile.js");
if (existsSync(plopfilePath)) return plopfilePath;

const options = dirContent.map(element => {
const isDir = existsSync(join(path, element.name, "plopfile.js"));
return {
label: element.name,
value: element.name,
hint: isDir ? "Script" : "Directory"
};
});

const choice = await prompter.select({
message: "Dir Content:",
options: options
});

return readDir(join(path, choice));
}
return readDir(path);
},
execute: ({ results }) => {
logging.debug(results.readFiles);
if (results.readFiles) spawn(`pnpm plop --cwd "${results.readFiles}" --dest "./"`, { shell: true, stdio: "inherit" });
else {
prompter.cancel("Not found any Script");
process.exit(1);
}
}
},

{
onCancel: () => {
console.log(chalk.bgRed("Canceled"));
Expand Down
2 changes: 1 addition & 1 deletion packages/myplop/src/cli/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ program
message: "What do you want to select?",
initialValue: "app",
options: [
{ label: "Config", value: "config.ts"},
{ label: "Config", value: "config.ts" },
{ label: "Profile", value: "profile.ts" }
]
}),
Expand Down

0 comments on commit 27a80e6

Please sign in to comment.