From e12479ed43c4c5361de8d4b9c55120e2aa9ccb22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdoulaye=20K=2E=20Traor=C3=A9?= Date: Sat, 13 Jul 2019 18:57:16 +0200 Subject: [PATCH] feat: log files loaded from glob patterns (#4346) This new feature logs the files that are loaded using the glob patterns to aid in debugging. Closes: #4162 --- docs/troubleshooting.md | 16 ++++++++++++++++ docs/zh_CN/troubleshooting.md | 16 ++++++++++++++++ src/connection/ConnectionMetadataBuilder.ts | 6 +++--- src/logger/AdvancedConsoleLogger.ts | 4 ++-- src/util/DirectoryExportedClassesLoader.ts | 12 ++++++++++-- 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 docs/troubleshooting.md create mode 100644 docs/zh_CN/troubleshooting.md diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md new file mode 100644 index 0000000000..4baf4d8921 --- /dev/null +++ b/docs/troubleshooting.md @@ -0,0 +1,16 @@ +# Troubleshooting + +* [Glob patterns](#glob-patterns) + +## Glob Patterns + +Glob patterns are used in the TypeOrm to specify the locations of entities, migrations, subscriber and other information. Errors in the patterns can lead to the common `RepositoryNotFoundError` and familiar errors. In order to check if any files were loaded by TypeOrm using the glob patterns, all you need to do is set the logging level to `info` such as explained in the [Logging](./logging.md) section of the documentation. This will allow you to have logs in the console that may look like this: + +```bash +# in case of an error + INFO: No classes were found using the provided glob pattern: "dist/**/*.entity{.ts}" +``` +```bash +# when files are found +INFO: All classes found using provided glob pattern "dist/**/*.entity{.js,.ts}" : "dist/app/user/user.entity.js | dist/app/common/common.entity.js" +``` \ No newline at end of file diff --git a/docs/zh_CN/troubleshooting.md b/docs/zh_CN/troubleshooting.md new file mode 100644 index 0000000000..eda40222aa --- /dev/null +++ b/docs/zh_CN/troubleshooting.md @@ -0,0 +1,16 @@ +# 故障排除 + +* [全球模式](#全球模式) + +## 全球模式 + +在类型中使用全局模式来指定实体,迁移,订户和其他信息的位置。模式中的错误可能导致常见的`RepositoryNotFoundError`和熟悉的错误。为了检查TypeOrm是否使用glob模式加载了任何文件,您需要做的就是将日志级别设置为`info`,如文档的[Logging](./logging.md)部分所述。 这将允许您拥有可能如下所示的日志: + +```bash +# 如果出错 + INFO: No classes were found using the provided glob pattern: "dist/**/*.entity{.ts}" +``` +```bash +# 何时找到文件 +INFO: All classes found using provided glob pattern "dist/**/*.entity{.js,.ts}" : "dist/app/user/user.entity.js | dist/app/common/common.entity.js" +``` \ No newline at end of file diff --git a/src/connection/ConnectionMetadataBuilder.ts b/src/connection/ConnectionMetadataBuilder.ts index d67577365d..e8c77ba61b 100644 --- a/src/connection/ConnectionMetadataBuilder.ts +++ b/src/connection/ConnectionMetadataBuilder.ts @@ -31,7 +31,7 @@ export class ConnectionMetadataBuilder { */ buildMigrations(migrations: (Function|string)[]): MigrationInterface[] { const [migrationClasses, migrationDirectories] = OrmUtils.splitClassesAndStrings(migrations); - const allMigrationClasses = [...migrationClasses, ...importClassesFromDirectories(migrationDirectories)]; + const allMigrationClasses = [...migrationClasses, ...importClassesFromDirectories(this.connection.logger, migrationDirectories)]; return allMigrationClasses.map(migrationClass => getFromContainer(migrationClass)); } @@ -40,7 +40,7 @@ export class ConnectionMetadataBuilder { */ buildSubscribers(subscribers: (Function|string)[]): EntitySubscriberInterface[] { const [subscriberClasses, subscriberDirectories] = OrmUtils.splitClassesAndStrings(subscribers || []); - const allSubscriberClasses = [...subscriberClasses, ...importClassesFromDirectories(subscriberDirectories)]; + const allSubscriberClasses = [...subscriberClasses, ...importClassesFromDirectories(this.connection.logger, subscriberDirectories)]; return getMetadataArgsStorage() .filterSubscribers(allSubscriberClasses) .map(metadata => getFromContainer>(metadata.target)); @@ -56,7 +56,7 @@ export class ConnectionMetadataBuilder { const entityClasses: Function[] = entityClassesOrSchemas.filter(entityClass => (entityClass instanceof EntitySchema) === false) as any; const entitySchemas: EntitySchema[] = entityClassesOrSchemas.filter(entityClass => entityClass instanceof EntitySchema) as any; - const allEntityClasses = [...entityClasses, ...importClassesFromDirectories(entityDirectories)]; + const allEntityClasses = [...entityClasses, ...importClassesFromDirectories(this.connection.logger, entityDirectories)]; allEntityClasses.forEach(entityClass => { // if we have entity schemas loaded from directories if (entityClass instanceof EntitySchema) { entitySchemas.push(entityClass); diff --git a/src/logger/AdvancedConsoleLogger.ts b/src/logger/AdvancedConsoleLogger.ts index 912d2b9da4..a974d633ef 100644 --- a/src/logger/AdvancedConsoleLogger.ts +++ b/src/logger/AdvancedConsoleLogger.ts @@ -74,11 +74,11 @@ export class AdvancedConsoleLogger implements Logger { switch (level) { case "log": if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("log") !== -1)) - console.log(message); + PlatformTools.log(message); break; case "info": if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("info") !== -1)) - console.info(message); + PlatformTools.logInfo("INFO:", message); break; case "warn": if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("warn") !== -1)) diff --git a/src/util/DirectoryExportedClassesLoader.ts b/src/util/DirectoryExportedClassesLoader.ts index 5d9c3056c2..9aea39c571 100644 --- a/src/util/DirectoryExportedClassesLoader.ts +++ b/src/util/DirectoryExportedClassesLoader.ts @@ -1,11 +1,14 @@ import {PlatformTools} from "../platform/PlatformTools"; import {EntitySchema} from "../index"; - +import {Logger} from "../logger/Logger"; /** * Loads all exported classes from the given directory. */ -export function importClassesFromDirectories(directories: string[], formats = [".js", ".ts"]): Function[] { +export function importClassesFromDirectories(logger: Logger, directories: string[], formats = [".js", ".ts"]): Function[] { + const logLevel = "info"; + const classesNotFoundMessage = "No classes were found using the provided glob pattern: "; + const classesFoundMessage = "All classes found using provided glob pattern"; function loadFileClasses(exported: any, allLoaded: Function[]) { if (typeof exported === "function" || exported instanceof EntitySchema) { allLoaded.push(exported); @@ -24,6 +27,11 @@ export function importClassesFromDirectories(directories: string[], formats = [" return allDirs.concat(PlatformTools.load("glob").sync(PlatformTools.pathNormalize(dir))); }, [] as string[]); + if (directories.length > 0 && allFiles.length === 0) { + logger.log(logLevel, `${classesNotFoundMessage} "${directories}"`); + } else if (allFiles.length > 0) { + logger.log(logLevel, `${classesFoundMessage} "${directories}" : "${allFiles}"`); + } const dirs = allFiles .filter(file => { const dtsExtension = file.substring(file.length - 5, file.length);