UNPKG

@sap/cds-dk

Version:

Command line client and development toolkit for the SAP Cloud Application Programming Model

98 lines (86 loc) 3.71 kB
const cds = require('../cds'); const { ParamCollection } = require('./params'); const AuthManager = require('./auth_manager'); const { localhostRegex } = require('./util/urls'); require('../util/axios').pruneErrors(); const { getMessage } = require('./util/logging'); const { capitalize } = require('./util/strings'); const { handleHttpError } = require('./util/errors'); const DEBUG = cds.debug('cli'); const COMMANDS = { subscribe: 'to', upgrade: 'at', unsubscribe: 'from' }; const DEFAULT_SUBSCRIBE_USER = 'yves'; const DEFAULT_SUBSCRIBE_PASSWORD = ''; module.exports = class TenantAction { // TODO JSDoc /** * @param command {string} the command to execute: 'subscribe', 'upgrade' or 'unsubscribe' */ constructor(command) { if (!(command in COMMANDS)) { throw `invalid command: ${command}`; } this.command = command; this.gerund = command.replace(/e$/, 'ing'); this.preposition = COMMANDS[command]; } async run(paramValues, { tenant, local } = {}) { let params = new ParamCollection(paramValues); params.set('skipToken', true); // Token would have insufficient scopes for this command. if (local) { // Connect to locally-served MTXS await this.runWithServer(params, tenant); } else { params = await AuthManager.login(params.toEntries()); // subscribe: use 'yves' as default username against localhost if no username is provided if (this.command === 'subscribe' && !params.has('username') && params.has('appUrl') && localhostRegex.test(params.get('appUrl'))) { params.set('username', DEFAULT_SUBSCRIBE_USER); params.set('password', DEFAULT_SUBSCRIBE_PASSWORD); params = await AuthManager.login(params.toEntries()); // set reqAuth param } // Connect to running MTXS server await this.runClientOnly(params, tenant); } console.log(`${capitalize(this.command)} successful.`); } async runClientOnly(params, tenant) { console.log(capitalize(this.gerund), { tenant }, this.preposition, { url: params.get('appUrl') }); const url = `${params.get('appUrl')}/-/cds/deployment/${this.command}`; DEBUG?.(`Sending request to ${url}`); const body = params.get('body'); let parsedBody; if (body) { try { parsedBody = JSON.parse(body) } catch (error) { throw getMessage('Invalid subscription body', { error, command: this.command }); } } try { await require('axios').post(url, { tenant, metadata: parsedBody, options: parsedBody }, params.get('reqAuth')); } catch (error) { handleHttpError(error, params, { url }); } } async runWithServer(params, tenant) { const libId = '@sap/cds-mtxs/bin/cds-mtx'; let libPath; try { libPath = require.resolve(libId, { paths: [process.cwd()] }); DEBUG?.(`Using local @sap/cds-mtxs from ${libPath}`); } catch { try { libPath = require.resolve(libId); DEBUG?.(`Using cds-mtxs from ${libPath}`); } catch (error) { throw getMessage('@sap/cds-mtxs not found or outdated. Have you changed into the directory of your SaaS app? If so, please upgrade to @sap/cds-mtxs@>=1.7.0', { error, command: this.command }); } } const { cds_mtx } = require(libPath); const body = params.get('body') return cds_mtx(this.command, tenant, body); } }