/* 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 "run": { const server : lib_server.class_server = new lib_server.class_server ( 7777, ns_server.query_http_raw ); server.start(); break; } case "show": { const concept_id = parseInt(args.shift()); const exposal : services.concept.type_exposal = await services.concept.expose(concept_id); console.info(helpers.json.encode_extended(exposal, true, 0)); 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_(); const digits : int = 3; for (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), digits, "0"), } ); delete exposal.id; const content : string = helpers.json.encode_extended(exposal, true, 0); await helpers.file.write(path, content); // console.info(path); } 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 "add": { if (args.length < 1) { syntaxerror(); } else { /* SYNOPSIS add [-d ] [-t ] [-t ] … [-e :] [-e :] _ OPTIONS -d | --description= -t | --tag= -e : | --expression=: */ const concept_thing : {type ?: string; description : null|string; tags : Array; expressions : {[language : string] : Array};} = { "type": undefined, "description": null, "tags": [], "expressions": {}, }; let positional : {list : Array; index : int;} = { "list": ["type"], "index": 0, }; while (args.length > 0) { const arg : string = args.shift(); switch (arg) { case "-h": { console.info("add [-d ] [-t ] [-t ] … [-e :] [-e :] _"); process.exit(0); break; } case "-d": { concept_thing["description"] = args.shift(); break; } case "-t": { concept_thing["tags"].push(args.shift()); break; } case "-e": { const [language, value] : Array = args.shift().split(":", 2); if (! concept_thing["expressions"].hasOwnProperty(language)) { concept_thing["expressions"][language] = []; } concept_thing["expressions"][language].push(value); break; } default: { if (positional.index < positional.list.length) { concept_thing[positional.list[positional.index]] = arg; positional.index += 1; } else { throw (new Error("unexpected: " + arg)); } break; } } } // console.info(helpers.json.encode_extended(concept_thing, true, 0)); 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)) .catch ( (error) => { console.error(error.toString()); } );