UNPKG

4.52 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/assembler
4 *
5 * (c) AdonisJS
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");
22/**
23 * Command to make a new Factory
24 */
25class MakeFactory extends standalone_1.BaseCommand {
26 constructor() {
27 super(...arguments);
28 /**
29 * Name of the model to be used in the factory
30 */
31 Object.defineProperty(this, "model", {
32 enumerable: true,
33 configurable: true,
34 writable: true,
35 value: void 0
36 });
37 /**
38 * Import path to the model used in the factory
39 */
40 Object.defineProperty(this, "modelPath", {
41 enumerable: true,
42 configurable: true,
43 writable: true,
44 value: void 0
45 });
46 Object.defineProperty(this, "exact", {
47 enumerable: true,
48 configurable: true,
49 writable: true,
50 value: void 0
51 });
52 }
53 /**
54 * Generate model import path used in the factory
55 */
56 generateModelImportPath() {
57 let base = this.application.rcFile.namespaces.models || 'App/Models';
58 if (!base.endsWith('/')) {
59 base += '/';
60 }
61 let importPath = this.model;
62 if (this.modelPath) {
63 importPath = this.modelPath;
64 }
65 else if (importPath.endsWith('Factory')) {
66 importPath = importPath.replace(/Factory$/, '');
67 }
68 if (importPath.startsWith(base)) {
69 return importPath;
70 }
71 return base + importPath;
72 }
73 /**
74 * Path to the factories directory
75 */
76 getDestinationPath() {
77 const base = this.application.rcFile.directories.database || 'database';
78 return (0, path_1.join)(base, 'factories');
79 }
80 /**
81 * Passed down to the stub template
82 */
83 templateData() {
84 return {
85 model: this.model,
86 modelImportPath: this.generateModelImportPath(),
87 toModelName: () => {
88 return function (model, render) {
89 return render(model).split('/').pop();
90 };
91 },
92 };
93 }
94 async run() {
95 const stub = (0, path_1.join)(__dirname, '..', 'templates', 'factory.txt');
96 this.generator
97 .addFile(this.model, { pattern: 'pascalcase', form: 'singular', suffix: 'Factory' })
98 .stub(stub)
99 .useMustache()
100 .destinationDir(this.getDestinationPath())
101 .appRoot(this.application.appRoot)
102 .apply(this.templateData());
103 await this.generator.run();
104 }
105}
106Object.defineProperty(MakeFactory, "commandName", {
107 enumerable: true,
108 configurable: true,
109 writable: true,
110 value: 'make:factory'
111});
112Object.defineProperty(MakeFactory, "description", {
113 enumerable: true,
114 configurable: true,
115 writable: true,
116 value: 'Make a new factory'
117});
118__decorate([
119 standalone_1.args.string({ description: 'The name of the model' }),
120 __metadata("design:type", String)
121], MakeFactory.prototype, "model", void 0);
122__decorate([
123 standalone_1.flags.string({ description: 'The path to the model' }),
124 __metadata("design:type", String)
125], MakeFactory.prototype, "modelPath", void 0);
126__decorate([
127 standalone_1.flags.boolean({
128 description: 'Create the factory with the exact name as provided',
129 alias: 'e',
130 }),
131 __metadata("design:type", Boolean)
132], MakeFactory.prototype, "exact", void 0);
133exports.default = MakeFactory;