@unito/integration-cli
Version:
Integration CLI
111 lines (108 loc) • 5.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const baseCommand_1 = require("../baseCommand");
const errors_1 = require("../errors");
const integrations_1 = require("../resources/integrations");
const GlobalConfiguration = tslib_1.__importStar(require("../resources/globalConfiguration"));
const IntegrationsPlatform = tslib_1.__importStar(require("../services/integrationsPlatform"));
const IntegrationConfiguration = tslib_1.__importStar(require("../resources/configuration"));
class Activity extends baseCommand_1.BaseCommand {
static description = 'Show the activity associated with your integration';
static examples = ['<%= config.bin %> <%= command.id %>'];
static flags = {
number: core_1.Flags.integer({
char: 'n',
summary: 'number of events to show',
description: `Limit the number of events to show. Must be less than or equal to 1000. Default is 1000.
Usage: <%= config.bin %> <%= command.id %> --number=10`,
}),
environment: core_1.Flags.custom({
description: 'the environment of the platform',
options: Object.values(GlobalConfiguration.Environment),
default: GlobalConfiguration.Environment.Production,
})(),
follow: core_1.Flags.boolean({
char: 'f',
summary: 'follow the activity',
description: `Follow the activity of your integration. This will keep the command running and will show new events as they are created.
Usage: <%= config.bin %> <%= command.id %> --follow`,
}),
};
async catch(error) {
/* istanbul ignore if */
if ((0, errors_1.handleError)(this, error)) {
this.exit(-1);
}
throw error;
}
async run() {
(0, integrations_1.validateIsIntegrationDirectory)();
const { flags } = await this.parse(Activity);
// Read the configurations.
const globalConfiguration = await GlobalConfiguration.read(this.config.configDir);
const environment = flags.environment ?? GlobalConfiguration.Environment.Production;
const integrationConfiguration = await IntegrationConfiguration.getConfiguration(environment);
// Choose the right API key for the environment.
let apiKey = undefined;
if (environment === GlobalConfiguration.Environment.Local) {
apiKey = globalConfiguration.apiKeyLocal;
}
else if (environment === GlobalConfiguration.Environment.Staging) {
apiKey = globalConfiguration.apiKeyStaging;
}
else {
apiKey = globalConfiguration.apiKey;
}
// Setup the platform client.
IntegrationsPlatform.setApiKey(apiKey);
IntegrationsPlatform.setEnvironment(environment);
core_1.ux.action.start('Finding the integration', undefined, { stdout: true });
let integration;
try {
integration = await IntegrationsPlatform.getIntegrationByName(integrationConfiguration.name);
}
catch (error) {
if (error instanceof IntegrationsPlatform.HttpError && error.status === 403) {
core_1.ux.log(chalk_1.default.redBright(`Access Denied! You do not have access to this integration.`));
this.exit(-1);
}
else {
core_1.ux.log(chalk_1.default.redBright(`Integration not found! You can only retrieve activity for existing integrations.`));
this.exit(-1);
}
}
core_1.ux.action.stop();
core_1.ux.action.start('Retrieving activity', undefined, { stdout: true });
let lastEventDate;
// eslint-disable-next-line no-constant-condition
while (true) {
const getEventsOptions = {
$from: lastEventDate,
limit: flags.number,
};
const events = (await IntegrationsPlatform.getIntegrationEvents(integration.id, getEventsOptions)).reverse();
for (const event of events) {
core_1.ux.log();
core_1.ux.log(chalk_1.default.bold(` ${event.date} - ${event.title}`));
core_1.ux.log(` ${event.text}`);
core_1.ux.log(chalk_1.default.dim(chalk_1.default.italic(` ${event.tags.join(', ')}`)));
}
if (process.env.NODE_ENV === 'test' || !flags.follow) {
break;
}
const mostRecentEvent = events.at(-1);
const mostRecentEventTimestamp = Date.parse(mostRecentEvent?.date ?? '');
lastEventDate = isNaN(mostRecentEventTimestamp) ? Date.now() : mostRecentEventTimestamp + 1;
// Wait 5 seconds before checking for new events.
await this.wait(5000);
}
core_1.ux.action.stop();
}
async wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
exports.default = Activity;