UNPKG

@commercelayer/cli

Version:
117 lines (116 loc) 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const config_1 = require("../../config"); const cli_core_1 = require("@commercelayer/cli-core"); const token_1 = require("../../commands/applications/token"); const cliux = tslib_1.__importStar(require("@commercelayer/cli-ux")); const excludedTopics = ['applications', 'config', 'plugins', 'util', 'autocomplete', 'token', 'cli']; const exludedCommands = [ 'plugins', 'imports:types', 'exports:types', 'resources', 'resources:doc', 'resources:filters', 'seeder:check', 'webhooks:topics', 'autocomplete', 'applications', 'cleanups:types', 'links:resources' ]; const isCommandExcluded = (cmd) => { if (exludedCommands.includes(cmd)) return true; const comPos = cmd.indexOf(':'); return (comPos >= 0) && excludedTopics.includes(cmd.substring(0, comPos)); }; // eslint-disable-next-line complexity const hook = async function (opts) { // Only for test purpouses to avoid an error of undefined object if (!opts.Command || !opts.argv) return; // Check excluded topics and commands if (isCommandExcluded(opts.Command.id)) return; const keyFlag = cli_core_1.clCommand.findLongStringFlag(opts.argv, 'appkey'); const app = { key: keyFlag?.value || '', mode: 'test', // execMode(flags.live), }; let configData; // No appkey passed io command line, looking for saved current application if (!cli_core_1.clApplication.appKeyValid(app)) { const current = (0, config_1.configParam)(config_1.ConfigParams.currentApplication); if (current !== undefined) Object.assign(app, current); if (!cli_core_1.clApplication.appKeyValid(app)) return; } // Check if config file exists and load application data if ((0, config_1.configFileExists)(this.config, app)) configData = (0, config_1.readConfigFile)(this.config, app); else this.error('Unable to find configuration file for application'); if (!configData) return; // If present remove application key flag option (needed only to load application info) if (keyFlag) opts.argv.splice(keyFlag.index, keyFlag.single ? 1 : 2); // If config file exists check application type const typeCheck = (0, config_1.configParam)(config_1.ConfigParams.applicationTypeCheck); if (typeCheck) { if (!typeCheck.includes(configData.kind)) this.error(`The application (${cli_core_1.clColor.api.application(configData.key)}) has an invalid type: ${cli_core_1.clColor.msg.error(configData.kind)}, while the only accepted type are ${cli_core_1.clColor.api.kind(typeCheck.join(','))}\nPlease use a correct one or access the online organization's dashboard and create a new valid application`); } // Check command flags const _flags = cli_core_1.clCommand.allFlags(opts.Command); const ffIdx = Math.max(opts.argv.findIndex(arg => !arg.startsWith('-')) + 1, opts.argv.findIndex(arg => arg.startsWith('-'))); // Add to command line args application info read from config file if (_flags.organization && configData.slug) opts.argv.splice(ffIdx, 0, '--organization=' + configData.slug); if (_flags.domain && configData.domain) opts.argv.splice(ffIdx, 0, '--domain=' + configData.domain); // If command requires clientId and clientSecret (or scope) add them to the command line arguments if (_flags.clientId && configData.clientId) opts.argv.splice(ffIdx, 0, '--clientId=' + configData.clientId); if (_flags.clientSecret && configData.clientSecret) opts.argv.splice(ffIdx, 0, '--clientSecret=' + configData.clientSecret); const scope = cli_core_1.clApplication.arrayScope(configData.scope); if (_flags.scope && (scope.length > 0)) opts.argv.splice(ffIdx, 0, scope.map(s => `--scope=${s}`).join(' ')); const atFlag = cli_core_1.clCommand.findLongStringFlag(opts.argv, 'accessToken'); if (!atFlag && _flags.accessToken) { // accessToken not passed in comman line but required by the command let tokenData = null; let refresh = false; if ((0, config_1.tokenFileExists)(opts.config, app)) { tokenData = (0, config_1.readTokenFile)(opts.config, app); if (cli_core_1.clToken.isAccessTokenExpiring(tokenData)) { cliux.action.start('Refreshing access token ...'); refresh = true; // If not overridden by saved current application, load configuration data if (!configData) configData = (0, config_1.readConfigFile)(this.config, app); await cli_core_1.clToken.revokeAccessToken(configData, tokenData.accessToken).catch(err => { this.log(`Error revoking old access token: ${err.message}`); }); // no matter if token revoke fails tokenData = null; } } if (tokenData === null) { const token = await (0, token_1.newAccessToken)(this.config, app, true).catch(() => { if (refresh) cliux.action.stop(cli_core_1.clColor.msg.error('Error')); }); if (token) tokenData = token; if (refresh) cliux.action.stop(); } if (!tokenData?.accessToken) this.error('Unable to refresh application access token'); opts.argv.push('--accessToken=' + String(tokenData?.accessToken)); } }; exports.default = hook;