UNPKG

2.09 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/lucid
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.SeedersSource = void 0;
12const utils_1 = require("../utils");
13/**
14 * Seeders source exposes the API to read the seeders from disk for a given connection.
15 */
16class SeedersSource {
17 constructor(config, app) {
18 Object.defineProperty(this, "config", {
19 enumerable: true,
20 configurable: true,
21 writable: true,
22 value: config
23 });
24 Object.defineProperty(this, "app", {
25 enumerable: true,
26 configurable: true,
27 writable: true,
28 value: app
29 });
30 }
31 /**
32 * Returns an array of files inside a given directory. Relative
33 * paths are resolved from the project root
34 */
35 async getDirectoryFiles(directoryPath) {
36 const { files } = await (0, utils_1.sourceFiles)(this.app.appRoot, directoryPath);
37 return files;
38 }
39 /**
40 * Returns an array of seeders paths for a given connection. If paths
41 * are not defined, then `database/seeders` fallback is used
42 */
43 getSeedersPaths() {
44 const directories = (this.config.seeders || {}).paths;
45 const defaultDirectory = this.app.directoriesMap.get('seeds') || 'database/seeders';
46 return directories && directories.length ? directories : [`./${defaultDirectory}`];
47 }
48 /**
49 * Returns an array of files for the defined seed directories
50 */
51 async getSeeders() {
52 const seedersPaths = this.getSeedersPaths();
53 const directories = await Promise.all(seedersPaths.map((directoryPath) => {
54 return this.getDirectoryFiles(directoryPath);
55 }));
56 return directories.reduce((result, directory) => {
57 result = result.concat(directory);
58 return result;
59 }, []);
60 }
61}
62exports.SeedersSource = SeedersSource;