@commercelayer/cli
Version:
78 lines (77 loc) • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const sdk_1 = require("@commercelayer/sdk");
const config_1 = require("../../config");
const util_1 = require("util");
const login_1 = tslib_1.__importStar(require("./login"));
const cli_core_1 = require("@commercelayer/cli-core");
class ApplicationsAdd extends core_1.Command {
static description = `add a new Commerce Layer application to CLI config (application must be of kind 'integration' or 'sales_channel')`;
static aliases = ['app:add'];
static examples = [
'$ commercelayer applications:add -o <organizationSlug> -i <clientId> -s <clientSecret> -a <applicationAlias>',
'$ cl app:add -i <clientId> -s <clientSecret> -a <applicationAlias>'
];
static flags = {
...login_1.default.flags
};
async catch(error) {
this.error(error.message);
}
async parse(c) {
cli_core_1.clCommand.fixDashedFlagValue(this.argv, c.flags.clientId);
const parsed = await super.parse(c);
cli_core_1.clCommand.fixDashedFlagValue(this.argv, c.flags.clientId, 'i', parsed);
return parsed;
}
async run() {
const { flags } = await this.parse(ApplicationsAdd);
if (!flags.clientSecret && !flags.scope)
this.error(`You must provide one of the arguments ${cli_core_1.clColor.cli.flag('clientSecret')} and ${cli_core_1.clColor.cli.flag('scope')}`);
const scope = (0, login_1.checkScope)(flags.scope);
const alias = (0, login_1.checkAlias)(flags.alias, this.config, flags.organization);
const config = {
clientId: flags.clientId,
clientSecret: flags.clientSecret,
slug: flags.organization,
domain: flags.domain,
scope,
email: flags.email,
password: flags.password
};
if (config.domain === (0, config_1.configParam)(config_1.ConfigParams.defaultDomain))
config.domain = undefined;
try {
const token = await cli_core_1.clToken.getAccessToken(config);
if (!token?.accessToken)
this.error('Unable to get access token');
const app = await (0, login_1.getApplicationInfo)(config, token?.accessToken || '');
const typeCheck = (0, config_1.configParam)(config_1.ConfigParams.applicationTypeCheck);
if (typeCheck) {
if (!typeCheck.includes(app.kind))
this.error(`The credentials provided are associated to an application of type ${cli_core_1.clColor.msg.error(app.kind)} while the only allowed types are: ${cli_core_1.clColor.api.kind(typeCheck.join(','))}`
// , { suggestions: [`Double check your credentials or access the online dashboard of ${clColor.api.organization(app.organization)} and create a new valid application `] }
);
}
app.alias = alias;
(0, config_1.appsDirCreate)(this.config);
(0, config_1.writeConfigFile)(this.config, app);
(0, config_1.writeTokenFile)(this.config, app, token);
this.log(`\n${cli_core_1.clColor.msg.success.bold('Login successful!')} The new application has been successfully added to the CLI\n`);
}
catch (error) {
this.log(cli_core_1.clColor.msg.error.bold('Login failed!'));
if (flags.debug)
this.error((0, util_1.inspect)(error, false, null, true));
else if (sdk_1.CommerceLayerStatic.isApiError(error))
this.error((0, util_1.inspect)(error.errors, false, null, true));
else {
const connectMsg = cli_core_1.clApplication.isProvisioningApp(config) ? cli_core_1.clColor.msg.error('Provisioning API') : `organization ${cli_core_1.clColor.msg.error(config.slug)}`;
this.error(`Unable to connect to ${connectMsg}: ${cli_core_1.clColor.italic(error.message)}`);
}
}
}
}
exports.default = ApplicationsAdd;