1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const authentications_1 = require("@bearer/types/lib/authentications");
|
4 | const command_1 = require("@oclif/command");
|
5 | const fs = require("fs-extra");
|
6 | const Listr = require("listr");
|
7 | const path = require("path");
|
8 | const Case = require("case");
|
9 | const base_command_1 = require("../base-command");
|
10 | const install_dependencies_1 = require("../tasks/install-dependencies");
|
11 | const init_views_1 = require("../tasks/init-views");
|
12 | const helpers_1 = require("../utils/helpers");
|
13 | const component_1 = require("./generate/component");
|
14 | const spec_1 = require("./generate/spec");
|
15 | const inquirer = require("inquirer");
|
16 | const prompts_1 = require("../utils/prompts");
|
17 | exports.authTypes = {
|
18 | [authentications_1.Authentications.OAuth1]: { name: 'OAuth1', value: authentications_1.Authentications.OAuth1 },
|
19 | [authentications_1.Authentications.OAuth2]: { name: 'OAuth2', value: authentications_1.Authentications.OAuth2 },
|
20 | [authentications_1.Authentications.Basic]: { name: 'Basic Auth', value: authentications_1.Authentications.Basic },
|
21 | [authentications_1.Authentications.ApiKey]: { name: 'API Key', value: authentications_1.Authentications.ApiKey },
|
22 | [authentications_1.Authentications.NoAuth]: { name: 'NoAuth', value: authentications_1.Authentications.NoAuth },
|
23 | [authentications_1.Authentications.Custom]: { name: 'Custom', value: authentications_1.Authentications.Custom }
|
24 | };
|
25 | class New extends base_command_1.default {
|
26 | constructor() {
|
27 | super(...arguments);
|
28 | this.getVars = (name, authType) => ({
|
29 | integrationTitle: name,
|
30 | componentName: Case.pascal(name),
|
31 | componentTagName: Case.kebab(name),
|
32 | bearerTagVersion: process.env.BEARER_PACKAGE_VERSION || 'latest',
|
33 | bearerRestClient: this.bearerRestClient(authType)
|
34 | });
|
35 | this.initViewsVars = (name, authType) => {
|
36 | const setup = authType === authentications_1.Authentications.NoAuth
|
37 | ? ''
|
38 | : `
|
39 | {
|
40 | classname: 'SetupAction',
|
41 | isRoot: true,
|
42 | initialTagName: 'setup-action',
|
43 | name: 'setup-action',
|
44 | label: 'Setup Action Component'
|
45 | },
|
46 | {
|
47 | classname: 'SetupView',
|
48 | isRoot: true,
|
49 | initialTagName: 'setup-view',
|
50 | name: 'setup-view',
|
51 | label: 'Setup Display Component'
|
52 | },`;
|
53 | return Object.assign({}, this.getVars(name, authType), { setup });
|
54 | };
|
55 | }
|
56 | async run() {
|
57 | const { args, flags } = this.parse(New);
|
58 | this.path = flags.path;
|
59 | try {
|
60 | const name = args.IntegrationName || (await prompts_1.askForString('Integration name'));
|
61 | const authType = flags.authType || (await this.askForAuthType());
|
62 | const skipInstall = flags.skipInstall;
|
63 | this.destinationFolder = name;
|
64 | const tasks = [
|
65 | {
|
66 | title: 'Generating integration structure',
|
67 | task: async (ctx) => {
|
68 | try {
|
69 | const files = await this.createStructure(name, authType);
|
70 | ctx.files = files;
|
71 | return true;
|
72 | }
|
73 | catch (e) {
|
74 | this.error(e);
|
75 | return null;
|
76 | }
|
77 | }
|
78 | },
|
79 | {
|
80 | title: 'Create auth files',
|
81 | task: async (ctx, _task) => {
|
82 | const files = await this.createAuthenticationFiles(name, authType);
|
83 | ctx.files = [...ctx.files, ...files];
|
84 | }
|
85 | },
|
86 | {
|
87 | title: 'Create views related files',
|
88 | enabled: () => flags.withViews,
|
89 | task: async (_ctx, _task) => {
|
90 | return new Listr([
|
91 | init_views_1.default({
|
92 | cmd: this,
|
93 | vars: this.initViewsVars(name, authType)
|
94 | }),
|
95 | {
|
96 | title: 'Create integration specification file',
|
97 | task: async (_ctx, _task) => spec_1.default.run(['--path', this.copyDestFolder, '--silent'])
|
98 | },
|
99 | {
|
100 | title: 'Create intial components',
|
101 | task: async (_ctx, _task) => component_1.default.run(['feature', '--type', 'root', '--path', this.copyDestFolder, '--silent'])
|
102 | }
|
103 | ]);
|
104 | }
|
105 | }
|
106 | ];
|
107 | if (!skipInstall) {
|
108 | tasks.push(install_dependencies_1.default({ cwd: this.copyDestFolder }));
|
109 | }
|
110 | const { files } = await new Listr(tasks).run();
|
111 | helpers_1.printFiles(this, files);
|
112 | this.success(`Integration initialized, name: ${name}, authentication type: ${exports.authTypes[authType].name}`);
|
113 | this.log("\nWhat's next?\n");
|
114 | this.log('* read the bearer documentation at https://docs.bearer.sh\n');
|
115 | this.log(`* start your local development environment by running:\n`);
|
116 | this.log(this.colors.bold(` cd ${name} && bearer start`));
|
117 | }
|
118 | catch (e) {
|
119 | this.error(e);
|
120 | }
|
121 | }
|
122 | bearerRestClient(authType) {
|
123 | switch (authType) {
|
124 | case authentications_1.Authentications.OAuth1:
|
125 | return '"oauth": "^0.9.15"';
|
126 | case authentications_1.Authentications.ApiKey:
|
127 | case authentications_1.Authentications.Basic:
|
128 | case authentications_1.Authentications.NoAuth:
|
129 | case authentications_1.Authentications.OAuth2:
|
130 | case authentications_1.Authentications.Custom:
|
131 | return '"axios": "^0.18.0"';
|
132 | default:
|
133 | throw new Error(`Authentication not found: ${authType}`);
|
134 | }
|
135 | }
|
136 | get copyDestFolder() {
|
137 | if (this.path) {
|
138 | return path.resolve(this.path);
|
139 | }
|
140 | return path.join(process.cwd(), this.destinationFolder);
|
141 | }
|
142 | createStructure(name, authType) {
|
143 | if (fs.existsSync(this.copyDestFolder)) {
|
144 | return Promise.reject(this.colors.bold('Destination already exists: ') + this.copyDestFolder);
|
145 | }
|
146 | return helpers_1.copyFiles(this, path.join('init', 'structure'), this.copyDestFolder, this.getVars(name, authType), true);
|
147 | }
|
148 | createAuthenticationFiles(name, authType) {
|
149 | return helpers_1.copyFiles(this, path.join('init', authType), this.copyDestFolder, this.getVars(name, authType), true);
|
150 | }
|
151 | async askForAuthType() {
|
152 | const { authenticationType } = await inquirer.prompt([
|
153 | {
|
154 | message: 'Select an authentication method for the API you want to consume:',
|
155 | type: 'list',
|
156 | name: 'authenticationType',
|
157 | choices: Object.values(exports.authTypes)
|
158 | }
|
159 | ]);
|
160 | return authenticationType;
|
161 | }
|
162 | }
|
163 | New.description = 'generate integration boilerplate';
|
164 | New.flags = Object.assign({}, base_command_1.default.flags, { help: command_1.flags.help({ char: 'h' }), skipInstall: command_1.flags.boolean({ hidden: true }), withViews: command_1.flags.boolean(), authType: command_1.flags.string({
|
165 | char: 'a',
|
166 | description: 'Authorization type',
|
167 |
|
168 | options: Object.keys(exports.authTypes).map(auth => exports.authTypes[auth].value)
|
169 | }) });
|
170 | New.args = [{ name: 'IntegrationName' }];
|
171 | exports.default = New;
|