starlight-cli 1.0.7 → 1.0.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.
package/dist/index.js CHANGED
@@ -2439,12 +2439,14 @@ module.exports = require("path");
2439
2439
  var __webpack_exports__ = {};
2440
2440
  const fs = __nccwpck_require__(896);
2441
2441
  const path = __nccwpck_require__(928);
2442
+ const os = __nccwpck_require__(857);
2443
+ const { exec } = __nccwpck_require__(317);
2442
2444
 
2443
2445
  const Lexer = __nccwpck_require__(211);
2444
2446
  const Parser = __nccwpck_require__(222);
2445
2447
  const Evaluator = __nccwpck_require__(112);
2446
2448
 
2447
- const VERSION = '1.0.7';
2449
+ const VERSION = '1.0.8';
2448
2450
 
2449
2451
  const COLOR = {
2450
2452
  reset: '\x1b[0m',
@@ -2484,7 +2486,6 @@ function printSourceContext(code, line, column) {
2484
2486
  );
2485
2487
  }
2486
2488
 
2487
-
2488
2489
  const args = process.argv.slice(2);
2489
2490
 
2490
2491
  if (args.length === 0) {
@@ -2492,9 +2493,11 @@ if (args.length === 0) {
2492
2493
  Starlight Programming Language
2493
2494
 
2494
2495
  Usage:
2495
- starlight <file.sl> Run a Starlight program
2496
- starlight -v Show version
2497
- starlight --help Show help
2496
+ starlight <file.sl> Run a Starlight program
2497
+ starlight -v Show version
2498
+ starlight --help Show help
2499
+ starlight --learn Open learning guide
2500
+ starlight --writedirectly Write and run temporary code
2498
2501
  `);
2499
2502
  process.exit(0);
2500
2503
  }
@@ -2509,81 +2512,117 @@ if (args[0] === '--help') {
2509
2512
  Starlight CLI Help
2510
2513
 
2511
2514
  Commands:
2512
- starlight file.sl Run a Starlight program
2513
- starlight -v Show version
2514
- starlight --help Show this help message
2515
+ starlight file.sl Run a Starlight program
2516
+ starlight -v Show version
2517
+ starlight --learn Open learning guide
2518
+ starlight --writedirectly Write & run temporary code
2515
2519
  `);
2516
2520
  process.exit(0);
2517
2521
  }
2522
+ if (args[0] === '--learn') {
2523
+ console.log(COLOR.cyan + 'Opening Starlight Learning Guide...' + COLOR.reset);
2524
+ exec('start https://programming-lang.pages.dev/learning-guide');
2525
+ process.exit(0);
2526
+ }
2518
2527
 
2519
- const filePath = path.resolve(args[0]);
2528
+ if (args[0] === '--writedirectly') {
2529
+ console.log(COLOR.green + 'Write your Starlight code below.' + COLOR.reset);
2530
+ console.log(COLOR.gray + 'Press CTRL + Z then ENTER to run.\n' + COLOR.reset);
2520
2531
 
2521
- if (!fs.existsSync(filePath)) {
2522
- return fatal(`File not found: ${filePath}`);
2523
- }
2532
+ let input = '';
2533
+
2534
+ process.stdin.resume();
2535
+ process.stdin.setEncoding('utf8');
2536
+
2537
+ process.stdin.on('data', chunk => {
2538
+ input += chunk;
2539
+ });
2540
+
2541
+ process.stdin.on('end', () => {
2542
+ const tempFile = path.join(
2543
+ os.tmpdir(),
2544
+ `starlight-temp-${Date.now()}.sl`
2545
+ );
2524
2546
 
2525
- let code;
2526
- try {
2527
- code = fs.readFileSync(filePath, 'utf-8');
2528
- } catch (err) {
2529
- return fatal(`Failed to read file: ${err.message}`);
2547
+ fs.writeFileSync(tempFile, input, 'utf8');
2548
+
2549
+ runFile(tempFile, true);
2550
+ });
2551
+
2552
+ return;
2530
2553
  }
2554
+ function runFile(filePath, isTemp = false) {
2555
+ if (!fs.existsSync(filePath)) {
2556
+ return fatal(`File not found: ${filePath}`);
2557
+ }
2531
2558
 
2559
+ let code;
2560
+ try {
2561
+ code = fs.readFileSync(filePath, 'utf-8');
2562
+ } catch (err) {
2563
+ return fatal(`Failed to read file: ${err.message}`);
2564
+ }
2532
2565
 
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);
2566
+ let tokens;
2567
+ try {
2568
+ const lexer = new Lexer(code);
2569
+ tokens = lexer.getTokens();
2570
+ } catch (err) {
2571
+ console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
2572
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2573
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2574
+
2575
+ if (err.line && err.column) {
2576
+ printSourceContext(code, err.line, err.column);
2577
+ }
2541
2578
 
2542
- if (err.line && err.column) {
2543
- printSourceContext(code, err.line, err.column);
2579
+ return waitAndExit(1);
2544
2580
  }
2545
2581
 
2546
- return waitAndExit(1);
2547
- }
2582
+ let ast;
2583
+ try {
2584
+ const parser = new Parser(tokens);
2585
+ ast = parser.parse();
2586
+ } catch (err) {
2587
+ console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
2588
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2589
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2590
+
2591
+ if (err.token && err.token.line && err.token.column) {
2592
+ printSourceContext(code, err.token.line, err.token.column);
2593
+ }
2548
2594
 
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);
2595
+ return waitAndExit(1);
2560
2596
  }
2561
2597
 
2562
- return waitAndExit(1);
2563
- }
2598
+ try {
2599
+ const evaluator = new Evaluator();
2600
+ evaluator.evaluate(ast);
2601
+ } catch (err) {
2602
+ console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
2603
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2604
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2564
2605
 
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);
2606
+ if (err.line && err.column) {
2607
+ printSourceContext(code, err.line, err.column);
2608
+ }
2572
2609
 
2573
- if (err.line && err.column) {
2574
- printSourceContext(code, err.line, err.column);
2610
+ return waitAndExit(1);
2575
2611
  }
2576
2612
 
2577
- return waitAndExit(1);
2578
- }
2613
+ console.log(
2614
+ COLOR.green +
2615
+ '\nProgram finished successfully.' +
2616
+ COLOR.reset
2617
+ );
2579
2618
 
2580
- console.log(
2581
- COLOR.green +
2582
- '\nProgram finished successfully.' +
2583
- COLOR.reset
2584
- );
2619
+ if (isTemp) {
2620
+ try { fs.unlinkSync(filePath); } catch {}
2621
+ }
2585
2622
 
2586
- waitAndExit(0);
2623
+ waitAndExit(0);
2624
+ }
2625
+ runFile(path.resolve(args[0]));
2587
2626
 
2588
2627
  module.exports = __webpack_exports__;
2589
2628
  /******/ })()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
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.8';
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,114 @@ 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
+ console.log(COLOR.green + 'Write your Starlight code below.' + COLOR.reset);
91
+ console.log(COLOR.gray + 'Press CTRL + Z then ENTER to run.\n' + COLOR.reset);
81
92
 
82
- if (!fs.existsSync(filePath)) {
83
- return fatal(`File not found: ${filePath}`);
84
- }
93
+ let input = '';
94
+
95
+ process.stdin.resume();
96
+ process.stdin.setEncoding('utf8');
85
97
 
86
- let code;
87
- try {
88
- code = fs.readFileSync(filePath, 'utf-8');
89
- } catch (err) {
90
- return fatal(`Failed to read file: ${err.message}`);
98
+ process.stdin.on('data', chunk => {
99
+ input += chunk;
100
+ });
101
+
102
+ process.stdin.on('end', () => {
103
+ const tempFile = path.join(
104
+ os.tmpdir(),
105
+ `starlight-temp-${Date.now()}.sl`
106
+ );
107
+
108
+ fs.writeFileSync(tempFile, input, 'utf8');
109
+
110
+ runFile(tempFile, true);
111
+ });
112
+
113
+ return;
91
114
  }
115
+ function runFile(filePath, isTemp = false) {
116
+ if (!fs.existsSync(filePath)) {
117
+ return fatal(`File not found: ${filePath}`);
118
+ }
119
+
120
+ let code;
121
+ try {
122
+ code = fs.readFileSync(filePath, 'utf-8');
123
+ } catch (err) {
124
+ return fatal(`Failed to read file: ${err.message}`);
125
+ }
92
126
 
127
+ let tokens;
128
+ try {
129
+ const lexer = new Lexer(code);
130
+ tokens = lexer.getTokens();
131
+ } catch (err) {
132
+ console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
133
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
134
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
93
135
 
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);
136
+ if (err.line && err.column) {
137
+ printSourceContext(code, err.line, err.column);
138
+ }
102
139
 
103
- if (err.line && err.column) {
104
- printSourceContext(code, err.line, err.column);
140
+ return waitAndExit(1);
105
141
  }
106
142
 
107
- return waitAndExit(1);
108
- }
143
+ let ast;
144
+ try {
145
+ const parser = new Parser(tokens);
146
+ ast = parser.parse();
147
+ } catch (err) {
148
+ console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
149
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
150
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
109
151
 
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);
152
+ if (err.token && err.token.line && err.token.column) {
153
+ printSourceContext(code, err.token.line, err.token.column);
154
+ }
155
+
156
+ return waitAndExit(1);
121
157
  }
122
158
 
123
- return waitAndExit(1);
124
- }
159
+ try {
160
+ const evaluator = new Evaluator();
161
+ evaluator.evaluate(ast);
162
+ } catch (err) {
163
+ console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
164
+ console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
165
+ console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
125
166
 
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);
167
+ if (err.line && err.column) {
168
+ printSourceContext(code, err.line, err.column);
169
+ }
133
170
 
134
- if (err.line && err.column) {
135
- printSourceContext(code, err.line, err.column);
171
+ return waitAndExit(1);
136
172
  }
137
173
 
138
- return waitAndExit(1);
139
- }
174
+ console.log(
175
+ COLOR.green +
176
+ '\nProgram finished successfully.' +
177
+ COLOR.reset
178
+ );
140
179
 
141
- console.log(
142
- COLOR.green +
143
- '\nProgram finished successfully.' +
144
- COLOR.reset
145
- );
180
+ if (isTemp) {
181
+ try { fs.unlinkSync(filePath); } catch {}
182
+ }
146
183
 
147
- waitAndExit(0);
184
+ waitAndExit(0);
185
+ }
186
+ runFile(path.resolve(args[0]));