UNPKG

4.45 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 /**
54 * Run migrations
55 */
56 async runMakeMigration() {
57 if (!this.migration) {
58 return;
59 }
60 const makeMigration = await this.kernel.exec('make:migration', [this.name]);
61 this.exitCode = makeMigration.exitCode;
62 this.error = makeMigration.error;
63 }
64 /**
65 * Make controller
66 */
67 async runMakeController() {
68 if (!this.controller) {
69 return;
70 }
71 const makeController = await this.kernel.exec('make:controller', [this.name]);
72 this.exitCode = makeController.exitCode;
73 this.error = makeController.error;
74 }
75 /**
76 * Execute command
77 */
78 async run() {
79 const stub = (0, path_1.join)(__dirname, '..', 'templates', 'model.txt');
80 const path = this.application.resolveNamespaceDirectory('models');
81 this.generator
82 .addFile(this.name, { pattern: 'pascalcase', form: 'singular' })
83 .stub(stub)
84 .destinationDir(path || 'app/Models')
85 .useMustache()
86 .appRoot(this.application.cliCwd || this.application.appRoot);
87 await this.generator.run();
88 await this.runMakeMigration();
89 if (this.exitCode) {
90 return;
91 }
92 await this.runMakeController();
93 }
94}
95Object.defineProperty(MakeModel, "commandName", {
96 enumerable: true,
97 configurable: true,
98 writable: true,
99 value: 'make:model'
100});
101Object.defineProperty(MakeModel, "description", {
102 enumerable: true,
103 configurable: true,
104 writable: true,
105 value: 'Make a new Lucid model'
106});
107Object.defineProperty(MakeModel, "settings", {
108 enumerable: true,
109 configurable: true,
110 writable: true,
111 value: {
112 loadApp: true,
113 }
114});
115__decorate([
116 standalone_1.args.string({ description: 'Name of the model class' }),
117 __metadata("design:type", String)
118], MakeModel.prototype, "name", void 0);
119__decorate([
120 standalone_1.flags.boolean({
121 name: 'migration',
122 alias: 'm',
123 description: 'Generate the migration for the model',
124 }),
125 __metadata("design:type", Boolean)
126], MakeModel.prototype, "migration", void 0);
127__decorate([
128 standalone_1.flags.boolean({
129 name: 'controller',
130 alias: 'c',
131 description: 'Generate the controller for the model',
132 }),
133 __metadata("design:type", Boolean)
134], MakeModel.prototype, "controller", void 0);
135exports.default = MakeModel;