UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var ConnectionOptionsReader_1 = require("../connection/ConnectionOptionsReader");
5var CommandUtils_1 = require("./CommandUtils");
6var chalk = require("chalk");
7/**
8 * Generates a new subscriber.
9 */
10var SubscriberCreateCommand = /** @class */ (function () {
11 function SubscriberCreateCommand() {
12 this.command = "subscriber:create";
13 this.describe = "Generates a new subscriber.";
14 }
15 SubscriberCreateCommand.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 subscriber class.",
25 demand: true
26 })
27 .option("d", {
28 alias: "dir",
29 describe: "Directory where subscriber 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 SubscriberCreateCommand.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, err_2;
40 return tslib_1.__generator(this, function (_a) {
41 switch (_a.label) {
42 case 0:
43 _a.trys.push([0, 6, , 7]);
44 fileContent = SubscriberCreateCommand.getTemplate(args.name);
45 filename = args.name + ".ts";
46 directory = args.dir;
47 if (!!directory) return [3 /*break*/, 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 /*yield*/, connectionOptionsReader.get(args.connection)];
56 case 2:
57 connectionOptions = _a.sent();
58 directory = connectionOptions.cli ? connectionOptions.cli.subscribersDir : undefined;
59 return [3 /*break*/, 4];
60 case 3:
61 err_1 = _a.sent();
62 return [3 /*break*/, 4];
63 case 4:
64 path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
65 return [4 /*yield*/, CommandUtils_1.CommandUtils.createFile(path, fileContent)];
66 case 5:
67 _a.sent();
68 console.log(chalk.green("Subscriber " + chalk.blue(path) + " has been created successfully."));
69 return [3 /*break*/, 7];
70 case 6:
71 err_2 = _a.sent();
72 console.log(chalk.black.bgRed("Error during subscriber creation:"));
73 console.error(err_2);
74 process.exit(1);
75 return [3 /*break*/, 7];
76 case 7: return [2 /*return*/];
77 }
78 });
79 });
80 };
81 // -------------------------------------------------------------------------
82 // Protected Static Methods
83 // -------------------------------------------------------------------------
84 /**
85 * Gets contents of the entity file.
86 */
87 SubscriberCreateCommand.getTemplate = function (name) {
88 return "import {EventSubscriber, EntitySubscriberInterface} from \"typeorm\";\n\n@EventSubscriber()\nexport class " + name + " implements EntitySubscriberInterface<any> {\n\n}\n";
89 };
90 return SubscriberCreateCommand;
91}());
92exports.SubscriberCreateCommand = SubscriberCreateCommand;
93
94//# sourceMappingURL=SubscriberCreateCommand.js.map