starlight-cli 1.0.7 → 1.0.9

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.
package/dist/index.js CHANGED
@@ -2396,6 +2396,14 @@ module.exports = require("os");
2396
2396
  "use strict";
2397
2397
  module.exports = require("path");
2398
2398
 
2399
+ /***/ }),
2400
+
2401
+ /***/ 785:
2402
+ /***/ ((module) => {
2403
+
2404
+ "use strict";
2405
+ module.exports = require("readline");
2406
+
2399
2407
  /***/ })
2400
2408
 
2401
2409
  /******/ });
@@ -2439,12 +2447,14 @@ module.exports = require("path");
2439
2447
  var __webpack_exports__ = {};
2440
2448
  const fs = __nccwpck_require__(896);
2441
2449
  const path = __nccwpck_require__(928);
2450
+ const os = __nccwpck_require__(857);
2451
+ const { exec } = __nccwpck_require__(317);
2442
2452
 
2443
2453
  const Lexer = __nccwpck_require__(211);
2444
2454
  const Parser = __nccwpck_require__(222);
2445
2455
  const Evaluator = __nccwpck_require__(112);
2446
2456
 
2447
- const VERSION = '1.0.7';
2457
+ const VERSION = '1.0.9';
2448
2458
 
2449
2459
  const COLOR = {
2450
2460
  reset: '\x1b[0m',
@@ -2484,7 +2494,6 @@ function printSourceContext(code, line, column) {
2484
2494
  );
2485
2495
  }
2486
2496
 
2487
-
2488
2497
  const args = process.argv.slice(2);
2489
2498
 
2490
2499
  if (args.length === 0) {
@@ -2492,9 +2501,11 @@ if (args.length === 0) {
2492
2501
  Starlight Programming Language
2493
2502
 
2494
2503
  Usage:
2495
- starlight <file.sl> Run a Starlight program
2496
- starlight -v Show version
2497
- starlight --help Show help
2504
+ starlight <file.sl> Run a Starlight program
2505
+ starlight -v Show version
2506
+ starlight --help Show help
2507
+ starlight --learn Open learning guide
2508
+ starlight --writedirectly Write and run temporary code
2498
2509
  `);
2499
2510
  process.exit(0);
2500
2511
  }
@@ -2509,81 +2520,129 @@ if (args[0] === '--help') {
2509
2520
  Starlight CLI Help
2510
2521
 
2511
2522
  Commands:
2512
- starlight file.sl Run a Starlight program
2513
- starlight -v Show version
2514
- starlight --help Show this help message
2523
+ starlight file.sl Run a Starlight program
2524
+ starlight -v Show version
2525
+ starlight --learn Open learning guide
2526
+ starlight --writedirectly Write & run temporary code
2515
2527
  `);
2516
2528
  process.exit(0);
2517
2529
  }
2530
+ if (args[0] === '--learn') {
2531
+ console.log(COLOR.cyan + 'Opening Starlight Learning Guide...' + COLOR.reset);
2532
+ exec('start https://programming-lang.pages.dev/learning-guide');
2533
+ process.exit(0);
2534
+ }
2518
2535
 
2519
- const filePath = path.resolve(args[0]);
2536
+ if (args[0] === '--writedirectly') {
2537
+ const readline = __nccwpck_require__(785);
2538
+ const tempFile = path.join(os.tmpdir(), `starlight-temp-${Date.now()}.sl`);
2520
2539
 
2521
- if (!fs.existsSync(filePath)) {
2522
- return fatal(`File not found: ${filePath}`);
2523
- }
2540
+ console.log(COLOR.green + 'Interactive Starlight Editor' + COLOR.reset);
2541
+ console.log(COLOR.gray + 'Type your code. Enter ":run" on a new line to execute.\n' + COLOR.reset);
2542
+
2543
+ const rl = readline.createInterface({
2544
+ input: process.stdin,
2545
+ output: process.stdout,
2546
+ prompt: COLOR.cyan + '> ' + COLOR.reset
2547
+ });
2548
+
2549
+ let lines = [];
2550
+
2551
+ rl.prompt();
2552
+
2553
+ rl.on('line', (line) => {
2554
+ if (line.trim() === ':run') {
2555
+ rl.close();
2556
+ fs.writeFileSync(tempFile, lines.join('\n'), 'utf8');
2557
+ runFile(tempFile, true);
2558
+ return;
2559
+ }
2560
+
2561
+ let coloredLine = line
2562
+ .replace(/"(.*?)"/g, COLOR.yellow + '"$1"' + COLOR.reset)
2563
+ .replace(/\b(sldeploy|import|from|const|let|var|if|else|for|while)\b/g, COLOR.blue + '$1' + COLOR.reset);
2564
+
2565
+ console.log(coloredLine);
2524
2566
 
2525
- let code;
2526
- try {
2527
- code = fs.readFileSync(filePath, 'utf-8');
2528
- } catch (err) {
2529
- return fatal(`Failed to read file: ${err.message}`);
2567
+ lines.push(line);
2568
+ rl.prompt();
2569
+ });
2570
+
2571
+ return;
2530
2572
  }
2531
2573
 
2574
+ function runFile(filePath, isTemp = false) {
2575
+ if (!fs.existsSync(filePath)) {
2576
+ return fatal(`File not found: ${filePath}`);
2577
+ }
2532
2578
 
2533
- let tokens;
2534
- try {
2535
- const lexer = new Lexer(code);
2536
- tokens = lexer.getTokens();
2537
- } catch (err) {
2538
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
2539
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2540
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2579
+ let code;
2580
+ try {
2581
+ code = fs.readFileSync(filePath, 'utf-8');
2582
+ } catch (err) {
2583
+ return fatal(`Failed to read file: ${err.message}`);
2584
+ }
2541
2585
 
2542
- if (err.line && err.column) {
2543
- printSourceContext(code, err.line, err.column);
2586
+ let tokens;
2587
+ try {
2588
+ const lexer = new Lexer(code);
2589
+ tokens = lexer.getTokens();
2590
+ } catch (err) {
2591
+ console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
2592
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2593
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2594
+
2595
+ if (err.line && err.column) {
2596
+ printSourceContext(code, err.line, err.column);
2597
+ }
2598
+
2599
+ return waitAndExit(1);
2544
2600
  }
2545
2601
 
2546
- return waitAndExit(1);
2547
- }
2602
+ let ast;
2603
+ try {
2604
+ const parser = new Parser(tokens);
2605
+ ast = parser.parse();
2606
+ } catch (err) {
2607
+ console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
2608
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2609
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2610
+
2611
+ if (err.token && err.token.line && err.token.column) {
2612
+ printSourceContext(code, err.token.line, err.token.column);
2613
+ }
2548
2614
 
2549
- let ast;
2550
- try {
2551
- const parser = new Parser(tokens);
2552
- ast = parser.parse();
2553
- } catch (err) {
2554
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
2555
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2556
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2557
-
2558
- if (err.token && err.token.line && err.token.column) {
2559
- printSourceContext(code, err.token.line, err.token.column);
2615
+ return waitAndExit(1);
2560
2616
  }
2561
2617
 
2562
- return waitAndExit(1);
2563
- }
2618
+ try {
2619
+ const evaluator = new Evaluator();
2620
+ evaluator.evaluate(ast);
2621
+ } catch (err) {
2622
+ console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
2623
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2624
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2564
2625
 
2565
- try {
2566
- const evaluator = new Evaluator();
2567
- evaluator.evaluate(ast);
2568
- } catch (err) {
2569
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
2570
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2571
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2626
+ if (err.line && err.column) {
2627
+ printSourceContext(code, err.line, err.column);
2628
+ }
2572
2629
 
2573
- if (err.line && err.column) {
2574
- printSourceContext(code, err.line, err.column);
2630
+ return waitAndExit(1);
2575
2631
  }
2576
2632
 
2577
- return waitAndExit(1);
2578
- }
2633
+ console.log(
2634
+ COLOR.green +
2635
+ '\nProgram finished successfully.' +
2636
+ COLOR.reset
2637
+ );
2579
2638
 
2580
- console.log(
2581
- COLOR.green +
2582
- '\nProgram finished successfully.' +
2583
- COLOR.reset
2584
- );
2639
+ if (isTemp) {
2640
+ try { fs.unlinkSync(filePath); } catch {}
2641
+ }
2585
2642
 
2586
- waitAndExit(0);
2643
+ waitAndExit(0);
2644
+ }
2645
+ runFile(path.resolve(args[0]));
2587
2646
 
2588
2647
  module.exports = __webpack_exports__;
2589
2648
  /******/ })()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Starlight Programming Language CLI",
5
5
  "bin": {
6
6
  "starlight": "index.js"
package/src/starlight.js CHANGED
@@ -1,11 +1,13 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
+ const os = require('os');
4
+ const { exec } = require('child_process');
3
5
 
4
6
  const Lexer = require('./lexer');
5
7
  const Parser = require('./parser');
6
8
  const Evaluator = require('./evaluator');
7
9
 
8
- const VERSION = '1.0.7';
10
+ const VERSION = '1.0.9';
9
11
 
10
12
  const COLOR = {
11
13
  reset: '\x1b[0m',
@@ -45,7 +47,6 @@ function printSourceContext(code, line, column) {
45
47
  );
46
48
  }
47
49
 
48
-
49
50
  const args = process.argv.slice(2);
50
51
 
51
52
  if (args.length === 0) {
@@ -53,9 +54,11 @@ if (args.length === 0) {
53
54
  Starlight Programming Language
54
55
 
55
56
  Usage:
56
- starlight <file.sl> Run a Starlight program
57
- starlight -v Show version
58
- starlight --help Show help
57
+ starlight <file.sl> Run a Starlight program
58
+ starlight -v Show version
59
+ starlight --help Show help
60
+ starlight --learn Open learning guide
61
+ starlight --writedirectly Write and run temporary code
59
62
  `);
60
63
  process.exit(0);
61
64
  }
@@ -70,78 +73,126 @@ if (args[0] === '--help') {
70
73
  Starlight CLI Help
71
74
 
72
75
  Commands:
73
- starlight file.sl Run a Starlight program
74
- starlight -v Show version
75
- starlight --help Show this help message
76
+ starlight file.sl Run a Starlight program
77
+ starlight -v Show version
78
+ starlight --learn Open learning guide
79
+ starlight --writedirectly Write & run temporary code
76
80
  `);
77
81
  process.exit(0);
78
82
  }
83
+ if (args[0] === '--learn') {
84
+ console.log(COLOR.cyan + 'Opening Starlight Learning Guide...' + COLOR.reset);
85
+ exec('start https://programming-lang.pages.dev/learning-guide');
86
+ process.exit(0);
87
+ }
79
88
 
80
- const filePath = path.resolve(args[0]);
89
+ if (args[0] === '--writedirectly') {
90
+ const readline = require('readline');
91
+ const tempFile = path.join(os.tmpdir(), `starlight-temp-${Date.now()}.sl`);
81
92
 
82
- if (!fs.existsSync(filePath)) {
83
- return fatal(`File not found: ${filePath}`);
84
- }
93
+ console.log(COLOR.green + 'Interactive Starlight Editor' + COLOR.reset);
94
+ console.log(COLOR.gray + 'Type your code. Enter ":run" on a new line to execute.\n' + COLOR.reset);
95
+
96
+ const rl = readline.createInterface({
97
+ input: process.stdin,
98
+ output: process.stdout,
99
+ prompt: COLOR.cyan + '> ' + COLOR.reset
100
+ });
101
+
102
+ let lines = [];
103
+
104
+ rl.prompt();
85
105
 
86
- let code;
87
- try {
88
- code = fs.readFileSync(filePath, 'utf-8');
89
- } catch (err) {
90
- return fatal(`Failed to read file: ${err.message}`);
106
+ rl.on('line', (line) => {
107
+ if (line.trim() === ':run') {
108
+ rl.close();
109
+ fs.writeFileSync(tempFile, lines.join('\n'), 'utf8');
110
+ runFile(tempFile, true);
111
+ return;
112
+ }
113
+
114
+ let coloredLine = line
115
+ .replace(/"(.*?)"/g, COLOR.yellow + '"$1"' + COLOR.reset)
116
+ .replace(/\b(sldeploy|import|from|const|let|var|if|else|for|while)\b/g, COLOR.blue + '$1' + COLOR.reset);
117
+
118
+ console.log(coloredLine);
119
+
120
+ lines.push(line);
121
+ rl.prompt();
122
+ });
123
+
124
+ return;
91
125
  }
92
126
 
127
+ function runFile(filePath, isTemp = false) {
128
+ if (!fs.existsSync(filePath)) {
129
+ return fatal(`File not found: ${filePath}`);
130
+ }
131
+
132
+ let code;
133
+ try {
134
+ code = fs.readFileSync(filePath, 'utf-8');
135
+ } catch (err) {
136
+ return fatal(`Failed to read file: ${err.message}`);
137
+ }
138
+
139
+ let tokens;
140
+ try {
141
+ const lexer = new Lexer(code);
142
+ tokens = lexer.getTokens();
143
+ } catch (err) {
144
+ console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
145
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
146
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
93
147
 
94
- let tokens;
95
- try {
96
- const lexer = new Lexer(code);
97
- tokens = lexer.getTokens();
98
- } catch (err) {
99
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
100
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
101
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
148
+ if (err.line && err.column) {
149
+ printSourceContext(code, err.line, err.column);
150
+ }
102
151
 
103
- if (err.line && err.column) {
104
- printSourceContext(code, err.line, err.column);
152
+ return waitAndExit(1);
105
153
  }
106
154
 
107
- return waitAndExit(1);
108
- }
155
+ let ast;
156
+ try {
157
+ const parser = new Parser(tokens);
158
+ ast = parser.parse();
159
+ } catch (err) {
160
+ console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
161
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
162
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
109
163
 
110
- let ast;
111
- try {
112
- const parser = new Parser(tokens);
113
- ast = parser.parse();
114
- } catch (err) {
115
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
116
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
117
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
118
-
119
- if (err.token && err.token.line && err.token.column) {
120
- printSourceContext(code, err.token.line, err.token.column);
164
+ if (err.token && err.token.line && err.token.column) {
165
+ printSourceContext(code, err.token.line, err.token.column);
166
+ }
167
+
168
+ return waitAndExit(1);
121
169
  }
122
170
 
123
- return waitAndExit(1);
124
- }
171
+ try {
172
+ const evaluator = new Evaluator();
173
+ evaluator.evaluate(ast);
174
+ } catch (err) {
175
+ console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
176
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
177
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
125
178
 
126
- try {
127
- const evaluator = new Evaluator();
128
- evaluator.evaluate(ast);
129
- } catch (err) {
130
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
131
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
132
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
179
+ if (err.line && err.column) {
180
+ printSourceContext(code, err.line, err.column);
181
+ }
133
182
 
134
- if (err.line && err.column) {
135
- printSourceContext(code, err.line, err.column);
183
+ return waitAndExit(1);
136
184
  }
137
185
 
138
- return waitAndExit(1);
139
- }
186
+ console.log(
187
+ COLOR.green +
188
+ '\nProgram finished successfully.' +
189
+ COLOR.reset
190
+ );
140
191
 
141
- console.log(
142
- COLOR.green +
143
- '\nProgram finished successfully.' +
144
- COLOR.reset
145
- );
192
+ if (isTemp) {
193
+ try { fs.unlinkSync(filePath); } catch {}
194
+ }
146
195
 
147
- waitAndExit(0);
196
+ waitAndExit(0);
197
+ }
198
+ runFile(path.resolve(args[0]));