@commercelayer/cli
Version:
80 lines (79 loc) • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const base_1 = tslib_1.__importStar(require("../../base"));
const config_1 = require("../../config");
const common_1 = require("../../common");
const cli_core_1 = require("@commercelayer/cli-core");
class ApplicationsIndex extends base_1.default {
static description = 'show a list of all (logged in) available CLI applications';
static aliases = ['app:list', 'applications:list', 'app:available', 'applications:available', 'apps'];
static examples = [
'$ commercelayer applications',
'$ cl applications'
];
static flags = {
extra: base_1.Flags.boolean({
char: 'X',
description: 'show applications extra info',
hidden: true
}),
sort: base_1.Flags.boolean({
char: 'S',
description: 'sort applications by Organization and Application name'
})
};
async run() {
const { flags } = await this.parse(ApplicationsIndex);
let configData;
try {
configData = (0, config_1.filterApplications)(this.config, flags);
}
catch (error) {
this.error('No application config file found', { suggestions: ['Execute first login to at least one Commerce Layer application'] });
}
const current = (0, config_1.configParam)(config_1.ConfigParams.currentApplication);
const currentChar = '\u25C9';
this.log();
if (configData.length > 0) {
const currentVisible = configData.some(a => cli_core_1.clApplication.appKeyMatch(current, a));
const sortedData = flags.sort ? configData.sort((a, b) => {
const cmp = (a.organization || '').localeCompare(b.organization || '');
return (cmp === 0) ? a.name.localeCompare(b.name) : cmp;
}) : configData;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
base_1.cliux.Table.table(sortedData, {
current: { header: `[${currentChar}]`, minWidth: 3, get: row => cli_core_1.clApplication.appKeyMatch(current, row) ? cli_core_1.clColor.magentaBright(` ${currentChar} `) : ' ' },
organization: { header: 'ORGANIZATION / USER', get: row => currentColor(row, current)(row.user || row.organization) },
slug: { header: 'SLUG', get: row => currentColor(row, current)(row.slug) },
name: { header: 'APPLICATION', get: row => currentColor(row, current)(row.name) },
kind: { header: 'KIND', get: row => currentColor(row, current)(row.kind) },
scope: { header: 'SCOPE', minWidth: 10, get: row => currentColor(row, current)((0, common_1.printScope)(row.scope)) },
customer: { header: 'PWD', get: row => (row.email && row.password) ? cli_core_1.clColor.cyanBright(cli_core_1.clOutput.center('\u221A', 'PWD'.length)) : '' },
mode: { header: 'MODE', get: row => `${((row.mode === 'live') ? cli_core_1.clColor.api.live : cli_core_1.clColor.api.test)(row.mode)}` },
alias: { header: 'ALIAS', get: row => cli_core_1.clColor.cli.alias(row.alias || '') },
...extraColumns(flags),
}, {
printLine: cli_core_1.clUtil.log,
});
if (current && currentVisible)
this.log(cli_core_1.clColor.italic.magentaBright(`\n(${currentChar}) Current application`));
}
else
this.log(cli_core_1.clColor.italic('No application found'));
this.log();
}
}
exports.default = ApplicationsIndex;
const extraColumns = (flags) => {
const extra = {};
if (flags.extra) {
extra.id = { header: 'ID', get: (row) => cli_core_1.clColor.dim(row.id || '') };
extra.appkey = { header: 'APPKEY', get: (row) => cli_core_1.clColor.dim(row.key || '') };
extra.domain = { header: 'DOMAIN', get: (row) => cli_core_1.clColor.dim(row.domain || '') };
}
return extra;
};
const currentColor = (app, current) => {
return (cli_core_1.clApplication.appKeyMatch(current, app) ? cli_core_1.clColor.magentaBright : cli_core_1.clColor.visible);
};