#!/usr/bin/env node /* import {repositories.language} from './repository-language'; import {repositories.type} from './repository-type'; import {repositories.tag} from './repository-tag'; import {repositories.concept} from './repository-concept'; */ function syntaxerror() : void { console.error("-- wrong syntax"); process.exit(1); } /** */ async function main(args : Array) : Promise { const command : string = args.shift(); switch (command) { case "setup": { await repositories.language.setup(); await repositories.type.setup(); await repositories.tag.setup(); await repositories.expression.setup(); await repositories.concept.setup(); process.exit(0); break; } case "show": { const concept_id = parseInt(args.shift()); const exposal : services.concept.type_exposal = await services.concept.expose(concept_id); console.info(JSON.stringify(exposal, undefined, "\t")); process.exit(0); break; } case "translate": { const language_from : string = args.shift(); const language_to : string = args.shift(); const part : string = args.join(" "); args = []; const result = await services.concept.get_translations ( language_from, language_to, part ); result.forEach ( (entry) => { console.info ( helpers.string.coin ( "[{{language_from}}] {{value_from}} ~ [{{language_to}}] {{value_to}}", { "language_from": entry["language_from"], "value_from": entry["value_from"], "language_to": entry["language_to"], "value_to": entry["value_to"], } ) ); } ); process.exit(0); break; } case "export": { const directory : string = ((args.length >= 1) ? args.shift() : "/tmp"); const result : Array = await services.concept.export_(); for await (const exposal of result) { const path : string = helpers.string.coin ( "{{directory}}/concept_{{id}}.json", { "directory": directory, "id": helpers.string.pad_left(exposal.id.toFixed(0), 5, "0"), } ); const content : string = JSON.stringify(result, undefined, "\t"); await helpers.file.write(path, content); } console.info(directory); process.exit(0); break; } case "search": { const part : string = args.join(" "); args = []; const result : Array = await services.concept.search(part); result.forEach ( (entry) => { console.info ( helpers.string.coin ( "{{id}} | {{type}} | {{description}} | {{tags}} | {{expressions}}", { "id": entry["id"].toFixed(), "type": entry["type"], "description": (entry["description"] ?? '-'), "tags": entry["tags"].join(","), "expressions": ( () => { let parts : Array = []; for (const [key, value] of Object.entries(entry["expressions"])) { parts.push(key + ":" + value.join("/")); } return parts.join(","); } ) (), } ) ); } ); process.exit(0); break; } case "feed": { const content : string = await helpers.misc.stdin(); const data : any = JSON.parse(content); const concept_thing : any = data; const concept_id : int = await services.concept.suck(concept_thing); console.info(concept_id); process.exit(0); break; } case "new": { if (args.length < 1) { syntaxerror(); } else { const type : string = args.shift(); const tags : Array = ( (args.length >= 1) ? args.shift().split(",") : [] ); const description : string = ( (args.length >= 1) ? args.shift() : null ); const concept_thing : any = { "type": type, "description": description, "tags": tags, "expressions": [], }; const concept_id : int = await services.concept.suck(concept_thing); console.info(concept_id); process.exit(0); } break; } case "set": { if (args.length < 1) { syntaxerror(); } else { const concept_id : int = parseInt(args.shift()); } } default: { console.error("unhandled command: " + command); process.exit(1); break; } } } main(process.argv.slice(2));