UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const authentications_1 = require("@bearer/types/lib/authentications");
5const command_1 = require("@oclif/command");
6const base_command_1 = require("../../base-command");
7const decorators_1 = require("../../utils/decorators");
8const helpers_1 = require("../../utils/helpers");
9var TComponent;
10(function (TComponent) {
11 TComponent["BLANK"] = "blank";
12 TComponent["COLLECTION"] = "collection";
13 TComponent["ROOT"] = "root";
14})(TComponent || (TComponent = {}));
15class GenerateComponent extends base_command_1.default {
16 async run() {
17 const { args, flags } = this.parse(GenerateComponent);
18 const type = flags.type || (await this.askForComponentType());
19 const name = args.name || (await this.askForString('Name'));
20 const outDir = type === TComponent.ROOT ? this.locator.srcViewsDir : this.locator.srcViewsDirResource('components');
21 try {
22 await helpers_1.copyFiles(this, `generate/${type}Component`, outDir, this.getVars(name, this.integrationAuthConfig.authType));
23 // TODO: add a nicer display
24 this.success(`\nComponent generated`);
25 if (type === TComponent.ROOT) {
26 this.warn(this.colors.italic(
27 // tslint:disable-next-line:max-line-length
28 'Please make sure to update the spec.ts file to reflect your new Root Component on the Developer Portal preview page.'));
29 }
30 }
31 catch (e) {
32 this.error(e);
33 }
34 }
35 getVars(name, authType) {
36 const componentName = this.case.pascal(name);
37 return {
38 componentName,
39 fileName: name,
40 componentClassName: componentName,
41 componentTagName: this.case.kebab(componentName),
42 groupName: this.case.kebab(componentName),
43 withAuthScreen: authType === authentications_1.default.OAuth2 ? '<bearer-navigator-auth-screen />' : null
44 };
45 }
46 async askForComponentType() {
47 const { type } = await this.inquirer.prompt([
48 {
49 choices,
50 message: 'What kind of component would you like to generate:',
51 type: 'list',
52 name: 'type'
53 }
54 ]);
55 return type;
56 }
57}
58GenerateComponent.description = 'Generate a Bearer component';
59GenerateComponent.aliases = ['g:c'];
60GenerateComponent.flags = Object.assign({}, base_command_1.default.flags, { type: command_1.flags.string({ char: 't', options: Object.values(TComponent) }) });
61GenerateComponent.args = [{ name: 'name' }];
62tslib_1.__decorate([
63 decorators_1.RequireIntegrationFolder()
64], GenerateComponent.prototype, "run", null);
65exports.default = GenerateComponent;
66// TODO: better names
67const choices = [
68 {
69 name: 'Blank',
70 value: TComponent.BLANK
71 },
72 {
73 name: 'Collection',
74 value: TComponent.COLLECTION
75 },
76 {
77 name: 'Root component',
78 value: TComponent.ROOT
79 }
80];