Skip to content

Commit

Permalink
feat(cli): add tsx support
Browse files Browse the repository at this point in the history
  • Loading branch information
remidewitte authored and Shinigami92 committed Aug 28, 2024
1 parent 80c4a99 commit f33952c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
13 changes: 12 additions & 1 deletion bin/node-pg-migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ process.on('uncaughtException', (err) => {

const crossRequire = createRequire(resolve('_'));

/**
* Try to require a module and return null if it doesn't exist.
*
* @param moduleName The name of the module to require.
*/
function tryRequire<TModule = unknown>(moduleName: string): TModule | null {
try {
return crossRequire(moduleName);
Expand Down Expand Up @@ -256,7 +261,7 @@ let VERBOSE = argv[verboseArg];
let DECAMELIZE = argv[decamelizeArg];
let tsconfigPath = argv[tsconfigArg];

function readTsconfig() {
function readTsconfig(): void {
if (tsconfigPath) {
let tsconfig;
const json5 = tryRequire<typeof import('json5')>('json5');
Expand All @@ -283,6 +288,12 @@ function readTsconfig() {
console.error("Can't load tsconfig.json:", error);
}

const tsx = tryRequire<typeof import('tsx/dist/cjs/api/index.cjs')>('tsx');
if (tsx) {
process.env.TSX_TSCONFIG_PATH = tsconfigPath;
crossRequire('tsx/cjs');
}

const tsnode = tryRequire<typeof import('ts-node')>('ts-node');
if (!tsnode) {
console.error("For TypeScript support, please install 'ts-node' module");
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"rimraf": "6.0.1",
"ts-node": "10.9.2",
"tsup": "8.2.4",
"tsx": "4.19.0",
"typescript": "5.5.4",
"vitepress": "1.3.4",
"vitest": "2.0.5"
Expand Down
36 changes: 32 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f33952c

Please sign in to comment.