UNPKG

@unito/integration-cli

Version:

Integration CLI

78 lines (77 loc) 3.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const inquirer_1 = tslib_1.__importDefault(require("inquirer")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const gradient = tslib_1.__importStar(require("gradient-string")); const baseCommand_1 = require("../baseCommand"); const errors_1 = require("../errors"); const GlobalConfiguration = tslib_1.__importStar(require("../resources/globalConfiguration")); const IntegrationsPlatform = tslib_1.__importStar(require("../services/integrationsPlatform")); class Login extends baseCommand_1.BaseCommand { static description = 'Login to the Integrations Platform'; static examples = ['<%= config.bin %> <%= command.id %>']; static flags = { environment: core_1.Flags.custom({ description: 'the environment of the platform', options: Object.values(GlobalConfiguration.Environment), default: GlobalConfiguration.Environment.Production, })(), }; async catch(error) { /* istanbul ignore if */ if ((0, errors_1.handleError)(this, error)) { this.exit(-1); } throw error; } async run() { const { flags } = await this.parse(Login); // Read the current global configuration. const configuration = await GlobalConfiguration.read(this.config.configDir); const environment = flags.environment ?? GlobalConfiguration.Environment.Production; // Inquire for the API key. let apiKey = undefined; try { apiKey = (await inquirer_1.default.prompt([ { type: 'input', name: 'apiKey', message: 'Enter your API key:', }, ])).apiKey; } catch (err) { core_1.ux.log(chalk_1.default.redBright(`failed because: ${(err ?? {}).message}`)); this.exit(-1); } // Retrieve the user's profile. core_1.ux.action.start('Log in', undefined, { stdout: true }); let profile = undefined; IntegrationsPlatform.setEnvironment(environment); IntegrationsPlatform.setApiKey(apiKey); try { profile = await IntegrationsPlatform.getProfile(); } catch (err) { core_1.ux.action.stop(chalk_1.default.redBright(`failed because: ${(err ?? {}).message}`)); this.exit(-1); } core_1.ux.action.stop(); core_1.ux.log([chalk_1.default.greenBright('Hello'), gradient.fruit(profile?.email ?? ''), chalk_1.default.greenBright('!')].join(' ')); // Set the API key in the global configuration. if (environment === GlobalConfiguration.Environment.Local) { configuration.apiKeyLocal = apiKey; } else if (environment === GlobalConfiguration.Environment.Staging) { configuration.apiKeyStaging = apiKey; } else { configuration.apiKey = apiKey; } // Write the new global configuration. await GlobalConfiguration.write(this.config.configDir, configuration); } } exports.default = Login;