starlight-cli 1.0.6 → 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,11 +2439,15 @@ 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
 
2449
+ const VERSION = '1.0.8';
2450
+
2447
2451
  const COLOR = {
2448
2452
  reset: '\x1b[0m',
2449
2453
  bold: '\x1b[1m',
@@ -2485,75 +2489,140 @@ function printSourceContext(code, line, column) {
2485
2489
  const args = process.argv.slice(2);
2486
2490
 
2487
2491
  if (args.length === 0) {
2488
- return fatal('Usage: starlight <file.sl>');
2492
+ console.log(`
2493
+ Starlight Programming Language
2494
+
2495
+ Usage:
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
2501
+ `);
2502
+ process.exit(0);
2489
2503
  }
2490
2504
 
2491
- const filePath = path.resolve(args[0]);
2505
+ if (args[0] === '-v' || args[0] === '--version') {
2506
+ console.log(`Starlight CLI v${VERSION}`);
2507
+ process.exit(0);
2508
+ }
2492
2509
 
2493
- if (!fs.existsSync(filePath)) {
2494
- return fatal(`File not found: ${filePath}`);
2510
+ if (args[0] === '--help') {
2511
+ console.log(`
2512
+ Starlight CLI Help
2513
+
2514
+ Commands:
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
2519
+ `);
2520
+ process.exit(0);
2495
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
+ }
2527
+
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);
2531
+
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
+ );
2546
+
2547
+ fs.writeFileSync(tempFile, input, 'utf8');
2548
+
2549
+ runFile(tempFile, true);
2550
+ });
2496
2551
 
2497
- let code;
2498
- try {
2499
- code = fs.readFileSync(filePath, 'utf-8');
2500
- } catch (err) {
2501
- return fatal(`Failed to read file: ${err.message}`);
2552
+ return;
2502
2553
  }
2554
+ function runFile(filePath, isTemp = false) {
2555
+ if (!fs.existsSync(filePath)) {
2556
+ return fatal(`File not found: ${filePath}`);
2557
+ }
2503
2558
 
2504
- let tokens;
2505
- try {
2506
- const lexer = new Lexer(code);
2507
- tokens = lexer.getTokens();
2508
- } catch (err) {
2509
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
2510
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2511
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2512
-
2513
- if (err.line && err.column) {
2514
- printSourceContext(code, err.line, err.column);
2559
+ let code;
2560
+ try {
2561
+ code = fs.readFileSync(filePath, 'utf-8');
2562
+ } catch (err) {
2563
+ return fatal(`Failed to read file: ${err.message}`);
2515
2564
  }
2516
2565
 
2517
- return waitAndExit(1);
2518
- }
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
+ }
2519
2578
 
2520
- let ast;
2521
- try {
2522
- const parser = new Parser(tokens);
2523
- ast = parser.parse();
2524
- } catch (err) {
2525
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
2526
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2527
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2528
-
2529
- if (err.token && err.token.line && err.token.column) {
2530
- printSourceContext(code, err.token.line, err.token.column);
2579
+ return waitAndExit(1);
2531
2580
  }
2532
2581
 
2533
- return waitAndExit(1);
2534
- }
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
+ }
2535
2594
 
2536
- try {
2537
- const evaluator = new Evaluator();
2538
- evaluator.evaluate(ast);
2539
- } catch (err) {
2540
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
2541
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
2542
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
2595
+ return waitAndExit(1);
2596
+ }
2597
+
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);
2605
+
2606
+ if (err.line && err.column) {
2607
+ printSourceContext(code, err.line, err.column);
2608
+ }
2543
2609
 
2544
- if (err.line && err.column) {
2545
- printSourceContext(code, err.line, err.column);
2610
+ return waitAndExit(1);
2546
2611
  }
2547
2612
 
2548
- return waitAndExit(1);
2549
- }
2613
+ console.log(
2614
+ COLOR.green +
2615
+ '\nProgram finished successfully.' +
2616
+ COLOR.reset
2617
+ );
2550
2618
 
2551
- console.log(
2552
- COLOR.green +
2553
- '\nProgram finished successfully.' +
2554
- COLOR.reset
2555
- );
2556
- waitAndExit(0);
2619
+ if (isTemp) {
2620
+ try { fs.unlinkSync(filePath); } catch {}
2621
+ }
2622
+
2623
+ waitAndExit(0);
2624
+ }
2625
+ runFile(path.resolve(args[0]));
2557
2626
 
2558
2627
  module.exports = __webpack_exports__;
2559
2628
  /******/ })()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-cli",
3
- "version": "1.0.6",
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,10 +1,14 @@
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
 
10
+ const VERSION = '1.0.8';
11
+
8
12
  const COLOR = {
9
13
  reset: '\x1b[0m',
10
14
  bold: '\x1b[1m',
@@ -46,72 +50,137 @@ function printSourceContext(code, line, column) {
46
50
  const args = process.argv.slice(2);
47
51
 
48
52
  if (args.length === 0) {
49
- return fatal('Usage: starlight <file.sl>');
53
+ console.log(`
54
+ Starlight Programming Language
55
+
56
+ Usage:
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
62
+ `);
63
+ process.exit(0);
50
64
  }
51
65
 
52
- const filePath = path.resolve(args[0]);
66
+ if (args[0] === '-v' || args[0] === '--version') {
67
+ console.log(`Starlight CLI v${VERSION}`);
68
+ process.exit(0);
69
+ }
53
70
 
54
- if (!fs.existsSync(filePath)) {
55
- return fatal(`File not found: ${filePath}`);
71
+ if (args[0] === '--help') {
72
+ console.log(`
73
+ Starlight CLI Help
74
+
75
+ Commands:
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
80
+ `);
81
+ process.exit(0);
56
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
+ }
88
+
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);
92
+
93
+ let input = '';
94
+
95
+ process.stdin.resume();
96
+ process.stdin.setEncoding('utf8');
57
97
 
58
- let code;
59
- try {
60
- code = fs.readFileSync(filePath, 'utf-8');
61
- } catch (err) {
62
- 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;
63
114
  }
115
+ function runFile(filePath, isTemp = false) {
116
+ if (!fs.existsSync(filePath)) {
117
+ return fatal(`File not found: ${filePath}`);
118
+ }
64
119
 
65
- let tokens;
66
- try {
67
- const lexer = new Lexer(code);
68
- tokens = lexer.getTokens();
69
- } catch (err) {
70
- console.error(COLOR.red + COLOR.bold + 'Starlight Lexer Error' + COLOR.reset);
71
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
72
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
73
-
74
- if (err.line && err.column) {
75
- printSourceContext(code, err.line, err.column);
120
+ let code;
121
+ try {
122
+ code = fs.readFileSync(filePath, 'utf-8');
123
+ } catch (err) {
124
+ return fatal(`Failed to read file: ${err.message}`);
76
125
  }
77
126
 
78
- return waitAndExit(1);
79
- }
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);
80
135
 
81
- let ast;
82
- try {
83
- const parser = new Parser(tokens);
84
- ast = parser.parse();
85
- } catch (err) {
86
- console.error(COLOR.red + COLOR.bold + 'Starlight Parser Error' + COLOR.reset);
87
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
88
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
89
-
90
- if (err.token && err.token.line && err.token.column) {
91
- printSourceContext(code, err.token.line, err.token.column);
136
+ if (err.line && err.column) {
137
+ printSourceContext(code, err.line, err.column);
138
+ }
139
+
140
+ return waitAndExit(1);
92
141
  }
93
142
 
94
- return waitAndExit(1);
95
- }
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);
96
151
 
97
- try {
98
- const evaluator = new Evaluator();
99
- evaluator.evaluate(ast);
100
- } catch (err) {
101
- console.error(COLOR.red + COLOR.bold + 'Starlight Runtime Error' + COLOR.reset);
102
- console.error(COLOR.cyan + `File: ${filePath}` + COLOR.reset);
103
- console.error(COLOR.yellow + `Message: ${err.message}` + COLOR.reset);
152
+ if (err.token && err.token.line && err.token.column) {
153
+ printSourceContext(code, err.token.line, err.token.column);
154
+ }
104
155
 
105
- if (err.line && err.column) {
106
- printSourceContext(code, err.line, err.column);
156
+ return waitAndExit(1);
107
157
  }
108
158
 
109
- return waitAndExit(1);
110
- }
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);
166
+
167
+ if (err.line && err.column) {
168
+ printSourceContext(code, err.line, err.column);
169
+ }
170
+
171
+ return waitAndExit(1);
172
+ }
173
+
174
+ console.log(
175
+ COLOR.green +
176
+ '\nProgram finished successfully.' +
177
+ COLOR.reset
178
+ );
111
179
 
112
- console.log(
113
- COLOR.green +
114
- '\nProgram finished successfully.' +
115
- COLOR.reset
116
- );
117
- waitAndExit(0);
180
+ if (isTemp) {
181
+ try { fs.unlinkSync(filePath); } catch {}
182
+ }
183
+
184
+ waitAndExit(0);
185
+ }
186
+ runFile(path.resolve(args[0]));