nesoi 3.3.7 → 3.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,422 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = script;
4
+ const string_1 = require("../util/string");
5
+ /**
6
+ * @category Engine
7
+ * @subcategory CLI
8
+ */
9
+ function script(name, def) {
10
+ const builder = def(new ScriptBuilder(name));
11
+ const schema = ScriptBuilder.build(builder);
12
+ return new Script(schema);
13
+ }
14
+ //
15
+ class Script {
16
+ constructor(schema) {
17
+ this.schema = schema;
18
+ this.args = {};
19
+ }
20
+ init() {
21
+ if (process.argv.includes('-h') || process.argv.includes('--help')) {
22
+ this.log_help();
23
+ process.exit();
24
+ }
25
+ this.parse();
26
+ return this.args;
27
+ }
28
+ log_help() {
29
+ this.log_header();
30
+ this.log_syntax();
31
+ const pos_args = this.schema.args.filter(arg => arg.type === 'pos');
32
+ if (pos_args.length) {
33
+ console.log('Positional Arguments:\n');
34
+ for (const arg of pos_args) {
35
+ let str = ' ';
36
+ str += (0, string_1.colored)(arg.name, 'brown');
37
+ str += '\n ';
38
+ if (arg.values[0].amount === '1?') {
39
+ str += (0, string_1.colored)('(optional) ', 'blue');
40
+ }
41
+ else if (arg.values[0].amount === '0+') {
42
+ str += (0, string_1.colored)('(zero or more) ', 'blue');
43
+ }
44
+ else if (arg.values[0].amount === '1+') {
45
+ str += (0, string_1.colored)('(one or more) ', 'blue');
46
+ }
47
+ str += (0, string_1.colored)(arg.description ?? '', 'blue');
48
+ str += '\n';
49
+ if (arg.values[0].type === 'enum') {
50
+ for (const opt in arg.values[0].meta.enum.options) {
51
+ const option = arg.values[0].meta.enum.options[opt];
52
+ str += ' - ' + (0, string_1.colored)(opt, 'lightblue') + ' ' + (0, string_1.colored)(option.description ? (': ' + option.description) : '', 'blue');
53
+ str += '\n';
54
+ }
55
+ }
56
+ console.log(str);
57
+ }
58
+ }
59
+ this.log_flags();
60
+ }
61
+ log_header() {
62
+ console.log();
63
+ console.log((0, string_1.colored)(this.schema.name, 'lightpurple'));
64
+ if (this.schema.description) {
65
+ console.log((0, string_1.colored)(this.schema.description, 'purple'));
66
+ }
67
+ console.log();
68
+ }
69
+ log_syntax() {
70
+ const syntax = this.schema.args.map(arg => {
71
+ const values = [];
72
+ for (const val of arg.values) {
73
+ switch (val.amount) {
74
+ case '1':
75
+ values.push(val.name);
76
+ break;
77
+ case '1?':
78
+ values.push(val.name + '?');
79
+ break;
80
+ case '0+':
81
+ values.push(val.name + '0..' + val.name + '+');
82
+ break;
83
+ case '1+':
84
+ values.push(val.name + '1..' + val.name + '+');
85
+ break;
86
+ }
87
+ }
88
+ let p = arg.name;
89
+ if (arg.type === 'pos') {
90
+ p = `{${values[0]}}`;
91
+ }
92
+ else if (arg.type === 'key') {
93
+ p = `[${p}${values.length ? (' ' + values.join(' ')) : ''}]`;
94
+ }
95
+ return p;
96
+ }).join(' ');
97
+ console.log(`Syntax:\n\n${(0, string_1.colored)(this.schema.name + ' ' + syntax, 'brown')}`);
98
+ console.log();
99
+ }
100
+ log_flags() {
101
+ const key_args = this.schema.args.filter(arg => arg.type === 'key');
102
+ console.log('Keyword Arguments:\n');
103
+ console.log(` ${(0, string_1.colored)('-h --help', 'cyan')}?\n ${(0, string_1.colored)('Show this help info', 'blue')}\n`);
104
+ for (const arg of key_args) {
105
+ const values = [];
106
+ for (const val of arg.values) {
107
+ switch (val.amount) {
108
+ case '1':
109
+ values.push(val.name);
110
+ break;
111
+ case '1?':
112
+ values.push(val.name + '?');
113
+ break;
114
+ case '0+':
115
+ values.push(val.name + '0..' + val.name + '+');
116
+ break;
117
+ case '1+':
118
+ values.push(val.name + '1..' + val.name + '+');
119
+ break;
120
+ }
121
+ }
122
+ let str = ' ';
123
+ if (arg.short) {
124
+ str += (0, string_1.colored)(arg.short + ' ' + arg.name, 'brown');
125
+ }
126
+ else {
127
+ str += (0, string_1.colored)(arg.name, 'brown');
128
+ }
129
+ for (const value of values) {
130
+ str += ' ' + (0, string_1.colored)(value, 'green');
131
+ }
132
+ str += '\n ';
133
+ str += (0, string_1.colored)(arg.description ?? '', 'blue');
134
+ str += '\n ';
135
+ for (let i = 0; i < values.length; i++) {
136
+ str += (0, string_1.colored)(values[i], 'green');
137
+ if (arg.values[i].amount === '1?') {
138
+ str += (0, string_1.colored)(': (optional)', 'cyan');
139
+ }
140
+ else if (arg.values[i].amount === '0+') {
141
+ str += (0, string_1.colored)(': (zero or more)', 'cyan');
142
+ }
143
+ else if (arg.values[i].amount === '1+') {
144
+ str += (0, string_1.colored)(': (one or more)', 'cyan');
145
+ }
146
+ if (arg.values[i].description) {
147
+ str += (0, string_1.colored)(` ${arg.values[i].description}`, 'cyan');
148
+ }
149
+ str += '\n ';
150
+ }
151
+ console.log(str);
152
+ }
153
+ console.log();
154
+ }
155
+ error(error, type) {
156
+ this.log_header();
157
+ if (type === 'syntax') {
158
+ this.log_syntax();
159
+ }
160
+ else {
161
+ this.log_flags();
162
+ }
163
+ console.log((0, string_1.colored)('---\n' + error + '\n---', 'red'));
164
+ console.log();
165
+ process.exit();
166
+ }
167
+ parse() {
168
+ const argv = process.argv;
169
+ let i = 2;
170
+ // Positional
171
+ for (const arg of this.schema.args) {
172
+ if (arg.type !== 'pos')
173
+ break;
174
+ const { i: j, value } = this.parse_argv(arg, arg.values[0], i);
175
+ this.args[arg.name] = value;
176
+ i = j;
177
+ }
178
+ // Keyword
179
+ let arg;
180
+ while (i < argv.length) {
181
+ if (!argv[i].startsWith('-')) {
182
+ this.error(`Error: Unexpected argument(s) at the end '${argv.slice(i).join(' ')}'`, 'syntax');
183
+ }
184
+ arg = this.schema.args.find(arg => arg.name === argv[i] || arg.short === argv[i]);
185
+ if (!arg) {
186
+ this.error(`Error: Unknown keyword argument '${argv[i]}'`, 'flag');
187
+ break;
188
+ }
189
+ const name = arg.name.replace(/-+(.*)/, '$1');
190
+ if (arg.values.length) {
191
+ this.args[name] = {};
192
+ i++;
193
+ for (const val of arg.values) {
194
+ const { i: j, value } = this.parse_argv(arg, val, i);
195
+ i = j;
196
+ this.args[name][val.name] = value;
197
+ }
198
+ }
199
+ else {
200
+ this.args[name] = true;
201
+ i++;
202
+ }
203
+ }
204
+ // for (const arg of this.schema.args) {
205
+ // if (arg.type !== 'key') continue;
206
+ // if (i >= argv.length) break;
207
+ // }
208
+ }
209
+ parse_argv(arg, value, i) {
210
+ const argv = process.argv;
211
+ const values = [];
212
+ // Single
213
+ if (value.amount === '1' || value.amount === '1?') {
214
+ if (argv[i] && !argv[i].startsWith('-')) {
215
+ values.push(argv[i]);
216
+ i++;
217
+ }
218
+ }
219
+ // Many
220
+ else {
221
+ while (i < argv.length) {
222
+ if (argv[i].startsWith('-'))
223
+ break;
224
+ values.push(argv[i]);
225
+ i++;
226
+ }
227
+ }
228
+ if (values.length === 0 && (value.amount === '1' || value.amount === '1+')) {
229
+ this.error(`Error: Missing required ${arg.type === 'pos' ? 'positional argument' : ('value from arg \'' + arg.name + '\' named')} '${value.name}'`, 'syntax');
230
+ }
231
+ const validated = values.map(val => this.validate(value, val));
232
+ // Single
233
+ if (value.amount === '1' || value.amount === '1?') {
234
+ return { value: validated[0], i };
235
+ }
236
+ else {
237
+ return { value: validated, i };
238
+ }
239
+ }
240
+ validate(schema, value) {
241
+ switch (schema.type) {
242
+ case 'string':
243
+ return value;
244
+ break;
245
+ case 'boolean':
246
+ if (value.toLowerCase() === 'true' || value === '1' || value.toLowerCase() === 'yes')
247
+ return true;
248
+ return false;
249
+ break;
250
+ case 'enum':
251
+ if (!(value in schema.meta.enum.options)) {
252
+ this.error(`Error: Value '${schema.name}=${value}' doesn't match any of the options: ${Object.keys(schema.meta.enum.options).join(', ')}`, 'syntax');
253
+ }
254
+ return value;
255
+ break;
256
+ }
257
+ }
258
+ }
259
+ class ScriptBuilder {
260
+ constructor(name) {
261
+ this.schema = {
262
+ args: []
263
+ };
264
+ this.schema.name = name;
265
+ }
266
+ d(description) {
267
+ this.schema.description = description;
268
+ return this;
269
+ }
270
+ arg(name, arg1, arg2) {
271
+ const def = arg2 || arg1;
272
+ const short = typeof arg1 === 'string' ? arg1 : undefined;
273
+ let schema;
274
+ if (name.startsWith('-')) {
275
+ const builder = new ScriptKeyArgBuilder(name, short);
276
+ def(builder);
277
+ schema = ScriptKeyArgBuilder.build(builder);
278
+ }
279
+ else {
280
+ const builder = new ScriptPosArgBuilder(name);
281
+ def(builder);
282
+ schema = ScriptPosArgBuilder.build(builder);
283
+ }
284
+ this.schema.args?.push(schema);
285
+ return this;
286
+ }
287
+ static build(builder) {
288
+ const args = builder.schema.args;
289
+ for (let i = 1; i < args.length; i++) {
290
+ if (args[i].type !== 'pos')
291
+ break;
292
+ const lastArg = args[i - 1];
293
+ if (lastArg.values[0].amount === '0+' || lastArg.values[0].amount === '1+') {
294
+ throw new Error('Only a single 0+/1+ positional argument is allowed, as the last one.');
295
+ }
296
+ }
297
+ builder.schema.args = [
298
+ ...args.filter(arg => arg.type === 'pos' && arg.values[0].amount === '1'),
299
+ ...args.filter(arg => arg.type === 'pos' && arg.values[0].amount !== '1'),
300
+ ...args.filter(arg => arg.type === 'key')
301
+ ];
302
+ return builder.schema;
303
+ }
304
+ }
305
+ class ScriptPosArgBuilder {
306
+ constructor(name) {
307
+ this.schema = {};
308
+ this.schema.name = name;
309
+ this.schema.type = 'pos';
310
+ this.schema.values = [{
311
+ name,
312
+ type: 'string',
313
+ amount: '1',
314
+ meta: {}
315
+ }];
316
+ }
317
+ d(description) {
318
+ this.schema.description = description;
319
+ return this;
320
+ }
321
+ value(def) {
322
+ const builder = new ScriptValueBuilder(this.schema.name);
323
+ def(builder);
324
+ const value = builder.build();
325
+ this.schema.values = [value];
326
+ return this;
327
+ }
328
+ static build(builder) {
329
+ return builder.schema;
330
+ }
331
+ }
332
+ class ScriptKeyArgBuilder {
333
+ constructor(name, short) {
334
+ this.schema = {};
335
+ this.schema.name = name;
336
+ this.schema.short = short;
337
+ this.schema.type = 'key';
338
+ this.schema.values = [];
339
+ }
340
+ d(description) {
341
+ this.schema.description = description;
342
+ return this;
343
+ }
344
+ value(name, def) {
345
+ const lastValue = this.schema.values.at(-1);
346
+ if (lastValue?.amount === '0+' || lastValue?.amount === '1+') {
347
+ throw new Error('Only a single 0+/1+ value is allowed, as the last one.');
348
+ }
349
+ const builder = new ScriptValueBuilder(name);
350
+ def(builder);
351
+ const value = builder.build();
352
+ this.schema.values.push(value);
353
+ return this;
354
+ }
355
+ static build(builder) {
356
+ builder.schema.values = [
357
+ ...builder.schema.values.filter(value => value.amount === '1'),
358
+ ...builder.schema.values.filter(value => value.amount !== '1'),
359
+ ];
360
+ return builder.schema;
361
+ }
362
+ }
363
+ class ScriptValueBuilder {
364
+ constructor(name) {
365
+ this.schema = {
366
+ name,
367
+ type: 'string',
368
+ amount: '1',
369
+ meta: {}
370
+ };
371
+ }
372
+ d(description) {
373
+ this.schema.description = description;
374
+ return this;
375
+ }
376
+ get optional() {
377
+ if (this.schema.amount === '1') {
378
+ this.schema.amount = '1?';
379
+ }
380
+ return this;
381
+ }
382
+ get zero_or_more() {
383
+ this.schema.amount = '0+';
384
+ return this;
385
+ }
386
+ get one_or_more() {
387
+ this.schema.amount = '1+';
388
+ return this;
389
+ }
390
+ get boolean() {
391
+ this.schema = {
392
+ ...this.schema,
393
+ type: 'boolean',
394
+ meta: {}
395
+ };
396
+ return this;
397
+ }
398
+ enum(options) {
399
+ const opts = {};
400
+ if (Array.isArray(options)) {
401
+ for (const opt of options) {
402
+ opts[opt] = { name: opt };
403
+ }
404
+ }
405
+ else {
406
+ for (const name in options) {
407
+ opts[name] = { name, description: options[name] };
408
+ }
409
+ }
410
+ this.schema = {
411
+ ...this.schema,
412
+ type: 'enum',
413
+ meta: {
414
+ enum: {
415
+ options: opts
416
+ }
417
+ }
418
+ };
419
+ return this;
420
+ }
421
+ build() { return this.schema; }
422
+ }
@@ -72,7 +72,7 @@ class BucketQueryTrxNode {
72
72
  }
73
73
  async first() {
74
74
  const results = await this.wrap('queryFirst', { schema: this.query, view: this.view }, (trx, bucket) => {
75
- return bucket.query(this.trx, this.query, {
75
+ return bucket.query(trx, this.query, {
76
76
  perPage: 1
77
77
  }, this.view, {
78
78
  no_tenancy: !this.enableTenancy,
@@ -1,9 +1,8 @@
1
1
  export type DotEnvFile = Record<string, string>;
2
2
  export declare class DotEnv {
3
- static path: string;
4
- static load(): void;
5
- static parse(): DotEnvFile;
6
- static save(dotenv: DotEnvFile): void;
7
- static get(key: string): string;
8
- static set(key: string, value: string): void;
3
+ static load(filename?: string): void;
4
+ static parse(filename?: string): DotEnvFile;
5
+ static save(dotenv: DotEnvFile, filename?: string): void;
6
+ static get(key: string, filename?: string): string;
7
+ static set(key: string, value: string, filename?: string): void;
9
8
  }
@@ -37,16 +37,18 @@ exports.DotEnv = void 0;
37
37
  const path = __importStar(require("path"));
38
38
  const fs = __importStar(require("fs"));
39
39
  class DotEnv {
40
- static load() {
41
- if (!fs.existsSync(this.path))
40
+ static load(filename = '.env') {
41
+ const filepath = path.join(process.cwd(), filename);
42
+ if (!fs.existsSync(filepath))
42
43
  return;
43
- const file = this.parse();
44
+ const file = this.parse(filename);
44
45
  for (const key in file) {
45
46
  process.env[key] = file[key];
46
47
  }
47
48
  }
48
- static parse() {
49
- const file = fs.readFileSync(this.path, 'utf-8');
49
+ static parse(filename = '.env') {
50
+ const filepath = path.join(process.cwd(), filename);
51
+ const file = fs.readFileSync(filepath, 'utf-8');
50
52
  return file.split('\n').reduce((a, line) => {
51
53
  const p = line.split('=');
52
54
  if (p.length > 1)
@@ -54,19 +56,19 @@ class DotEnv {
54
56
  return a;
55
57
  }, {});
56
58
  }
57
- static save(dotenv) {
59
+ static save(dotenv, filename = '.env') {
60
+ const filepath = path.join(process.cwd(), filename);
58
61
  const file = Object.keys(dotenv).map(p => p + '=' + dotenv[p]).join('\n');
59
- fs.writeFileSync(this.path, file);
62
+ fs.writeFileSync(filepath, file);
60
63
  }
61
- static get(key) {
62
- const dotenv = this.parse();
64
+ static get(key, filename = '.env') {
65
+ const dotenv = this.parse(filename);
63
66
  return dotenv[key];
64
67
  }
65
- static set(key, value) {
66
- const dotenv = this.parse();
68
+ static set(key, value, filename = '.env') {
69
+ const dotenv = this.parse(filename);
67
70
  dotenv[key] = value;
68
71
  this.save(dotenv);
69
72
  }
70
73
  }
71
74
  exports.DotEnv = DotEnv;
72
- DotEnv.path = path.join(process.cwd(), '.env');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesoi",
3
- "version": "3.3.7",
3
+ "version": "3.3.8",
4
4
  "description": "Declarative framework for data-driven applications",
5
5
  "repository": {
6
6
  "type": "git",