1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const command_1 = require("@oclif/command");
|
5 | const fs = require("fs");
|
6 | const path = require("path");
|
7 | const base_command_1 = require("../../base-command");
|
8 | const decorators_1 = require("../../utils/decorators");
|
9 | const Listr = require("listr");
|
10 | const build_setup_1 = require("../../tasks/build-setup");
|
11 | const authentications_1 = require("@bearer/types/lib/authentications");
|
12 | class GenerateSetup extends base_command_1.default {
|
13 | async run() {
|
14 | const { flags } = this.parse(GenerateSetup);
|
15 | if (flags.force || !setupExists(this.locator.srcViewsDir)) {
|
16 | const { authType } = this.integrationAuthConfig;
|
17 | const fields = FIELDS[authType];
|
18 | if (fields && fields.length) {
|
19 | try {
|
20 | const vars = this.getVars(this.bearerConfig.integrationConfig.integrationTitle, fields);
|
21 | const tasks = build_setup_1.default({
|
22 | vars,
|
23 | cmd: this
|
24 | });
|
25 | await new Listr(tasks).run();
|
26 | }
|
27 | catch (e) {
|
28 | this.error(e);
|
29 | }
|
30 | }
|
31 | else {
|
32 | this.warn(`No setupViews key found within auth.config.json file. skipping`);
|
33 | }
|
34 | }
|
35 | else {
|
36 | this.warn('Setup files already exist, use --force flag to overwrite existing ones.');
|
37 | }
|
38 | }
|
39 | getVars(integrationTitle, fields) {
|
40 | return {
|
41 | componentName: this.case.pascal(integrationTitle),
|
42 | componentTagName: this.case.kebab(integrationTitle),
|
43 | fields: JSON.stringify(fields)
|
44 | };
|
45 | }
|
46 | }
|
47 | GenerateSetup.description = 'Generate a Bearer Setup';
|
48 | GenerateSetup.hidden = true;
|
49 | GenerateSetup.flags = Object.assign({}, base_command_1.default.flags, { force: command_1.flags.boolean({}) });
|
50 | GenerateSetup.args = [];
|
51 | tslib_1.__decorate([
|
52 | decorators_1.skipIfNoViews(),
|
53 | decorators_1.RequireIntegrationFolder()
|
54 | ], GenerateSetup.prototype, "run", null);
|
55 | exports.default = GenerateSetup;
|
56 |
|
57 | function setupExists(location) {
|
58 | return (fs.existsSync(path.join(location, 'setup-action.tsx')) || fs.existsSync(path.join(location, 'setup-display.tsx')));
|
59 | }
|
60 | const FIELDS = {
|
61 | [authentications_1.default.Basic]: [
|
62 | { type: 'text', label: 'Username', controlName: 'username' },
|
63 | { type: 'password', label: 'Password', controlName: 'password' }
|
64 | ],
|
65 | [authentications_1.default.ApiKey]: [{ type: 'password', label: 'Api Key', controlName: 'apiKey' }],
|
66 | [authentications_1.default.OAuth1]: [
|
67 | { type: 'text', label: 'Consumer Key', controlName: 'consumerKey' },
|
68 | { type: 'password', label: 'Consumer Secret', controlName: 'consumerSecret' }
|
69 | ],
|
70 | [authentications_1.default.OAuth2]: [
|
71 | { type: 'text', label: 'Client ID', controlName: 'clientID' },
|
72 | { type: 'password', label: 'Client Secret', controlName: 'clientSecret' }
|
73 | ],
|
74 | [authentications_1.default.NoAuth]: [],
|
75 | [authentications_1.default.Custom]: []
|
76 | };
|