UNPKG

3.25 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const command_1 = require("@oclif/command");
5const fs = require("fs");
6const path = require("path");
7const base_command_1 = require("../../base-command");
8const decorators_1 = require("../../utils/decorators");
9const Listr = require("listr");
10const build_setup_1 = require("../../tasks/build-setup");
11const authentications_1 = require("@bearer/types/lib/authentications");
12class 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}
47GenerateSetup.description = 'Generate a Bearer Setup';
48GenerateSetup.hidden = true;
49GenerateSetup.flags = Object.assign({}, base_command_1.default.flags, { force: command_1.flags.boolean({}) });
50GenerateSetup.args = [];
51tslib_1.__decorate([
52 decorators_1.skipIfNoViews(),
53 decorators_1.RequireIntegrationFolder()
54], GenerateSetup.prototype, "run", null);
55exports.default = GenerateSetup;
56// Note: using Or condition in case the developer delete one but customized the other component
57function setupExists(location) {
58 return (fs.existsSync(path.join(location, 'setup-action.tsx')) || fs.existsSync(path.join(location, 'setup-display.tsx')));
59}
60const 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};