Skip to content

Commit 3820dd6

Browse files
committed
Add the hability to parse Glob and Folder path in modelPaths
Changed the usage of FS to GLOB. If the Path isn't a glob, it's a folder path and to work with glob, must be turned into a glob (by adding /* in the end). I think the check if it `hasMagic` should be an outside function, chained in a `map` before `arg.reduce`.
1 parent 8973931 commit 3820dd6

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/services/models.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'reflect-metadata';
2-
import * as fs from 'fs';
2+
import * as glob from 'glob';
33
import * as path from 'path';
44
import {DataTypeAbstract, DefineOptions} from 'sequelize';
55
import {Model} from "../models/Model";
@@ -182,14 +182,16 @@ export function getModels(arg: Array<typeof Model | string>): Array<typeof Model
182182

183183
return arg.reduce((models: any[], dir) => {
184184

185-
const _models = fs
186-
.readdirSync(dir as string)
185+
if (!glob.hasMagic(dir)) dir = path.join(dir, '/*');
186+
const _models = glob
187+
.sync(dir as string)
187188
.filter(isImportable)
188-
.map(getFilenameWithoutExtension)
189+
.map(getFullfilepathWithoutExtension)
189190
.filter(uniqueFilter)
190-
.map(fileName => {
191-
const fullPath = path.join(dir, fileName);
191+
.map(fullPath => {
192+
192193
const module = require(fullPath);
194+
const fileName = getFilenameWithoutExtension(fullPath);
193195

194196
if (!module[fileName] && !module.default) {
195197
throw new Error(`No default export defined for file "${fileName}" or ` +
@@ -313,3 +315,11 @@ function isImportable(file: string): boolean {
313315
function getFilenameWithoutExtension(file: string): string {
314316
return path.parse(file).name;
315317
}
318+
319+
/**
320+
* Return the value of the full path with filename, without extension
321+
*/
322+
function getFullfilepathWithoutExtension(file: string): string {
323+
const parsedFile = path.parse(file);
324+
return path.join(parsedFile.dir, parsedFile.name);
325+
}

0 commit comments

Comments
 (0)