zcatalyst-cli
Version:
Command Line Tool for CATALYST
83 lines (82 loc) • 4.64 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ansi_colors_1 = require("ansi-colors");
const endpoints_1 = require("../../../endpoints");
const error_1 = __importDefault(require("../../../error"));
const command_1 = __importDefault(require("../../../internal/command"));
const prompt_1 = __importDefault(require("../../../prompt"));
const constants_1 = require("../../../util_modules/constants");
const fs_1 = require("../../../util_modules/fs");
const project_1 = require("../../../util_modules/project");
exports.default = new command_1.default('event:generate:job [jobpool_id]')
.description('Generate sample payloads for job functions')
.needs('rc')
.needs('auth')
.action((jobpoolId) => __awaiter(void 0, void 0, void 0, function* () {
if (!fs_1.ASYNC.fileExists(constants_1.TEMPLATE.job_data)) {
throw new error_1.default('Could not read request template', { exit: 2 });
}
const jobData = yield fs_1.ASYNC.readFile(constants_1.TEMPLATE.job_data);
const jobDataJson = JSON.parse(jobData || '');
const jobpoolAPI = yield (0, endpoints_1.jobScheduling)();
const jobpoolDetails = jobpoolId
? yield jobpoolAPI.getJobpoolDetails(jobpoolId)
: yield (() => __awaiter(void 0, void 0, void 0, function* () {
const allJobpoolDetails = yield jobpoolAPI.getJobpoolDetails();
if (allJobpoolDetails.length === 0) {
throw new error_1.default('No jobpool present in development environment', {
exit: 1,
errorId: 'JOB-1',
arg: [
(0, ansi_colors_1.bold)(`${(0, project_1.getProjectName)()} (${(0, project_1.getProjectId)()})`),
ansi_colors_1.italic.underline(`${constants_1.ORIGIN.console}/baas/${(0, project_1.getEnvId)()}/project/${(0, project_1.getProjectId)()}/Development#/jobscheduling/jobpool`)
]
});
}
const jobpoolChoices = allJobpoolDetails.reduce((poolChoices, _pool) => {
if (_pool.type !== 'Function') {
return poolChoices;
}
poolChoices.push(prompt_1.default.choice(`${_pool.name} [${_pool.id}]`, {
value: _pool,
short: _pool.name
}));
return poolChoices;
}, []);
if (jobpoolChoices.length === 0) {
throw new error_1.default('No valid function jobpool available in the development environment', {
exit: 1,
errorId: 'JOB-2',
arg: [
(0, ansi_colors_1.bold)(`${(0, project_1.getProjectName)()} (${(0, project_1.getProjectId)()})`),
ansi_colors_1.italic.underline(`${constants_1.ORIGIN.console}/baas/${(0, project_1.getEnvId)()}/project/${(0, project_1.getProjectId)()}/Development#/jobscheduling/jobpool`)
]
});
}
const jobpoolQuestion = yield prompt_1.default.ask(prompt_1.default.question('details', "Please choose one of the function jobpool's to generate the data", {
type: 'list',
choices: jobpoolChoices
}));
return jobpoolQuestion.details;
}))();
if (jobpoolDetails.type !== 'Function') {
throw new error_1.default(`The jobpool ${jobpoolId || jobpoolDetails.name || jobpoolDetails.id} is not a function jobpool`, {
exit: 1
});
}
jobDataJson.job_details.job_meta_details.jobpool_id = jobpoolDetails.id;
jobDataJson.job_details.job_meta_details.jobpool_details = jobpoolDetails;
process.stdout.write(JSON.stringify(jobDataJson, null, ' ') + '\n');
}));