714b5b0a962115fe16ec65d616035e41c74e2c30
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

1) /*
2) import {repositories.language} from './repository-language';
3) import {repositories.type} from './repository-type';
4) import {repositories.tag} from './repository-tag';
5) import {repositories.concept} from './repository-concept';
6)  */
7) 
8) 
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

9) function syntaxerror() : void
10) {
11) 	console.error("-- wrong syntax");
12) 	process.exit(1);
13) }
14) 
15) 
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

16) /**
17)  */
18) async function main(args : Array<string>) : Promise<void>
19) {
20) 	const command : string = args.shift();
21) 	switch (command)
22) 	{
23) 		case "setup":
24) 		{
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

25) 			await repositories.language.setup();
26) 			await repositories.type.setup();
27) 			await repositories.tag.setup();
28) 			await repositories.expression.setup();
29) 			await repositories.concept.setup();
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

30) 			process.exit(0);
31) 			break;
32) 		}
Christian Fraß [add] api

Christian Fraß authored 3 years ago

33) 		case "run":
34) 		{
35) 			const server : lib_comm.interface_server<string, lib_comm.type_response_server_http> = new lib_comm.class_server_http
36) 			(
37) 				{
38) 					"port": 7777,
39) 				}
40) 			);
41) 			server.setup
42) 			(
43) 				async (input_raw) =>
44) 				{
45) 					const stuff : any = JSON.parse(input_raw);
46) 					try
47) 					{
48) 						const output : any = await api.query(stuff.action, stuff.input);
49) 						return Promise.resolve<lib_comm.type_response_server_http>
50) 						(
51) 							{
52) 								"code": 200,
53) 								"text": JSON.stringify(output),
54) 							}
55) 						);
56) 					}
57) 					catch (exception)
58) 					{
59) 						console.error(exception);
60) 						return Promise.resolve<lib_comm.type_response_server_http>
61) 						(
62) 							{
63) 								"code": 500,
64) 								"text": 'error',
65) 							}
66) 						);
67) 					}
68) 				}
69) 			);
70) 			server.run();
71) 			break;
72) 		}
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

73) 		case "show":
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

74) 		{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

75) 			const concept_id = parseInt(args.shift());
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

76) 			const exposal : services.concept.type_exposal = await services.concept.expose(concept_id);
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

77) 			console.info(helpers.json.encode_extended(exposal, true, 0));
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

78) 			process.exit(0);
79) 			break;
80) 		}
81) 		case "translate":
82) 		{
83) 			const language_from : string = args.shift();
84) 			const language_to : string = args.shift();
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

85) 			const part : string = args.join(" "); args = [];
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

86) 			const result = await services.concept.get_translations
87) 			(
88) 				language_from,
89) 				language_to,
90) 				part
91) 			);
92) 			result.forEach
93) 			(
94) 				(entry) =>
95) 				{
96) 					console.info
97) 					(
98) 						helpers.string.coin
99) 						(
100) 							"[{{language_from}}] {{value_from}} ~ [{{language_to}}] {{value_to}}",
101) 							{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

102) 								"language_from": entry["language_from"],
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

103) 								"value_from": entry["value_from"],
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

104) 								"language_to": entry["language_to"],
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

105) 								"value_to": entry["value_to"],
106) 							}
107) 						)
108) 					);
109) 				}
110) 			);
111) 			process.exit(0);
112) 			break;
113) 		}
114) 		case "export":
115) 		{
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

116) 			const directory : string = ((args.length >= 1) ? args.shift() : "/tmp");
117) 			const result : Array<services.concept.type_exposal> = await services.concept.export_();
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

118) 			const digits : int = 3;
119) 			for (const exposal of result)
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

120) 			{
121) 				const path : string = helpers.string.coin
122) 				(
123) 					"{{directory}}/concept_{{id}}.json",
124) 					{
125) 						"directory": directory,
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

126) 						"id": helpers.string.pad_left(exposal.id.toFixed(0), digits, "0"),
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

127) 					}
128) 				);
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

129) 				delete exposal.id;
130) 				const content : string = helpers.json.encode_extended(exposal, true, 0);
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

131) 				await helpers.file.write(path, content);
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

132) 				// console.info(path);
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

133) 			}
134) 			console.info(directory);
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

135) 			process.exit(0);
136) 			break;
137) 		}
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

138) 		case "search":
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

139) 		{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

140) 			const part : string = args.join(" "); args = [];
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

141) 			const result : Array<services.concept.type_exposal> = await services.concept.search(part);
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

142) 			result.forEach
143) 			(
144) 				(entry) =>
145) 				{
146) 					console.info
147) 					(
148) 						helpers.string.coin
149) 						(
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

150) 							"{{id}} | {{type}} | {{description}} | {{tags}} | {{expressions}}",
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

151) 							{
152) 								"id": entry["id"].toFixed(),
153) 								"type": entry["type"],
154) 								"description": (entry["description"] ?? '-'),
155) 								"tags": entry["tags"].join(","),
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

156) 								"expressions": (
157) 									() =>
158) 									{
159) 										let parts : Array<string> = [];
160) 										for (const [key, value] of Object.entries(entry["expressions"]))
161) 										{
162) 											parts.push(key + ":" + value.join("/"));
163) 										}
164) 										return parts.join(",");
165) 									}
166) 								) (),
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

167) 							}
168) 						)
169) 					);
170) 				}
171) 			);
172) 			process.exit(0);
173) 			break;
174) 		}
175) 		case "feed":
176) 		{
177) 			const content : string = await helpers.misc.stdin();
178) 			const data : any = JSON.parse(content);
179) 			const concept_thing : any = data;
180) 			const concept_id : int = await services.concept.suck(concept_thing);
181) 			console.info(concept_id);
182) 			process.exit(0);
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

183) 			break;
184) 		}
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

185) 		case "add":
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

186) 		{
187) 			if (args.length < 1)
188) 			{
189) 				syntaxerror();
190) 			}
191) 			else
192) 			{
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

193) 				/*
194) 				SYNOPSIS
195) 					add <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _
196) 				
197) 				OPTIONS
198) 					<type>
199) 					-d <description> | --description=<description>
200) 					-t <tag> | --tag=<tag>
201) 					-e <language>:<value> | --expression=<language>:<value>
202) 				 */
203) 				const concept_thing : {type ?: string; description : null|string; tags : Array<string>; expressions : {[language : string] : Array<string>};} = {
204) 					"type": undefined,
205) 					"description": null,
206) 					"tags": [],
207) 					"expressions": {},
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

208) 				};
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

209) 				let positional : {list : Array<string>; index : int;} = {
210) 					"list": ["type"],
211) 					"index": 0,
212) 				};
213) 				while (args.length > 0)
214) 				{
215) 					const arg : string = args.shift();
216) 					switch (arg)
217) 					{
218) 						case "-h":
219) 						{
220) 							console.info("add <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _");
221) 							process.exit(0);
222) 							break;
223) 						}
224) 						case "-d":
225) 						{
226) 							concept_thing["description"] = args.shift();
227) 							break;
228) 						}
229) 						case "-t":
230) 						{
231) 							concept_thing["tags"].push(args.shift());
232) 							break;
233) 						}
234) 						case "-e":
235) 						{
236) 							const [language, value] : Array<string> = args.shift().split(":", 2);
237) 							if (! concept_thing["expressions"].hasOwnProperty(language)) {
238) 								concept_thing["expressions"][language] = [];
239) 							}
240) 							concept_thing["expressions"][language].push(value);
241) 							break;
242) 						}
243) 						default:
244) 						{
245) 							if (positional.index < positional.list.length) {
246) 								concept_thing[positional.list[positional.index]] = arg;
247) 								positional.index += 1;
248) 							}
249) 							else {
250) 								throw (new Error("unexpected: " + arg));
251) 							}
252) 							break;
253) 						}
254) 					}
255) 				}
256) // console.info(helpers.json.encode_extended(concept_thing, true, 0));
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

257) 				const concept_id : int = await services.concept.suck(concept_thing);
258) 				console.info(concept_id);
259) 				process.exit(0);
260) 			}
261) 			break;
262) 		}
263) 		case "set":
264) 		{
265) 			if (args.length < 1)
266) 			{
267) 				syntaxerror();
268) 			}
269) 			else
270) 			{
271) 				const concept_id : int = parseInt(args.shift());
272) 			}
273) 		}
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

274) 		default:
275) 		{
276) 			console.error("unhandled command: " + command);
277) 			process.exit(1);
278) 			break;
279) 		}
280) 	}
281) }
282) 
283) 
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

284) main(process.argv.slice(2))
285) .catch
286) (
287) 	(error) =>
288) 	{
289) 		console.error(error.toString());
290) 	}
291) );