UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

99 lines (90 loc) 2.68 kB
'use strict'; import * as path from 'path'; import * as fs from 'fs'; import { pathToFileURL } from 'url'; const args = process.argv.slice(2); const target = JSON.parse(args[0]); const userData = JSON.parse(args[1]); const projectJson = JSON.parse(args[2]); const authJson = JSON.parse(args[3]); const buildDir = JSON.parse(args[4]); const responseFile = path.join(buildDir, '.catalyst', 'user_res_body'); const metaFile = path.join(buildDir, '.catalyst', 'user_meta.json'); const writeToFile = (resp, status) => { fs.writeFileSync(responseFile, resp); fs.writeFileSync(metaFile, JSON.stringify({ response: { statusCode: status } })); }; /** * execution timeout of 15 minutes */ const timeout = 15 * 60 * 1000; const endTime = timeout + Date.now(); // exit on timeout process.env.DEBUG === 'false' && setTimeout(() => { writeToFile('TIMEOUT', 408); process.exit(0); }, timeout); const context = { catalystHeaders: Object.assign(projectJson, authJson), getMaxExecutionTimeMs: () => timeout, getRemainingExecutionTimeMs: () => endTime - Date.now(), closeWithSuccess: () => { writeToFile('SUCCESS', 200); process.exit(0); }, closeWithFailure: () => { writeToFile('FAILURE', 530); process.exit(0); } }; const eventReq = { data: userData.data, time: userData.event_time, getData: () => userData.data, getTime: () => userData.event_time, getAction: () => userData.action, getSource: () => userData.source, getSourceEntityId: () => userData.source_entity_id, getEventBusDetails: () => userData.event_bus_details, getProjectDetails: () => userData.project_details, getRawData: () => userData, /** * @deprecated use individual get methods. */ getSourceDetails: () => { // eslint-disable-next-line no-console console.error( 'CatalystDeprecationWarning: eventReq.getSourceDetails() is deprecated due to usability issues. Please use individual get methods instead.' ); return { action: userData.action, type: userData.source, entityId: userData.source_entity_id, getBusDetails: () => userData.event_bus_details }; } }; import(pathToFileURL(target.index)) .then((module) => { try { if (!('default' in module)) { throw new Error('Could not find any default export'); } if (typeof module.default !== 'function') { throw new Error('The default export is not a function'); } module.default(eventReq, context); } catch (e) { // eslint-disable-next-line no-console console.error(e); writeToFile('CODE_EXCEPTION', 532); process.exit(0); } }) .catch((e) => { // eslint-disable-next-line no-console console.error(e); writeToFile('INTERNAL_ERROR', 500); process.exit(0); });