-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
402 lines (376 loc) · 14.9 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env node
/*
clear
npx evalocale create -t 5 -c 4 -m description:string -l cs-CZ -l en-GB -n testify -f csv
npx evalocale add -r 5 -l sk-SR -m variace:string=NIC
*/
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
const $$ = require("./index")({alertsOn: true});
const _langs = new Map();
console.msg = function() {
//console.log("\x1b[33m", ...arguments);
console.log(...arguments);
}
Array.prototype.distinct = function() {
var m = new Map();
this.forEach(e => m.set(e,1));
return [...m.keys()];
}
require("i18n-locales").forEach(l => _langs.set(l.toLowerCase(),l));
program
.version('1.0.0')
.description('Evalocale CLI command');
program.command("create")
.option('-t, --total <count>', 'Number of records to be generated (default 10)')
.option('-c, --chars <length>', 'Length of a text key (default 8)')
.option('-m, --metadata <Array>', 'Metadata attribute to add.', collectMetadata, [])
.option('-l, --language <Array>', 'Language(s) to be created', collectLanguage, [])
.option('-f, --format <string>', 'Output format: either json (default) or csv')
.option('-n, --name <string>', 'The name of the bundle')
.option('-p, --path <string>', 'Directory to save the file to (default evalocale.json or evalocale.csv')
.option('-w, --no-watch', 'Starts watching a CSV file for changes and automatically converts it to a JSON file')
.description('Creates an evalocale file')
.action(function() {
try {
var config = arguments[0];
var config = arguments[0];
var total = Math.round(Number(config.total)) > 0 ? Math.round(Number(config.total)) : 10;
var chars = Math.round(Number(config.chars)) > 0 ? Math.round(Number(config.chars)) : 8;
var metadata = config.metadata;
var language = config.language.length > 0 ? config.language : [$$.system];
var format = ["json","csv"].indexOf(config.format?.toLowerCase().trim()) < 0 ? "json" : config.format;
var name = config.name || "evalocale";
var _path = config.path ? `${config.path}/${name}.${format}` : path.join(process.cwd(), `${name}.${format}`);
var bundle = {
library: {},
metadata: {}
};
var keys = [...Array(total)].map(i => $$.random(chars));
for(let l of language)
{
bundle.library[l] = {};
keys.forEach(function(k){
bundle.library[l][k] = "";
});
}
if(metadata.length > 0) {
var md = {};
for(let m of metadata) {
md[m.name] = m.def;
}
keys.forEach(function(k){
bundle.metadata[k] = md;
});
}
bundle.language = Object.keys(bundle.library)[0];
var content = format == "csv" ? $$.load(bundle).toCSV() : JSON.stringify(bundle, null, "\t");
fs.writeFileSync(_path, content);
modifyConfig(function(config){
config.default = _path;
return config;
});
console.msg(`- evalocale file ${name} created and set as default`);
if(!config.watch && format == "csv") {
watchCSV(_path, name);
}
} catch(e) {
console.error("type npx evalocale create -h for a help", e)
}
});
function collectMetadata(value, metadata) {
const types = ["string", "number", "boolean"]
var name, type, def;
if(value.match(/\=/g)) {
def = value.split(/\=/g)[1];
name = value.split(/\=/g)[0];
if(name.match(/\:/g)) {
type = name.split(/\:/g)[1]?.toLowerCase();
name = name.split(/\:/g)[0];
} else {
type = "string";
}
}
else {
if(value.match(/\:/g)) {
name = value.split(/\:/g)[0];
type = value.split(/\:/g)[1];
} else {
name = value;
type = "string";
}
}
if(name?.trim().length == 0) throw "Metadata name cannot be empty";
name = name.trim();
if(["_id"].indexOf(name) > -1) throw `Metadata ${name} is a reserved attribute, use another one.`;
if(["string", "number", "boolean"].indexOf(type) < 0) throw `Unknown type ${type} for property ${name}`;
if(type == "number" && def !== undefined) def = Number(def);
if(type == "boolean" && def !== undefined) def = def == "true" ? true : def == "false" ? false : undefined;
if(type == "string" && def === undefined) def = "";
metadata.push({name: name, type: type, def: def});
return metadata;
}
function collectLanguage(value = "", language) {
var standardized = _langs.get(value.trim().toLowerCase());
if(!standardized) {
console.warn(`Language ${value} was not found in the language list. Consider using standard language code.`)
language.push(value)
} else language.push(standardized);
return language;
}
program.command("add")
.option('-r, --rows <count>', 'Number of records to be added (default 1)')
.option('-c, --chars <length>', 'Length of a text key (default: an average lenght of existing keys)')
.option('-m, --metadata <Array>', 'Metadata attribute(s) to add.', collectMetadata, [])
.option('-l, --language <Array>', 'Language(s) to add', collectLanguage, [])
.option('-s, --source <string>', 'Path to the file to be modified')
.description('Creates an evalocale file')
.action(function() {
try {
var config = arguments[0];
modifyFile(config.source, function(content){
var rows = Math.round(Number(config.rows || 0)) > 0 ? Math.round(Number(config.rows)) : 0;
var chars = Math.round(Number(config.chars || 0)) > 0 ? Math.round(Number(config.chars)) : (function(){
var l = Object.keys(content.library || {})[0];
if(l) {
var ls = Object.keys(content.library[l]).map(k => k.length);
if(ls.length > 0) return ls.reduce((a,b) => a+b)/ls.length;
else return 8;
} else return 8
})();
var metadata = config.metadata || [];
var keys = [...getCurrentKeys(content), ...[...Array(rows)].map(e => $$.random(chars))].distinct();
var langs = new Map();
[...Object.keys(content.library), ...config.language].forEach(k => langs.set(k,1));
[...langs.keys()].forEach(function(l){
if(!content.library[l]) content.library[l] = {};
keys.forEach(k => !content.library[l][k] ? content.library[l][k] = "" : true);
})
if(content.metadata) {
var schema = {};
getCurrentMetaAttrs(content).forEach((m) => schema[m] = "");
keys.forEach(k => !content.metadata[k] ? content.metadata[k] = schema : true);
}
if(config.metadata.length > 0 && rows > 0) {
for(let m of config.metadata) {
Object.keys(content.metadata).forEach(function(mk){
content.metadata[mk][m.name] = m.default;
});
}
}
console.msg(`- elements add to ${config.source}`);
return content;
});
} catch(e) {
console.error("type npx evalocale create -h for a help", e)
console.error(e);
}
});
program.command("clean")
.option('-s, --source <string>', 'Path to the file to be cleaned')
.option('-i, --no-independent', 'Clean independently (remove empty no matter they have assigned a non-empty value in another dictionary/metadata.')
.description('Clean empty keys across dictionaries/metadata (where there is no assigned value to the key in all the resources).')
.action(function() {
try {
var config = arguments[0];
modifyFile(config.source, function(content){
if(!config.independent) {
Object.keys(_library || {}).forEach(function(lkey){
Object.keys(_library[lkey]).forEach(function(ik){
if(_library[lkey][ik] == "") delete _library[lkey][ik];
});
});
} else {
var keys = Object.keys(getBundleKeys());
for(let k of keys)
{
var onlyEmpty = true;
for(let l in (_library || {})) {
if(_library[l]?.[k] != "") onlyEmpty = false;
}
if(onlyEmpty){
for(let l in _library || {}) {
delete _library[l]?.[k];
}
}
}
}
return this;
console.msg(`- elements add to ${config.source}`);
return content;
});
} catch(e) {
console.error("type npx evalocale create -h for a help", e)
console.error(e);
}
});
// INIT
program
.command('init')
.description('Initialize the CLI processes')
.action(() => {
init();
});
// INSTALL
program
.command('install <what>')
.description('Install an Evalocale extension')
.action((what) => {
if(what?.toLowerCase() == "express") {
try {
var destination = path.join(process.cwd(), 'views');
fs.exists(destination, (exists) => {
if (!exists) {
fs.mkdir(destination, (err) => {
if (err) {
console.error('Error creating directory:', err);
} else {
copyFiles();
}
});
} else {
copyFiles();
}
});
function copyFiles(){
const sourceFilePath = path.join(__dirname, "resources", "evalocale.pug");
const targetFilePath = path.join(process.cwd(), 'views', 'evalocale.pug');
fs.copyFileSync(sourceFilePath, targetFilePath);
console.msg("- Express.js extension initialized, see https://github.com/muzikp/evalocale/blob/main/docs/express.md for further information.")
}
} catch (e) {
}
}
else console.log("- unknown service: " + what)
});
function watchCSV(filePath) {
console.msg(`- watching file ${filePath} for changes`);
fs.watchFile(filePath, (curr, prev) => {
var _$$ = require("./index.js")();
if (curr.mtime > prev.mtime) {
try {
var csv = fs.readFileSync(filePath).toString();
var json = _$$.fromCSV(csv, {delimiter: ";"}).save();
const directory = path.dirname(filePath);
const file = path.parse( path.basename(filePath)).name;
var _path = `${directory}/${file}..json`;
fs.writeFileSync(_path, JSON.stringify(json, null, "\t"));
updateWatch(filePath);
} catch(e) {
console.warn(e);
}
}
});
}
function init(){
try {
var cfPath = path.join(__dirname, 'evalocale.config.json');
fs.exists(cfPath, (exists) => {
var config = {
watch: []
};
if (exists) config = JSON.parse(fs.readFileSync(cfPath).toString());
for(let w of config.watch) {
try {
watchCSV(w);
} catch(e) {
config.watch = config.watch.filter(_w => _w !== w);
}
}
fs.writeFileSync(cfPath, JSON.stringify(config))``
});
} catch (e) {
}
}
function updateWatch(filePath) {
var cfPath = path.join(__dirname, 'evalocale.config.json');
fs.exists(cfPath, (exists) => {
var config = {
watch: []
};
if (exists) config = JSON.parse(fs.readFileSync(cfPath).toString());
config.watch.push(filePath);
fs.writeFileSync(cfPath, JSON.stringify(config));
});
}
function modifyFile(filePath, action) {
if(!filePath) {
try {
var _default = JSON.parse(fs.readFileSync(path.join(__dirname, 'evalocale.config.json')).toString()).default;
if(_default) filePath = _default;
} catch(e) {
console.error(e);
}
}
//if(!config.source) throw "Source argument cannot be emty.";
var format = path.extname(filePath).replace(/\./g,"");
var content;
try {
if(format == "csv")
{
content = $$.fromCSV(fs.readFileSync(filePath).toString()).save();
}
else if(format == "json")
{
content = JSON.parse(fs.readFileSync(filePath));
}
else throw ("Unsupported file format: " + format);
} catch(e) {
console.error(e);
}
if(typeof content == "object") {
content = action(content);
if(format == "csv") fs.writeFileSync(filePath, $$.load(content).toCSV());
else fs.writeFileSync(filePath, JSON.stringify(content, null, "\t"));
} else
{
console.log("The content is not an object", content);
}
}
function modifyConfig(action = function(config){return config}) {
try {
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'evalocale.config.json')).toString());
config = action(config);
fs.writeFileSync(path.join(__dirname, 'evalocale.config.json'), JSON.stringify(config, null, "\t"));
} catch(e) {
console.log("Failed to modify config: ", e);
}
}
function getCurrentKeys(bundle) {
var map = new Map();
for(let l of Object.keys(bundle?.analysis || {}))
{
Object.keys(bundle.analysis[l]).forEach(k => map.set(k,1));
}
Object.keys(bundle?.metadata || {}).forEach(k => map.set(k,1));
return [...map.keys()];
}
function getCurrentMetaAttrs(bundle) {
var map = new Map();
Object.keys(bundle?.metadata || {}).forEach(function(key){
Object.keys(bundle?.metadata?.[key]).forEach(k => map.set(k,1));
})
return [...map.keys()];
}
function getBundleKeys(bundle) {
var map = new Map();
Object.keys((bundle.metadata || {})).forEach(function(key){
map.set(key,1)
});
Object.keys(bundle.library || {}).forEach(function(lang){
Object.keys(bundle.library[lang]).forEach(function(key){
map.set(key, 1);
});
});
return mapToObject(map);
}
function mapToObject(m) {
var obj = {};
[...m].forEach(_ => obj[_[0]] = _[1]);
return obj;
}
program.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}