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