Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
feat: log files loaded from glob patterns (typeorm#4346)
Browse files Browse the repository at this point in the history
This new feature logs the files that are loaded using the glob patterns to aid in debugging.

Closes: typeorm#4162
  • Loading branch information
aktraore authored and vlapo committed Jul 13, 2019
1 parent c8dbf09 commit e12479e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
16 changes: 16 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -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"
```
16 changes: 16 additions & 0 deletions docs/zh_CN/troubleshooting.md
Original file line number Diff line number Diff line change
@@ -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"
```
6 changes: 3 additions & 3 deletions src/connection/ConnectionMetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MigrationInterface>(migrationClass));
}

Expand All @@ -40,7 +40,7 @@ export class ConnectionMetadataBuilder {
*/
buildSubscribers(subscribers: (Function|string)[]): EntitySubscriberInterface<any>[] {
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<EntitySubscriberInterface<any>>(metadata.target));
Expand All @@ -56,7 +56,7 @@ export class ConnectionMetadataBuilder {
const entityClasses: Function[] = entityClassesOrSchemas.filter(entityClass => (entityClass instanceof EntitySchema) === false) as any;
const entitySchemas: EntitySchema<any>[] = 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);
Expand Down
4 changes: 2 additions & 2 deletions src/logger/AdvancedConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
12 changes: 10 additions & 2 deletions src/util/DirectoryExportedClassesLoader.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit e12479e

Please sign in to comment.