zcatalyst-cli
Version:
Command Line Tool for CATALYST
128 lines (118 loc) • 3.46 kB
JavaScript
;
import * as util from 'util';
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 requestFile = path.join(buildDir, '.catalyst', 'user_req_body');
const responseFile = path.join(buildDir, '.catalyst', 'user_res_body');
const metaFile = path.join(buildDir, '.catalyst', 'user_meta.json');
const writeToFile = (respJson, metaJson) => {
const respStr = JSON.stringify(respJson);
fs.writeFileSync(responseFile, respStr);
metaJson.response['Content-Type'] = 'application/json';
metaJson.response['Content-Length'] = respStr.length;
fs.writeFileSync(metaFile, JSON.stringify(metaJson));
};
let body = {};
let finalResponse = '';
let userStatus = 200;
if (fs.existsSync(requestFile)) {
const content = fs.readFileSync(requestFile);
try {
body = JSON.parse(content.toString());
} catch (err) {
body = {};
}
}
const endTime = 30 * 1000 + Date.now();
const context = {
catalystHeaders: Object.assign(projectJson, authJson),
getMaxExecutionTimeMs: () => 30 * 1000, // 30 sec
getRemainingExecutionTimeMs: () => endTime - Date.now(),
close: () => {
writeToFile(
{
output: finalResponse
},
{ response: { statusCode: userStatus } }
);
process.exit(0);
},
// eslint-disable-next-line no-console
log: (...params) => console.log(...params)
};
const basicIO = {
write: (VALUE) => {
if (!typeof VALUE === 'string') {
// eslint-disable-next-line no-console
console.error(`Expected string but ${typeof VALUE} found!.`);
} else {
const res = VALUE.substring(0, 1 * 1024);
// eslint-disable-next-line no-console
console.log(res);
finalResponse += res;
}
},
getArgument: (KEY) => userData[KEY] || body[KEY],
getAllArguments: () => (userData && Object.keys(userData).length > 0 ? userData : body),
/**
* @deprecated use get getArgument instead.
*/
getParameter: (KEY) => {
// eslint-disable-next-line no-console
console.error(
'CatalystDeprecationWarning: basicIO.getParameter() is deprecated due to usability issues. Please use basicIO.getArgument() method instead.'
);
return basicIO.getArgument(KEY);
},
/**
* @deprecated use get getAllArgument instead.
*/
getAllParameters: () => {
// eslint-disable-next-line no-console
console.error(
'CatalystDeprecationWarning: basicIO.getAllParameters() is deprecated due to usability issues. Please use basicIO.getAllArgument() method instead.'
);
return basicIO.getAllArgument();
},
setStatus: (status) => {
userStatus = status;
}
};
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(context, basicIO);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
writeToFile(
{
error: util.inspect(e)
},
{ response: { statusCode: 500 } }
);
process.exit(0);
}
})
.catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
writeToFile({
error: util.inspect(e),
response: { statusCode: 500 }
});
process.exit(0);
});