|
1 | 1 | import 'reflect-metadata';
|
2 |
| -import * as fs from 'fs'; |
| 2 | +import * as glob from 'glob'; |
3 | 3 | import * as path from 'path';
|
4 | 4 | import {DataTypeAbstract, DefineOptions} from 'sequelize';
|
5 | 5 | import {Model} from "../models/Model";
|
@@ -182,14 +182,16 @@ export function getModels(arg: Array<typeof Model | string>): Array<typeof Model
|
182 | 182 |
|
183 | 183 | return arg.reduce((models: any[], dir) => {
|
184 | 184 |
|
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) |
187 | 188 | .filter(isImportable)
|
188 |
| - .map(getFilenameWithoutExtension) |
| 189 | + .map(getFullfilepathWithoutExtension) |
189 | 190 | .filter(uniqueFilter)
|
190 |
| - .map(fileName => { |
191 |
| - const fullPath = path.join(dir, fileName); |
| 191 | + .map(fullPath => { |
| 192 | + |
192 | 193 | const module = require(fullPath);
|
| 194 | + const fileName = getFilenameWithoutExtension(fullPath); |
193 | 195 |
|
194 | 196 | if (!module[fileName] && !module.default) {
|
195 | 197 | throw new Error(`No default export defined for file "${fileName}" or ` +
|
@@ -313,3 +315,11 @@ function isImportable(file: string): boolean {
|
313 | 315 | function getFilenameWithoutExtension(file: string): string {
|
314 | 316 | return path.parse(file).name;
|
315 | 317 | }
|
| 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