ad4m-host
Version:
Self hosting ad4m service
60 lines (59 loc) • 2.03 kB
JavaScript
import { buildAd4mClient, prettify } from './util';
export const command = 'expression [action]';
export const desc = 'Expression related action';
export const builder = (yargs) => yargs
.positional('action', {
type: 'string',
describe: 'Action that should be executed on the expression',
choices: ['get', 'create'],
})
.options({
url: { type: "string", describe: 'url of the expression' },
raw: { type: "boolean", describe: 'Flag to request raw data of the expression' },
content: { type: "string", describe: 'Content when create the expression' },
address: { type: "string", describe: 'Address of language used to create the expression' },
});
export const handler = async (argv) => {
const { server, url, raw, content, address, action } = argv;
const ad4mClient = buildAd4mClient(server);
switch (action) {
case 'get':
await get(ad4mClient, url, raw);
break;
case 'create':
await create(ad4mClient, content, address);
break;
default:
console.info(`Action "${argv.action}" is not defined on expression.`);
break;
}
process.exit();
};
async function get(ad4mClient, url, raw) {
if (raw && url) {
const expression = await ad4mClient.expression.getRaw(url);
prettify(expression);
return;
}
if (url) {
const expression = await ad4mClient.expression.get(url);
prettify(expression);
return;
}
console.info('Expression get action is missiong param <url> <raw>');
}
async function create(ad4mClient, content, address) {
var parsedContent;
try {
parsedContent = JSON.parse(content);
}
catch (e) {
parsedContent = content;
}
if (content && address) {
const expression = await ad4mClient.expression.create(parsedContent, address);
prettify(expression);
return;
}
console.info('Expression create action is missiong param <content> <address>');
}