bscript-cli 1.0.0

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.
Files changed (47) hide show
  1. package/BScript.js +233 -0
  2. package/Docs/BScript/BSCRIPT-REFERENCE-CLI.md +502 -0
  3. package/Docs/Images/BScript-baner.png +0 -0
  4. package/Docs/Images/BooleanType.png +0 -0
  5. package/Docs/Images/FuncionType.png +0 -0
  6. package/Docs/Images/Logo.png +0 -0
  7. package/Docs/Images/NumberType.png +0 -0
  8. package/Docs/Images/ObjectType.png +0 -0
  9. package/Docs/Images/PromiseType.png +0 -0
  10. package/Docs/Images/RawType.png +0 -0
  11. package/Docs/Images/RefType.png +0 -0
  12. package/Docs/Images/TextType.png +0 -0
  13. package/Docs/README.md +157 -0
  14. package/Docs/REFERENCE.md +602 -0
  15. package/Docs/TECHNICAL.md +1143 -0
  16. package/Docs/TUTORIAL.md +604 -0
  17. package/SRC/BScriptHistory.js +98 -0
  18. package/SRC/CMDPermissions.js +4 -0
  19. package/SRC/utils/utils.js +63 -0
  20. package/TerminalCommandController.js +193 -0
  21. package/bin/bscript.js +94 -0
  22. package/commands/.default/BScript/$arg.js +9 -0
  23. package/commands/.default/BScript/$val.js +19 -0
  24. package/commands/.default/BScript/Input.js +10 -0
  25. package/commands/.default/BScript/bool.js +9 -0
  26. package/commands/.default/BScript/delay.js +10 -0
  27. package/commands/.default/BScript/eval.js +9 -0
  28. package/commands/.default/BScript/import.js +17 -0
  29. package/commands/.default/BScript/js_new.js +9 -0
  30. package/commands/.default/BScript/js_require.js +9 -0
  31. package/commands/.default/BScript/jstype.js +9 -0
  32. package/commands/.default/BScript/num.js +9 -0
  33. package/commands/.default/BScript/object.js +19 -0
  34. package/commands/.default/BScript/print.js +9 -0
  35. package/commands/.default/BScript/raw.js +9 -0
  36. package/commands/.default/BScript/ref.js +65 -0
  37. package/commands/.default/BScript/run-with-await.js +13 -0
  38. package/commands/.default/BScript/run.js +11 -0
  39. package/commands/.default/BScript/script.js +13 -0
  40. package/commands/.default/BScript/str.js +9 -0
  41. package/commands/.default/DebugOnly/clear.js +8 -0
  42. package/commands/.default/DebugOnly/update-commands.js +8 -0
  43. package/commands/.default/help.js +20 -0
  44. package/commands/snapshot.md +421 -0
  45. package/index.js +8 -0
  46. package/package.json +12 -0
  47. package/postinstall.js +12 -0
package/BScript.js ADDED
@@ -0,0 +1,233 @@
1
+ const CMDPermissions = require('./SRC/CMDPermissions')
2
+ const EventEmitter = require('node:events');
3
+ const Runner = require('@beiser/bscript-runner').Runner;
4
+ const { readDirRecursive, delay } = require('./SRC/utils/utils');
5
+ const TCC = require('./TerminalCommandController');
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+
9
+ var runInNewContext = require('vm').runInNewContext;
10
+
11
+ module.exports = class CMD extends EventEmitter{
12
+ constructor(options={}) {
13
+ super();
14
+ // Terminal Command Controller
15
+ this.started = options.started??true;
16
+
17
+ this._commands = { };
18
+ this._commandHistoryLimit = 10;
19
+ this.commandHistory = [];
20
+ this._historyPos = -1;
21
+ this.returnCode = 1;
22
+
23
+ this._perUpdateInterval = null;
24
+ this.perrmissionsKeys={...CMDPermissions, ...options.perrmissionsKeys??{}};
25
+ this.commandsPaths=[__dirname + '/commands', ...options.commandsPaths??[], process.cwd() + "/commands"]
26
+ this.cmdRootPermissions = [...options.cmdRootPermissions??[]];
27
+ this.cmdPermissionsHistory = [[CMDPermissions.default, ...options.historyFirstPermissions??[]], ...options.cmdPermissionsHistory??[]];
28
+ this.commandController = new TCC(
29
+ {
30
+ defaultCommandPrefix: " <help>? ",
31
+ stdinEmmiter: process.stdin,
32
+ CMD: this
33
+ }
34
+ );
35
+ }
36
+ loader = async (func) => {
37
+ this.returnCode = -1
38
+ let _loaderAnimation = async () => {
39
+ var iteractions = 8
40
+ while(iteractions > 0) {
41
+ switch(iteractions % 4) {
42
+ case 4:
43
+ this.commandController.ClearLine();
44
+ this.commandController.CursorTo(0);
45
+ this.commandController.Write("Loading");
46
+ break;
47
+ case 3:
48
+ this.commandController.ClearLine();
49
+ this.commandController.CursorTo(0);
50
+ this.commandController.Write("Loading.");
51
+ break;
52
+ case 2:
53
+ this.commandController.ClearLine();
54
+ this.commandController.CursorTo(0);
55
+ this.commandController.Write("Loading..");
56
+ break;
57
+ case 1:
58
+ this.commandController.ClearLine();
59
+ this.commandController.CursorTo(0);
60
+ this.commandController.Write("Loading...");
61
+ break;
62
+ }
63
+ await delay(200);
64
+ iteractions--;
65
+ }
66
+ }
67
+ var loaderOn = true;
68
+ var loaderContinue = false;
69
+ const startAnimation = async () => {
70
+ loaderOn = true;
71
+ loaderContinue = false;
72
+ while(loaderOn) {
73
+ await _loaderAnimation();
74
+ }
75
+ loaderContinue = true;
76
+ };
77
+ startAnimation();
78
+ const waitForPromise = async (onStop) => {
79
+ loaderOn = false;
80
+ return await new Promise(res=> {
81
+ const interval = setInterval(()=>{
82
+ if(loaderContinue) {
83
+ clearInterval(interval);
84
+ (async () => {
85
+ res(await onStop());
86
+ startAnimation();
87
+ })()
88
+ }
89
+ }, 500)
90
+ });
91
+ }
92
+ const conAnimation = async () => {
93
+ loaderOn = false;
94
+ await new Promise(res=>setInterval(()=>{ if(loaderContinue) { res() } }, 500));
95
+ this.returnCode = 1;
96
+ }
97
+ return await new Promise(res => func( () => conAnimation().then(res), waitForPromise, startAnimation ) )
98
+ }
99
+ updateCommands() {
100
+ this._commands = {};
101
+ const cmdPermission = [...this.cmdPermissionsHistory[this.cmdPermissionsHistory.length-1], ...this.cmdRootPermissions]
102
+
103
+ const commandFiles = this.commandsPaths.filter(fs.existsSync).map(path =>
104
+ readDirRecursive(path)
105
+ .filter((file) => file.endsWith('.js'))
106
+ ).flat(1)
107
+ let uniqueKey = 0;
108
+ for (const file of commandFiles) {
109
+ delete require.cache[file];
110
+ var _module = new module.constructor();
111
+ _module.paths = module.paths;
112
+ var sandbox = {}
113
+ Object.getOwnPropertyNames(global).map((k)=>{
114
+ sandbox[k] = global[k];
115
+ })
116
+ sandbox.require = require
117
+ sandbox.exports = [];
118
+ sandbox.CMDPermissions = this.perrmissionsKeys;
119
+ sandbox.__filename = __filename;
120
+ sandbox.__dirname = __dirname;
121
+ sandbox.__commandDir = path.dirname(file);
122
+ sandbox.__rootDir = require.main.path;
123
+ sandbox.module = _module;
124
+ sandbox.global = {};
125
+
126
+ var content = fs.readFileSync(file, 'utf8');
127
+ runInNewContext(content, sandbox, { filename: file });
128
+
129
+ const command = _module.exports;
130
+ if((command.perms == undefined ? true : cmdPermission.some(currentPerm => command.perms.includes(currentPerm))) && !cmdPermission.some(currentPerm => command.excludePerms ? command.excludePerms.includes(currentPerm) : false))
131
+ if(Array.isArray(command.name)) {
132
+ command.name.map(commandName => this.createCommand({ name: commandName, execute: command.execute, description: command.description, syntax: command.syntax, uniqueKey: uniqueKey}));
133
+ } else {
134
+ this.createCommand({ name: command.name, execute: command.execute, description: command.description, syntax: command.syntax, uniqueKey: uniqueKey});
135
+ }
136
+ uniqueKey += 1;
137
+ }
138
+ }
139
+ pushToCommandHistory(cmd) {
140
+ if(this.commandHistory[this.commandHistory.length - 1] == cmd)
141
+ return;
142
+ this.commandHistory.push(cmd);
143
+
144
+ if(this.commandHistory.length > this._commandHistoryLimit) {
145
+ this.commandHistory.shift();
146
+ }
147
+ }
148
+ createCommand({ name, execute, description = "", syntax, uniqueKey }) {
149
+ this._commands[name] = { execute, description, syntax, uniqueKey };
150
+ }
151
+ removeCommand(name) {
152
+ delete this._commands[name]
153
+ }
154
+ bscript(run, options=this.options) {
155
+ if(!run)
156
+ return;
157
+ const bScriptRunner = new Runner(this, options)
158
+ bScriptRunner.Create(run)
159
+ return bScriptRunner.executer;
160
+ }
161
+ async Start() {
162
+ this.updateCommands();
163
+ this.started = true;
164
+ this._perUpdateInterval = setInterval(() => this.emit("perUpdate"), 300);
165
+ while(true) {
166
+ await this.waitForPerUpdate();
167
+ if(this.returnCode == 1){
168
+ const value = await this.Input(this.commandController.defaultCommandPrefix);
169
+ if(value)
170
+ {
171
+ this.pushToCommandHistory(value);
172
+ this.bscript(value)();
173
+ }
174
+ }
175
+ }
176
+ this.on("perUpdate", onPerUpdate);
177
+ }
178
+ Stop() {
179
+ this.started = false;
180
+ process.stdout.clearLine();
181
+ process.stdout.cursorTo(0);
182
+ clearInterval(this._perUpdateInterval);
183
+ }
184
+ async waitForPerUpdate() {
185
+ await new Promise(res => this.once('perUpdate', res));
186
+ }
187
+ async Input(prefix = " > ") {
188
+ this.returnCode = -1;
189
+ await this.waitForPerUpdate();
190
+ this.returnCode = 0;
191
+ const nativePrefix = this.commandController.defaultCommandPrefix;
192
+ this.commandController.defaultCommandPrefix = prefix;
193
+
194
+ this.commandController.InitInput(true);
195
+
196
+ this.commandController.stdinVal = "";
197
+ this.commandController.col = 0;
198
+
199
+ this.commandController.CursorTo(0);
200
+ this.commandController.WriteCommand("");
201
+
202
+ let onData = async (inputStdin) => {
203
+ this.returnCode = this.commandController.OnData(inputStdin);
204
+ }
205
+
206
+ this.commandController.SetStdinListener('data', onData);
207
+
208
+
209
+ return await new Promise(res => {
210
+ let exit = () => {
211
+ this.commandController.defaultCommandPrefix = nativePrefix;
212
+ this.commandController.RemoveStdinListener('data', onData);
213
+ this.removeListener('perUpdate', perUpdate);
214
+ this.commandController.InitInput(false);
215
+ }
216
+ let perUpdate = async () => {
217
+ switch(this.returnCode) {
218
+ case 1:
219
+ exit();
220
+ res(this.commandController.stdinVal);
221
+ break;
222
+ case -1: // breaking
223
+ exit();
224
+ this.commandController.ClearLine();
225
+ this.commandController.CursorTo(0);
226
+ res(undefined);
227
+ break;
228
+ }
229
+ }
230
+ this.on('perUpdate', perUpdate)
231
+ })
232
+ }
233
+ }
@@ -0,0 +1,502 @@
1
+ # BSCRIPT-REFERENCE-CLI
2
+
3
+ Complete reference of all built-in BScript CLI commands.
4
+
5
+ ---
6
+
7
+ # Contents
8
+
9
+ - [Core Types](#core-types)
10
+ - [Variables](#variables)
11
+ - [JavaScript Interop](#javascript-interop)
12
+ - [Execution](#execution)
13
+ - [Input / Output](#input--output)
14
+ - [Scripts](#scripts)
15
+ - [Utilities](#utilities)
16
+ - [Debug](#debug)
17
+
18
+ ---
19
+
20
+ # Core Types
21
+
22
+ ## bool
23
+
24
+ Creates a boolean value.
25
+
26
+ ### Syntax
27
+
28
+ ```bscript
29
+ bool <value>
30
+ ```
31
+
32
+ ### Returns
33
+
34
+ `bool`
35
+
36
+ ### Example
37
+
38
+ ```bscript
39
+ print ${bool true}
40
+ print ${bool false}
41
+ ```
42
+
43
+ ---
44
+
45
+ ## num
46
+
47
+ Creates a numeric value.
48
+
49
+ ### Syntax
50
+
51
+ ```bscript
52
+ num <number>
53
+ ```
54
+
55
+ ### Returns
56
+
57
+ `number`
58
+
59
+ ### Example
60
+
61
+ ```bscript
62
+ print ${num 100}
63
+ ```
64
+
65
+ ---
66
+
67
+ ## str
68
+
69
+ Creates a string by concatenating all arguments.
70
+
71
+ ### Syntax
72
+
73
+ ```bscript
74
+ str <value...>
75
+ ```
76
+
77
+ ### Returns
78
+
79
+ `string`
80
+
81
+ ### Example
82
+
83
+ ```bscript
84
+ print ${str Hello World}
85
+ ```
86
+
87
+ ---
88
+
89
+ ## object
90
+
91
+ Creates a BScript object.
92
+
93
+ ### Syntax
94
+
95
+ ```bscript
96
+ object <value>
97
+ ```
98
+
99
+ ### Returns
100
+
101
+ `object`
102
+
103
+ ### Example
104
+
105
+ ```bscript
106
+ object ${array
107
+ (
108
+ name = John
109
+ age = 20
110
+ )}
111
+ ```
112
+
113
+ ---
114
+
115
+ ## raw
116
+
117
+ Returns a raw value without its BScript wrapper.
118
+
119
+ ### Syntax
120
+
121
+ ```bscript
122
+ raw <value>
123
+ ```
124
+
125
+ ### Returns
126
+
127
+ Raw value.
128
+
129
+ ### Example
130
+
131
+ ```bscript
132
+ raw ${str Hello}
133
+ ```
134
+
135
+ ---
136
+
137
+ ## jstype
138
+
139
+ Wraps a JavaScript object as a JS type.
140
+
141
+ ### Syntax
142
+
143
+ ```bscript
144
+ jstype <value>
145
+ ```
146
+
147
+ ### Returns
148
+
149
+ `jstype`
150
+
151
+ ---
152
+
153
+ # Variables
154
+
155
+ ## \$arg
156
+
157
+ Returns a function argument.
158
+
159
+ ### Syntax
160
+
161
+ ```bscript
162
+ $arg <index>
163
+ ```
164
+
165
+ ### Returns
166
+
167
+ Argument value.
168
+
169
+ ### Example
170
+
171
+ ```bscript
172
+ $arg 0
173
+ ```
174
+
175
+ ---
176
+
177
+ ## \$val
178
+
179
+ Reads a stored variable.
180
+
181
+ ### Syntax
182
+
183
+ ```bscript
184
+ $val <name>
185
+ ```
186
+
187
+ ### Returns
188
+
189
+ Stored value.
190
+
191
+ ### Variable existence
192
+
193
+ ```bscript
194
+ $val exist <name>
195
+ ```
196
+
197
+ Returns
198
+
199
+ ```text
200
+ bool
201
+ ```
202
+
203
+ ---
204
+
205
+ ## ref
206
+
207
+ Creates a reference to an object property.
208
+
209
+ ### Syntax
210
+
211
+ ```bscript
212
+ ref <path>
213
+ ```
214
+
215
+ or
216
+
217
+ ```bscript
218
+ ref <object> <path>
219
+ ```
220
+
221
+ ### Returns
222
+
223
+ `ref`
224
+
225
+ ### Example
226
+
227
+ ```bscript
228
+ ref player.health
229
+ ```
230
+
231
+ ---
232
+
233
+ # JavaScript Interop
234
+
235
+ ## js
236
+
237
+ Imports a JavaScript module using Node.js require().
238
+
239
+ ### Syntax
240
+
241
+ ```bscript
242
+ js <module>
243
+ ```
244
+
245
+ ### Returns
246
+
247
+ JavaScript module.
248
+
249
+ ### Example
250
+
251
+ ```bscript
252
+ js fs
253
+ ```
254
+
255
+ ---
256
+
257
+ ## js_new
258
+
259
+ Creates a new JavaScript class instance.
260
+
261
+ ### Syntax
262
+
263
+ ```bscript
264
+ js_new <class> <arguments...>
265
+ ```
266
+
267
+ ### Returns
268
+
269
+ JavaScript object.
270
+
271
+ ### Example
272
+
273
+ ```bscript
274
+ js_new ${js Date}
275
+ ```
276
+
277
+ ---
278
+
279
+ ## eval
280
+
281
+ Evaluates JavaScript code.
282
+
283
+ Alias:
284
+
285
+ - evaluate
286
+
287
+ ### Syntax
288
+
289
+ ```bscript
290
+ eval <javascript>
291
+ ```
292
+
293
+ ### Returns
294
+
295
+ Evaluation result.
296
+
297
+ ### Example
298
+
299
+ ```bscript
300
+ eval "1 + 2"
301
+ ```
302
+
303
+ ---
304
+
305
+ # Execution
306
+
307
+ ## run
308
+
309
+ Executes a BScript function.
310
+
311
+ ### Syntax
312
+
313
+ ```bscript
314
+ run <function> <arguments...>
315
+ ```
316
+
317
+ ### Returns
318
+
319
+ Function result.
320
+
321
+ ---
322
+
323
+ ## await
324
+
325
+ Awaits a Promise.
326
+
327
+ ### Syntax
328
+
329
+ ```bscript
330
+ await <promise>
331
+ ```
332
+
333
+ or
334
+
335
+ ```bscript
336
+ await run <function>
337
+ ```
338
+
339
+ ### Returns
340
+
341
+ Resolved value.
342
+
343
+ ---
344
+
345
+ ## delay
346
+
347
+ Pauses execution.
348
+
349
+ ### Syntax
350
+
351
+ ```bscript
352
+ delay <milliseconds>
353
+ ```
354
+
355
+ ### Returns
356
+
357
+ Promise.
358
+
359
+ ### Example
360
+
361
+ ```bscript
362
+ delay 1000
363
+ ```
364
+
365
+ ---
366
+
367
+ # Input / Output
368
+
369
+ ## print
370
+
371
+ Prints values to the console.
372
+
373
+ ### Syntax
374
+
375
+ ```bscript
376
+ print <value...>
377
+ ```
378
+
379
+ ### Example
380
+
381
+ ```bscript
382
+ print Hello World
383
+ ```
384
+
385
+ ---
386
+
387
+ ## Input
388
+
389
+ Reads user input.
390
+
391
+ ### Syntax
392
+
393
+ ```bscript
394
+ Input <prompt>
395
+ ```
396
+
397
+ ### Returns
398
+
399
+ User input.
400
+
401
+ ### Example
402
+
403
+ ```bscript
404
+ Input Enter your name:
405
+ ```
406
+
407
+ ---
408
+
409
+ # Scripts
410
+
411
+ ## import
412
+
413
+ Loads another BScript file inside a new runner and returns its exported value.
414
+
415
+ ### Syntax
416
+
417
+ ```bscript
418
+ import <path>
419
+ ```
420
+
421
+ ### Returns
422
+
423
+ Exported value.
424
+
425
+ ### Example
426
+
427
+ ```bscript
428
+ import scripts/math.bs
429
+ ```
430
+
431
+ ---
432
+
433
+ ## script
434
+
435
+ Executes another BScript file.
436
+
437
+ ### Syntax
438
+
439
+ ```bscript
440
+ script <path>
441
+ ```
442
+
443
+ ### Returns
444
+
445
+ Nothing.
446
+
447
+ ### Example
448
+
449
+ ```bscript
450
+ script scripts/start.bs
451
+ ```
452
+
453
+ ---
454
+
455
+ # Utilities
456
+
457
+ ## help
458
+
459
+ Displays every registered command.
460
+
461
+ ### Syntax
462
+
463
+ ```bscript
464
+ help
465
+ ```
466
+
467
+ ---
468
+
469
+ # Debug
470
+
471
+ ## clear
472
+
473
+ Clears the console.
474
+
475
+ ### Syntax
476
+
477
+ ```bscript
478
+ clear
479
+ ```
480
+
481
+ ---
482
+
483
+ ## update-commands
484
+
485
+ Reloads all registered commands.
486
+
487
+ ### Syntax
488
+
489
+ ```bscript
490
+ update-commands
491
+ ```
492
+
493
+ ---
494
+
495
+ # Notes
496
+
497
+ - All commands are registered as modules.
498
+ - Commands may return native JavaScript objects or BScript values.
499
+ - JavaScript interoperability is available through `js`, `js_new`, `eval`, and `jstype`.
500
+ - `await` automatically resolves BScript Promise values.
501
+ - `import` executes a separate BScript runner and returns its exported value.
502
+ - `script` executes another script without returning exports.
Binary file
Binary file
Binary file
Binary file
Binary file