0eef574edbb31caa68e075467b74e57897efb411
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

32) 			process.exit(0);
33) 			break;
34) 		}
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

36) 		{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

40) 			process.exit(0);
41) 			break;
42) 		}
43) 		case "translate":
44) 		{
45) 			const language_from : string = args.shift();
46) 			const language_to : string = args.shift();
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

48) 			const result = await services.concept.get_translations
49) 			(
50) 				language_from,
51) 				language_to,
52) 				part
53) 			);
54) 			result.forEach
55) 			(
56) 				(entry) =>
57) 				{
58) 					console.info
59) 					(
60) 						helpers.string.coin
61) 						(
62) 							"[{{language_from}}] {{value_from}} ~ [{{language_to}}] {{value_to}}",
63) 							{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

67) 								"value_to": entry["value_to"],
68) 							}
69) 						)
70) 					);
71) 				}
72) 			);
73) 			process.exit(0);
74) 			break;
75) 		}
76) 		case "export":
77) 		{
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

82) 			{
83) 				const path : string = helpers.string.coin
84) 				(
85) 					"{{directory}}/concept_{{id}}.json",
86) 					{
87) 						"directory": directory,
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

89) 					}
90) 				);
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

97) 			process.exit(0);
98) 			break;
99) 		}
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

101) 		{
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

104) 			result.forEach
105) 			(
106) 				(entry) =>
107) 				{
108) 					console.info
109) 					(
110) 						helpers.string.coin
111) 						(
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

113) 							{
114) 								"id": entry["id"].toFixed(),
115) 								"type": entry["type"],
116) 								"description": (entry["description"] ?? '-'),
117) 								"tags": entry["tags"].join(","),
Christian Fraß [mod] makefile

Christian Fraß authored 3 years ago

118) 								"expressions": (
119) 									() =>
120) 									{
121) 										let parts : Array<string> = [];
122) 										for (const [key, value] of Object.entries(entry["expressions"]))
123) 										{
124) 											parts.push(key + ":" + value.join("/"));
125) 										}
126) 										return parts.join(",");
127) 									}
128) 								) (),
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

129) 							}
130) 						)
131) 					);
132) 				}
133) 			);
134) 			process.exit(0);
135) 			break;
136) 		}
137) 		case "feed":
138) 		{
139) 			const content : string = await helpers.misc.stdin();
140) 			const data : any = JSON.parse(content);
141) 			const concept_thing : any = data;
142) 			const concept_id : int = await services.concept.suck(concept_thing);
143) 			console.info(concept_id);
144) 			process.exit(0);
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

145) 			break;
146) 		}
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

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

Christian Fraß authored 3 years ago

148) 		{
149) 			if (args.length < 1)
150) 			{
151) 				syntaxerror();
152) 			}
153) 			else
154) 			{
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

155) 				/*
156) 				SYNOPSIS
157) 					add <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _
158) 				
159) 				OPTIONS
160) 					<type>
161) 					-d <description> | --description=<description>
162) 					-t <tag> | --tag=<tag>
163) 					-e <language>:<value> | --expression=<language>:<value>
164) 				 */
165) 				const concept_thing : {type ?: string; description : null|string; tags : Array<string>; expressions : {[language : string] : Array<string>};} = {
166) 					"type": undefined,
167) 					"description": null,
168) 					"tags": [],
169) 					"expressions": {},
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

170) 				};
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

171) 				let positional : {list : Array<string>; index : int;} = {
172) 					"list": ["type"],
173) 					"index": 0,
174) 				};
175) 				while (args.length > 0)
176) 				{
177) 					const arg : string = args.shift();
178) 					switch (arg)
179) 					{
180) 						case "-h":
181) 						{
182) 							console.info("add <type> [-d <description>] [-t <tag>] [-t <tag>] … [-e <language>:<value>] [-e <language>:<value>] _");
183) 							process.exit(0);
184) 							break;
185) 						}
186) 						case "-d":
187) 						{
188) 							concept_thing["description"] = args.shift();
189) 							break;
190) 						}
191) 						case "-t":
192) 						{
193) 							concept_thing["tags"].push(args.shift());
194) 							break;
195) 						}
196) 						case "-e":
197) 						{
198) 							const [language, value] : Array<string> = args.shift().split(":", 2);
199) 							if (! concept_thing["expressions"].hasOwnProperty(language)) {
200) 								concept_thing["expressions"][language] = [];
201) 							}
202) 							concept_thing["expressions"][language].push(value);
203) 							break;
204) 						}
205) 						default:
206) 						{
207) 							if (positional.index < positional.list.length) {
208) 								concept_thing[positional.list[positional.index]] = arg;
209) 								positional.index += 1;
210) 							}
211) 							else {
212) 								throw (new Error("unexpected: " + arg));
213) 							}
214) 							break;
215) 						}
216) 					}
217) 				}
218) // console.info(helpers.json.encode_extended(concept_thing, true, 0));
Christian Fraß [mod] logic

Christian Fraß authored 3 years ago

219) 				const concept_id : int = await services.concept.suck(concept_thing);
220) 				console.info(concept_id);
221) 				process.exit(0);
222) 			}
223) 			break;
224) 		}
225) 		case "set":
226) 		{
227) 			if (args.length < 1)
228) 			{
229) 				syntaxerror();
230) 			}
231) 			else
232) 			{
233) 				const concept_id : int = parseInt(args.shift());
234) 			}
235) 		}
Christian Fraß [add] typescript logic

Christian Fraß authored 3 years ago

236) 		default:
237) 		{
238) 			console.error("unhandled command: " + command);
239) 			process.exit(1);
240) 			break;
241) 		}
242) 	}
243) }
244) 
245) 
Christian Fraß [mod] main

Christian Fraß authored 3 years ago

246) main(process.argv.slice(2))
247) .catch
248) (
249) 	(error) =>
250) 	{
251) 		console.error(error.toString());
252) 	}
253) );