@commercelayer/cli
Version:
74 lines (73 loc) • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const cli_core_1 = require("@commercelayer/cli-core");
const core_1 = require("@oclif/core");
const config_1 = require("../../config");
const login_1 = tslib_1.__importStar(require("./login"));
const common_1 = require("../../common");
const current_1 = require("./current");
class ApplicationsScope extends core_1.Command {
static description = 'switch scope of current application';
static aliases = ['app:scope'];
static examples = [
'$ commercelayer <%= command.id %> market:code:1234',
'$ cl app:scope market:id:aBcDeFgHij'
];
static flags = {
alias: core_1.Flags.string({
char: 'a',
description: 'the alias you want to associate to the application',
multiple: false,
required: false
}),
};
static args = {
scope: core_1.Args.string({ name: 'scope', required: true, description: 'the application scope' }),
};
async run() {
const { args, flags } = await this.parse(ApplicationsScope);
const scope = (0, login_1.checkScope)([args.scope]);
const alias = flags.alias || scope.replace(':', '_');
const current = (0, config_1.currentApplication)();
if (!current)
this.error('No current application: execute login to an application before switching scope');
if (current.kind !== 'sales_channel')
this.error(`You can only switch scope of ${cli_core_1.clColor.api.kind('sales_channel')} applications`);
const apps = (0, config_1.filterApplications)(this.config, {
organization: current.slug,
domain: current.domain,
kind: current.kind,
mode: current.mode,
}).filter(a => {
if (!a.scope)
return true;
if (Array.isArray(a.scope))
return (a.scope.length === 1) && a.scope.includes(scope);
return (a.scope === scope);
});
// Always use current organization (for core apps) and (optional) domain
const argv = [];
if (current.slug)
argv.push('-o', current.slug);
if (current.domain)
argv.push('-d', current.domain);
let appInfo;
if (apps.length > 0) { // Application Switch
// ApplicationsSwitch command cannot filter by scope so it's necessary do "manually" show the list of applications
const app = (apps.length === 1) ? apps[0] : await (0, common_1.promptApplication)(apps);
const cApp = (0, config_1.currentApplication)(app);
this.log(`\nCurrent application: ${(0, current_1.printCurrent)(cApp)}\n`);
appInfo = app;
}
else { // Application Login
const app = (0, config_1.readConfigFile)(this.config, current);
argv.push('-i', app.clientId);
argv.push('-S', scope);
argv.push('-a', alias);
appInfo = await login_1.default.run(argv, this.config);
}
return appInfo;
}
}
exports.default = ApplicationsScope;