Skip to content

Commit

Permalink
Fix typescript issues, refactor a bit (#4)
Browse files Browse the repository at this point in the history
* Fix TypeScript issues

Commander can automatically import files for us by specifying
'executableFile' for commands. This unfortunately doesn't work well with
TypeScript, which was why we were using `node -r ts-node` to run the CLI
instead of `ts-node`. This setup to get commander working was also
causing other issues with file imports.

This commit updates so we now manually import files for commands, which
enables us to revert the less-conventional TypeScript config.

* 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

* Run ts-node with esm loader

This fixes the issue where we are unable to run packages
that only export ES Modules. It's possible we will still run into some
issues with this configuration since ESM support is still experimental
on the TypeScript side.

Relevant issue: TypeStrong/ts-node#1007
ESM loader docs: https://typestrong.org/ts-node/docs/imports/#native-ecmascript-modules
  • Loading branch information
stevehanson committed May 16, 2023
1 parent f7d9cc7 commit 0c40068
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion bin/belt
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

node -r ts-node/register ./react-native/react-native.ts "$@"
yarn ts-node ./src/cli.ts "$@"
16 changes: 0 additions & 16 deletions index.ts

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "Monorepo for project bootstrapping CLIs",
"main": "index.js",
"type": "module",
"scripts": {
"belt": "bin/belt",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
1 change: 0 additions & 1 deletion react-native/eslint.ts

This file was deleted.

1 change: 0 additions & 1 deletion react-native/prettier.ts

This file was deleted.

13 changes: 0 additions & 13 deletions react-native/react-native.ts

This file was deleted.

25 changes: 25 additions & 0 deletions 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();
3 changes: 3 additions & 0 deletions src/commands/eslint.ts
@@ -0,0 +1,3 @@
export default function runEslint() {
console.log("ESLint!");
}
3 changes: 3 additions & 0 deletions src/commands/prettier.ts
@@ -0,0 +1,3 @@
export default function runPrettier() {
console.log("Prettier!");
}
17 changes: 17 additions & 0 deletions 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);
};
}
13 changes: 7 additions & 6 deletions tsconfig.json
@@ -1,17 +1,18 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"lib": ["es2019"],
"target": "es2019",
"module": "commonjs",
"strict": true,
"module": "ES2020",
"moduleResolution": "node",
"noImplicitThis": true,
"noUnusedLocals": true,
"strict": true,
"sourceMap": true,
"outDir": "./build",
"skipLibCheck": true,
"types": ["node"]
},
"include": ["./**/*"],
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
},
"include": ["./**/*"]
}

0 comments on commit 0c40068

Please sign in to comment.