UNPKG

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