1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
11 | if (k2 === undefined) k2 = k;
|
12 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
13 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
14 | desc = { enumerable: true, get: function() { return m[k]; } };
|
15 | }
|
16 | Object.defineProperty(o, k2, desc);
|
17 | }) : (function(o, m, k, k2) {
|
18 | if (k2 === undefined) k2 = k;
|
19 | o[k2] = m[k];
|
20 | }));
|
21 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
22 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
23 | }) : function(o, v) {
|
24 | o["default"] = v;
|
25 | });
|
26 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
27 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
28 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
29 | else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
30 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
31 | };
|
32 | var __importStar = (this && this.__importStar) || function (mod) {
|
33 | if (mod && mod.__esModule) return mod;
|
34 | var result = {};
|
35 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
36 | __setModuleDefault(result, mod);
|
37 | return result;
|
38 | };
|
39 | var __metadata = (this && this.__metadata) || function (k, v) {
|
40 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
41 | };
|
42 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
43 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
44 | };
|
45 | Object.defineProperty(exports, "__esModule", { value: true });
|
46 | const slash_1 = __importDefault(require("slash"));
|
47 | const path_1 = require("path");
|
48 | const standalone_1 = require("@adonisjs/core/build/standalone");
|
49 | class DbSeed extends standalone_1.BaseCommand {
|
50 | constructor() {
|
51 | super(...arguments);
|
52 | Object.defineProperty(this, "seeder", {
|
53 | enumerable: true,
|
54 | configurable: true,
|
55 | writable: true,
|
56 | value: void 0
|
57 | });
|
58 | |
59 |
|
60 |
|
61 | Object.defineProperty(this, "hasError", {
|
62 | enumerable: true,
|
63 | configurable: true,
|
64 | writable: true,
|
65 | value: false
|
66 | });
|
67 | |
68 |
|
69 |
|
70 |
|
71 | Object.defineProperty(this, "connection", {
|
72 | enumerable: true,
|
73 | configurable: true,
|
74 | writable: true,
|
75 | value: void 0
|
76 | });
|
77 | |
78 |
|
79 |
|
80 | Object.defineProperty(this, "interactive", {
|
81 | enumerable: true,
|
82 | configurable: true,
|
83 | writable: true,
|
84 | value: void 0
|
85 | });
|
86 | |
87 |
|
88 |
|
89 |
|
90 | Object.defineProperty(this, "files", {
|
91 | enumerable: true,
|
92 | configurable: true,
|
93 | writable: true,
|
94 | value: []
|
95 | });
|
96 | }
|
97 | |
98 |
|
99 |
|
100 | printLogMessage(file) {
|
101 | const colors = this['colors'];
|
102 | let color = 'gray';
|
103 | let message = '';
|
104 | let prefix = '';
|
105 | switch (file.status) {
|
106 | case 'pending':
|
107 | message = 'pending ';
|
108 | color = 'gray';
|
109 | break;
|
110 | case 'failed':
|
111 | message = 'error ';
|
112 | prefix = file.error.message;
|
113 | color = 'red';
|
114 | break;
|
115 | case 'ignored':
|
116 | message = 'ignored ';
|
117 | prefix = 'Enabled only in development environment';
|
118 | color = 'dim';
|
119 | break;
|
120 | case 'completed':
|
121 | message = 'completed';
|
122 | color = 'green';
|
123 | break;
|
124 | }
|
125 | console.log(`${colors[color]('❯')} ${colors[color](message)} ${file.file.name}`);
|
126 | if (prefix) {
|
127 | console.log(` ${colors[color](prefix)}`);
|
128 | }
|
129 | }
|
130 | |
131 |
|
132 |
|
133 | printNotAValidConnection(connection) {
|
134 | this.logger.error(`"${connection}" is not a valid connection name. Double check "config/database" file`);
|
135 | }
|
136 | |
137 |
|
138 |
|
139 | printNotAValidFile(fileName) {
|
140 | this.printLogMessage({
|
141 | file: {
|
142 | name: fileName,
|
143 | absPath: fileName,
|
144 | getSource: () => { },
|
145 | },
|
146 | status: 'failed',
|
147 | error: new Error('Invalid file path. Pass relative path from the application root'),
|
148 | });
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 | async getCherryPickedFiles(seedersFiles) {
|
155 | if (this.files.length) {
|
156 | return this.files.map((file) => {
|
157 | const fileExt = (0, path_1.extname)(file);
|
158 | return (fileExt ? file.replace(fileExt, '') : file).replace(/^\.\/|^\.\\\\/, '');
|
159 | });
|
160 | }
|
161 | else if (this.interactive) {
|
162 | return await this.prompt.multiple('Select files to run', seedersFiles.map((file) => {
|
163 | return { name: file.name };
|
164 | }));
|
165 | }
|
166 | return seedersFiles.map((file) => file.name);
|
167 | }
|
168 | |
169 |
|
170 |
|
171 | async instantiateSeeder() {
|
172 | const db = this.application.container.use('Adonis/Lucid/Database');
|
173 | const { SeedsRunner } = await Promise.resolve().then(() => __importStar(require('../src/SeedsRunner')));
|
174 | this.seeder = new SeedsRunner(db, this.application, this.connection);
|
175 | }
|
176 | |
177 |
|
178 |
|
179 |
|
180 | async runAsSubCommand() {
|
181 | const db = this.application.container.use('Adonis/Lucid/Database');
|
182 | this.connection = this.connection || db.primaryConnectionName;
|
183 | |
184 |
|
185 |
|
186 | if (!db.manager.has(this.connection)) {
|
187 | this.printNotAValidConnection(this.connection);
|
188 | this.exitCode = 1;
|
189 | return;
|
190 | }
|
191 | |
192 |
|
193 |
|
194 | if (this.files && this.interactive) {
|
195 | this.logger.warning('Cannot use "--interactive" and "--files" together. Ignoring "--interactive"');
|
196 | }
|
197 | await this.instantiateSeeder();
|
198 | const files = await this.seeder.getList();
|
199 | const cherryPickedFiles = await this.getCherryPickedFiles(files);
|
200 | |
201 |
|
202 |
|
203 | for (let fileName of cherryPickedFiles) {
|
204 | const sourceFile = files.find(({ name }) => (0, slash_1.default)(fileName) === (0, slash_1.default)(name));
|
205 | if (!sourceFile) {
|
206 | this.printNotAValidFile(fileName);
|
207 | this.hasError = true;
|
208 | }
|
209 | else {
|
210 | const response = await this.seeder.run(sourceFile);
|
211 | if (response.status === 'failed') {
|
212 | this.hasError = true;
|
213 | }
|
214 | this.printLogMessage(response);
|
215 | }
|
216 | }
|
217 | this.exitCode = this.hasError ? 1 : 0;
|
218 | }
|
219 | |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | async runAsMain() {
|
226 | await this.runAsSubCommand();
|
227 | }
|
228 | |
229 |
|
230 |
|
231 | async run() {
|
232 | if (this.isMain) {
|
233 | await this.runAsMain();
|
234 | }
|
235 | else {
|
236 | await this.runAsSubCommand();
|
237 | }
|
238 | }
|
239 | |
240 |
|
241 |
|
242 |
|
243 | async completed() {
|
244 | if (this.seeder && this.isMain) {
|
245 | await this.seeder.close();
|
246 | }
|
247 | }
|
248 | }
|
249 | Object.defineProperty(DbSeed, "commandName", {
|
250 | enumerable: true,
|
251 | configurable: true,
|
252 | writable: true,
|
253 | value: 'db:seed'
|
254 | });
|
255 | Object.defineProperty(DbSeed, "description", {
|
256 | enumerable: true,
|
257 | configurable: true,
|
258 | writable: true,
|
259 | value: 'Execute database seeders'
|
260 | });
|
261 | Object.defineProperty(DbSeed, "settings", {
|
262 | enumerable: true,
|
263 | configurable: true,
|
264 | writable: true,
|
265 | value: {
|
266 | loadApp: true,
|
267 | }
|
268 | });
|
269 | __decorate([
|
270 | standalone_1.flags.string({ description: 'Define a custom database connection for the seeders', alias: 'c' }),
|
271 | __metadata("design:type", String)
|
272 | ], DbSeed.prototype, "connection", void 0);
|
273 | __decorate([
|
274 | standalone_1.flags.boolean({ description: 'Run seeders in interactive mode', alias: 'i' }),
|
275 | __metadata("design:type", Boolean)
|
276 | ], DbSeed.prototype, "interactive", void 0);
|
277 | __decorate([
|
278 | standalone_1.flags.array({ description: 'Define a custom set of seeders files names to run', alias: 'f' }),
|
279 | __metadata("design:type", Array)
|
280 | ], DbSeed.prototype, "files", void 0);
|
281 | exports.default = DbSeed;
|