UNPKG

5.29 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 */
10var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
11 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
13 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;
14 return c > 3 && r && Object.defineProperty(target, key, r), r;
15};
16var __metadata = (this && this.__metadata) || function (k, v) {
17 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
18};
19Object.defineProperty(exports, "__esModule", { value: true });
20const path_1 = require("path");
21const standalone_1 = require("@adonisjs/core/build/standalone");
22class MakeModel extends standalone_1.BaseCommand {
23 constructor() {
24 super(...arguments);
25 /**
26 * The name of the model file.
27 */
28 Object.defineProperty(this, "name", {
29 enumerable: true,
30 configurable: true,
31 writable: true,
32 value: void 0
33 });
34 /**
35 * Defines if we generate the migration for the model.
36 */
37 Object.defineProperty(this, "migration", {
38 enumerable: true,
39 configurable: true,
40 writable: true,
41 value: void 0
42 });
43 /**
44 * Defines if we generate the controller for the model.
45 */
46 Object.defineProperty(this, "controller", {
47 enumerable: true,
48 configurable: true,
49 writable: true,
50 value: void 0
51 });
52 /**
53 * Defines if we generate the factory for the model.
54 */
55 Object.defineProperty(this, "factory", {
56 enumerable: true,
57 configurable: true,
58 writable: true,
59 value: void 0
60 });
61 }
62 /**
63 * Run migrations
64 */
65 async runMakeMigration() {
66 if (!this.migration) {
67 return;
68 }
69 const makeMigration = await this.kernel.exec('make:migration', [this.name]);
70 this.exitCode = makeMigration.exitCode;
71 this.error = makeMigration.error;
72 }
73 /**
74 * Make controller
75 */
76 async runMakeController() {
77 if (!this.controller) {
78 return;
79 }
80 const makeController = await this.kernel.exec('make:controller', [this.name]);
81 this.exitCode = makeController.exitCode;
82 this.error = makeController.error;
83 }
84 /**
85 * Make factory
86 */
87 async runMakeFactory() {
88 if (!this.factory) {
89 return;
90 }
91 const makeFactory = await this.kernel.exec('make:factory', [this.name]);
92 this.exitCode = makeFactory.exitCode;
93 this.error = makeFactory.error;
94 }
95 /**
96 * Execute command
97 */
98 async run() {
99 const stub = (0, path_1.join)(__dirname, '..', 'templates', 'model.txt');
100 const path = this.application.resolveNamespaceDirectory('models');
101 this.generator
102 .addFile(this.name, { pattern: 'pascalcase', form: 'singular' })
103 .stub(stub)
104 .destinationDir(path || 'app/Models')
105 .useMustache()
106 .appRoot(this.application.cliCwd || this.application.appRoot);
107 await this.generator.run();
108 await this.runMakeMigration();
109 if (this.exitCode) {
110 return;
111 }
112 await this.runMakeController();
113 await this.runMakeFactory();
114 }
115}
116Object.defineProperty(MakeModel, "commandName", {
117 enumerable: true,
118 configurable: true,
119 writable: true,
120 value: 'make:model'
121});
122Object.defineProperty(MakeModel, "description", {
123 enumerable: true,
124 configurable: true,
125 writable: true,
126 value: 'Make a new Lucid model'
127});
128Object.defineProperty(MakeModel, "settings", {
129 enumerable: true,
130 configurable: true,
131 writable: true,
132 value: {
133 loadApp: true,
134 }
135});
136__decorate([
137 standalone_1.args.string({ description: 'Name of the model class' }),
138 __metadata("design:type", String)
139], MakeModel.prototype, "name", void 0);
140__decorate([
141 standalone_1.flags.boolean({
142 name: 'migration',
143 alias: 'm',
144 description: 'Generate the migration for the model',
145 }),
146 __metadata("design:type", Boolean)
147], MakeModel.prototype, "migration", void 0);
148__decorate([
149 standalone_1.flags.boolean({
150 name: 'controller',
151 alias: 'c',
152 description: 'Generate the controller for the model',
153 }),
154 __metadata("design:type", Boolean)
155], MakeModel.prototype, "controller", void 0);
156__decorate([
157 standalone_1.flags.boolean({
158 name: 'factory',
159 alias: 'f',
160 description: 'Generate a factory for the model',
161 }),
162 __metadata("design:type", Boolean)
163], MakeModel.prototype, "factory", void 0);
164exports.default = MakeModel;