From 97699e816e03867efe595f620ceb152af91f9f56 Mon Sep 17 00:00:00 2001 From: Gilad S Date: Tue, 22 Mar 2022 05:04:47 +0200 Subject: [PATCH] fix: broken CLI in ESM projects since version 0.3 (#8773) * fix: broken CLI in ESM projects * style: formatting --- src/commands/CacheClearCommand.ts | 2 +- src/commands/CommandUtils.ts | 9 +++++++-- src/commands/MigrationGenerateCommand.ts | 2 +- src/commands/MigrationRevertCommand.ts | 2 +- src/commands/MigrationRunCommand.ts | 2 +- src/commands/MigrationShowCommand.ts | 2 +- src/commands/QueryCommand.ts | 2 +- src/commands/SchemaDropCommand.ts | 2 +- src/commands/SchemaLogCommand.ts | 2 +- src/commands/SchemaSyncCommand.ts | 2 +- 10 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/commands/CacheClearCommand.ts b/src/commands/CacheClearCommand.ts index 10c34bbe56..5ad0653ec8 100644 --- a/src/commands/CacheClearCommand.ts +++ b/src/commands/CacheClearCommand.ts @@ -25,7 +25,7 @@ export class CacheClearCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/CommandUtils.ts b/src/commands/CommandUtils.ts index d0cd75d71d..b81aa33d0e 100644 --- a/src/commands/CommandUtils.ts +++ b/src/commands/CommandUtils.ts @@ -4,15 +4,20 @@ import mkdirp from "mkdirp" import { TypeORMError } from "../error" import { DataSource } from "../data-source" import { InstanceChecker } from "../util/InstanceChecker" +import { importOrRequireFile } from "../util/ImportUtils" /** * Command line utils functions. */ export class CommandUtils { - static loadDataSource(dataSourceFilePath: string): DataSource { + static async loadDataSource( + dataSourceFilePath: string, + ): Promise { let dataSourceFileExports try { - dataSourceFileExports = require(dataSourceFilePath) + ;[dataSourceFileExports] = await importOrRequireFile( + dataSourceFilePath, + ) } catch (err) { throw new Error( `Invalid file path given: "${dataSourceFilePath}". File must contain a TypeScript / JavaScript code and export a DataSource instance.`, diff --git a/src/commands/MigrationGenerateCommand.ts b/src/commands/MigrationGenerateCommand.ts index 3b08de2f19..93e658749d 100644 --- a/src/commands/MigrationGenerateCommand.ts +++ b/src/commands/MigrationGenerateCommand.ts @@ -70,7 +70,7 @@ export class MigrationGenerateCommand implements yargs.CommandModule { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/MigrationRevertCommand.ts b/src/commands/MigrationRevertCommand.ts index bf601f5563..75a269bd54 100644 --- a/src/commands/MigrationRevertCommand.ts +++ b/src/commands/MigrationRevertCommand.ts @@ -31,7 +31,7 @@ export class MigrationRevertCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/MigrationRunCommand.ts b/src/commands/MigrationRunCommand.ts index b4ea6bbd1d..d3b18f2bbd 100644 --- a/src/commands/MigrationRunCommand.ts +++ b/src/commands/MigrationRunCommand.ts @@ -31,7 +31,7 @@ export class MigrationRunCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/MigrationShowCommand.ts b/src/commands/MigrationShowCommand.ts index 2f665ba513..6a96661c0d 100644 --- a/src/commands/MigrationShowCommand.ts +++ b/src/commands/MigrationShowCommand.ts @@ -24,7 +24,7 @@ export class MigrationShowCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/QueryCommand.ts b/src/commands/QueryCommand.ts index 250514600d..04b5e376c0 100644 --- a/src/commands/QueryCommand.ts +++ b/src/commands/QueryCommand.ts @@ -33,7 +33,7 @@ export class QueryCommand implements yargs.CommandModule { let queryRunner: QueryRunner | undefined = undefined let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/SchemaDropCommand.ts b/src/commands/SchemaDropCommand.ts index f82ba86cbd..901777a0ee 100644 --- a/src/commands/SchemaDropCommand.ts +++ b/src/commands/SchemaDropCommand.ts @@ -27,7 +27,7 @@ export class SchemaDropCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/SchemaLogCommand.ts b/src/commands/SchemaLogCommand.ts index 9873d5497c..6401d6fa08 100644 --- a/src/commands/SchemaLogCommand.ts +++ b/src/commands/SchemaLogCommand.ts @@ -28,7 +28,7 @@ export class SchemaLogCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({ diff --git a/src/commands/SchemaSyncCommand.ts b/src/commands/SchemaSyncCommand.ts index 6a34f1598d..81b040c92b 100644 --- a/src/commands/SchemaSyncCommand.ts +++ b/src/commands/SchemaSyncCommand.ts @@ -27,7 +27,7 @@ export class SchemaSyncCommand implements yargs.CommandModule { async handler(args: yargs.Arguments) { let dataSource: DataSource | undefined = undefined try { - dataSource = CommandUtils.loadDataSource( + dataSource = await CommandUtils.loadDataSource( path.resolve(process.cwd(), args.dataSource as string), ) dataSource.setOptions({