git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
0eef574
Branches
Tags
develop-client_server
master
typescript
fs-words.git
source
main.ts
[mod] main
Christian Fraß
commited
0eef574
at 2021-03-05 07:46:19
main.ts
Blame
History
Raw
#!/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<string>) : Promise<void> { 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(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<services.concept.type_exposal> = 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<services.concept.type_exposal> = 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<string> = []; 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 <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _ OPTIONS <type> -d <description> | --description=<description> -t <tag> | --tag=<tag> -e <language>:<value> | --expression=<language>:<value> */ const concept_thing : {type ?: string; description : null|string; tags : Array<string>; expressions : {[language : string] : Array<string>};} = { "type": undefined, "description": null, "tags": [], "expressions": {}, }; let positional : {list : Array<string>; index : int;} = { "list": ["type"], "index": 0, }; while (args.length > 0) { const arg : string = args.shift(); switch (arg) { case "-h": { console.info("add <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _"); 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<string> = 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()); } );