From 5ed7cffd4247f9aad69d7628aa159bc8cb8fabb2 Mon Sep 17 00:00:00 2001 From: Stephen Hanson Date: Tue, 9 May 2023 22:23:27 -0500 Subject: [PATCH] Refactor file structure, update how commands run Since the tool is only React Native for now, it didn't seem necessary to have the 'react-native' directory. Moved to 'src'. Also created utility for importing commands for commander --- bin/belt | 2 +- index.ts | 16 ---------------- react-native/eslint.ts | 1 - react-native/prettier.ts | 1 - react-native/react-native.ts | 19 ------------------- src/cli.ts | 25 +++++++++++++++++++++++++ src/commands/eslint.ts | 3 +++ src/commands/prettier.ts | 3 +++ src/util/buildAction.ts | 17 +++++++++++++++++ 9 files changed, 49 insertions(+), 38 deletions(-) delete mode 100755 index.ts delete mode 100755 react-native/eslint.ts delete mode 100755 react-native/prettier.ts delete mode 100644 react-native/react-native.ts create mode 100644 src/cli.ts create mode 100755 src/commands/eslint.ts create mode 100755 src/commands/prettier.ts create mode 100644 src/util/buildAction.ts diff --git a/bin/belt b/bin/belt index 4ac60a4..f96a09a 100755 --- a/bin/belt +++ b/bin/belt @@ -1,3 +1,3 @@ #!/usr/bin/env bash -yarn ts-node ./react-native/react-native.ts "$@" +yarn ts-node ./src/cli.ts "$@" diff --git a/index.ts b/index.ts deleted file mode 100755 index e67520d..0000000 --- a/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { program } from "commander"; - -program - .name("thoughtbelt") - .description( - "Perform project setup and redundant tasks without your pants falling down!" - ) - .command( - "react-native [options]", - "Perform a React Native task", - { executableFile: "react-native/react-native.ts" } - ) - .alias("rn") - .alias("expo") - .showHelpAfterError() - .parse(); diff --git a/react-native/eslint.ts b/react-native/eslint.ts deleted file mode 100755 index 22cd7ba..0000000 --- a/react-native/eslint.ts +++ /dev/null @@ -1 +0,0 @@ -console.log('ESLint!') diff --git a/react-native/prettier.ts b/react-native/prettier.ts deleted file mode 100755 index 828c5cf..0000000 --- a/react-native/prettier.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("Prettier!"); diff --git a/react-native/react-native.ts b/react-native/react-native.ts deleted file mode 100644 index d884c17..0000000 --- a/react-native/react-native.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { program } from "commander"; - -console.log("React Native 🎉"); - -program - .name("thoughtbelt") - .description( - "Perform React Native and Expo setup and redundant tasks without your pants falling down!" - ) - - .command("eslint") - .description("Configure ESLint") - .action(async () => await import("./eslint")) - - .command("prettier") - .description("Configure Prettier") - .action(() => import("./prettier")) - .showHelpAfterError() - .parse(); diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..ce238e1 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,25 @@ +import { program } from "commander"; +import buildAction from "./util/buildAction"; + +console.log("React Native 🎉"); + +export default function runCli() { + program + .name("thoughtbelt") + .description( + "Perform React Native and Expo setup and redundant tasks without your pants falling down!" + ) + + .command("eslint") + .description("Configure ESLint") + .action(buildAction(import("./commands/eslint"))); + + program + .command("prettier") + .description("Configure Prettier") + .action(buildAction(import("./commands/prettier"))); + + program.showHelpAfterError().parse(); +} + +runCli(); diff --git a/src/commands/eslint.ts b/src/commands/eslint.ts new file mode 100755 index 0000000..5f8c481 --- /dev/null +++ b/src/commands/eslint.ts @@ -0,0 +1,3 @@ +export default function runEslint() { + console.log("ESLint!"); +} diff --git a/src/commands/prettier.ts b/src/commands/prettier.ts new file mode 100755 index 0000000..5d3bb16 --- /dev/null +++ b/src/commands/prettier.ts @@ -0,0 +1,3 @@ +export default function runPrettier() { + console.log("Prettier!"); +} diff --git a/src/util/buildAction.ts b/src/util/buildAction.ts new file mode 100644 index 0000000..4013fef --- /dev/null +++ b/src/util/buildAction.ts @@ -0,0 +1,17 @@ +type AsyncModule = Promise<{ + default: (...args: unknown[]) => void; +}>; + +/** + * builds the action function that is passed to Commander's + * program.action. + * Eg: program.action( + * buildAction(import('./commands/prettier)) + * ) + */ +export default function buildAction(asyncModule: AsyncModule) { + return async (...args: unknown[]) => { + const module = await asyncModule; + module.default(args); + }; +}