1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getModels = exports.prepareArgs = exports.prepareOptions = void 0;
|
4 | const path_1 = require("path");
|
5 | const glob = require("glob");
|
6 | const array_1 = require("../../shared/array");
|
7 |
|
8 |
|
9 |
|
10 | function prepareOptions(options) {
|
11 | if (options.validateOnly) {
|
12 | return getValidationOnlyOptions(options);
|
13 | }
|
14 | return Object.assign({}, options);
|
15 | }
|
16 | exports.prepareOptions = prepareOptions;
|
17 | function prepareArgs(...args) {
|
18 | const lastArg = args[args.length - 1];
|
19 | const options = lastArg && typeof lastArg === 'object' ? prepareOptions(lastArg) : undefined;
|
20 | if (options) {
|
21 | args[args.length - 1] = options;
|
22 | }
|
23 | return { preparedArgs: args, options };
|
24 | }
|
25 | exports.prepareArgs = prepareArgs;
|
26 | function getValidationOnlyOptions(options) {
|
27 | return Object.assign(Object.assign({}, options), { dialect: 'sqlite', dialectModulePath: __dirname + '/../validation-only/db-dialect-dummy' });
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | function getModels(arg, modelMatch) {
|
33 | const hasSupportedExtension = (path) => ['.ts', '.js'].indexOf((0, path_1.extname)(path)) !== -1;
|
34 | if (arg && typeof arg[0] === 'string') {
|
35 | return arg.reduce((models, dir) => {
|
36 | if (!glob.hasMagic(dir) && !hasSupportedExtension(dir))
|
37 | dir = (0, path_1.join)(dir, '/*');
|
38 | const _models = glob
|
39 | .sync(dir)
|
40 | .filter(isImportable)
|
41 | .map(getFullfilepathWithoutExtension)
|
42 | .filter(array_1.uniqueFilter)
|
43 | .map((fullPath) => {
|
44 |
|
45 | const module = require(fullPath);
|
46 | const fileName = (0, path_1.basename)(fullPath);
|
47 | const matchedMemberKey = Object.keys(module).find((m) => modelMatch(fileName, m));
|
48 | const matchedMember = matchedMemberKey ? module[matchedMemberKey] : undefined;
|
49 | if (!matchedMember && !module.default) {
|
50 | throw new Error(`No default export defined for file "${fileName}" or ` +
|
51 | `export does not satisfy filename.`);
|
52 | }
|
53 | return matchedMember || module.default;
|
54 | });
|
55 | models.push(..._models);
|
56 | return models;
|
57 | }, []);
|
58 | }
|
59 | return arg;
|
60 | }
|
61 | exports.getModels = getModels;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function isImportable(file) {
|
67 | const filePart = file.slice(-3);
|
68 | return filePart === '.js' || (filePart === '.ts' && file.slice(-5) !== '.d.ts');
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | function getFullfilepathWithoutExtension(file) {
|
74 | const parsedFile = (0, path_1.parse)(file);
|
75 | return (0, path_1.join)(parsedFile.dir, parsedFile.name);
|
76 | }
|
77 |
|
\ | No newline at end of file |