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