1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var ConnectionOptionsReader_1 = require("../connection/ConnectionOptionsReader");
|
5 | var CommandUtils_1 = require("./CommandUtils");
|
6 | var chalk = require("chalk");
|
7 |
|
8 |
|
9 |
|
10 | var EntityCreateCommand = (function () {
|
11 | function EntityCreateCommand() {
|
12 | this.command = "entity:create";
|
13 | this.describe = "Generates a new entity.";
|
14 | }
|
15 | EntityCreateCommand.prototype.builder = function (args) {
|
16 | return args
|
17 | .option("c", {
|
18 | alias: "connection",
|
19 | default: "default",
|
20 | describe: "Name of the connection on which to run a query"
|
21 | })
|
22 | .option("n", {
|
23 | alias: "name",
|
24 | describe: "Name of the entity class.",
|
25 | demand: true
|
26 | })
|
27 | .option("d", {
|
28 | alias: "dir",
|
29 | describe: "Directory where entity should be created."
|
30 | })
|
31 | .option("f", {
|
32 | alias: "config",
|
33 | default: "ormconfig",
|
34 | describe: "Name of the file with connection configuration."
|
35 | });
|
36 | };
|
37 | EntityCreateCommand.prototype.handler = function (args) {
|
38 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
39 | var fileContent, filename, directory, connectionOptionsReader, connectionOptions, err_1, path, fileExists, err_2;
|
40 | return tslib_1.__generator(this, function (_a) {
|
41 | switch (_a.label) {
|
42 | case 0:
|
43 | _a.trys.push([0, 7, , 8]);
|
44 | fileContent = EntityCreateCommand.getTemplate(args.name);
|
45 | filename = args.name + ".ts";
|
46 | directory = args.dir;
|
47 | if (!!directory) return [3 , 4];
|
48 | _a.label = 1;
|
49 | case 1:
|
50 | _a.trys.push([1, 3, , 4]);
|
51 | connectionOptionsReader = new ConnectionOptionsReader_1.ConnectionOptionsReader({
|
52 | root: process.cwd(),
|
53 | configName: args.config
|
54 | });
|
55 | return [4 , connectionOptionsReader.get(args.connection)];
|
56 | case 2:
|
57 | connectionOptions = _a.sent();
|
58 | directory = connectionOptions.cli ? connectionOptions.cli.entitiesDir : undefined;
|
59 | return [3 , 4];
|
60 | case 3:
|
61 | err_1 = _a.sent();
|
62 | return [3 , 4];
|
63 | case 4:
|
64 | path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
|
65 | return [4 , CommandUtils_1.CommandUtils.fileExists(path)];
|
66 | case 5:
|
67 | fileExists = _a.sent();
|
68 | if (fileExists) {
|
69 | throw "File " + chalk.blue(path) + " already exists";
|
70 | }
|
71 | return [4 , CommandUtils_1.CommandUtils.createFile(path, fileContent)];
|
72 | case 6:
|
73 | _a.sent();
|
74 | console.log(chalk.green("Entity " + chalk.blue(path) + " has been created successfully."));
|
75 | return [3 , 8];
|
76 | case 7:
|
77 | err_2 = _a.sent();
|
78 | console.log(chalk.black.bgRed("Error during entity creation:"));
|
79 | console.error(err_2);
|
80 | process.exit(1);
|
81 | return [3 , 8];
|
82 | case 8: return [2 ];
|
83 | }
|
84 | });
|
85 | });
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 | |
91 |
|
92 |
|
93 | EntityCreateCommand.getTemplate = function (name) {
|
94 | return "import {Entity} from \"typeorm\";\n\n@Entity()\nexport class " + name + " {\n\n}\n";
|
95 | };
|
96 | return EntityCreateCommand;
|
97 | }());
|
98 | exports.EntityCreateCommand = EntityCreateCommand;
|
99 |
|
100 |
|