@tsonic/cli 0.0.1

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 (82) hide show
  1. package/dist/cli/constants.d.ts +5 -0
  2. package/dist/cli/constants.d.ts.map +1 -0
  3. package/dist/cli/constants.js +5 -0
  4. package/dist/cli/constants.js.map +1 -0
  5. package/dist/cli/dispatcher.d.ts +8 -0
  6. package/dist/cli/dispatcher.d.ts.map +1 -0
  7. package/dist/cli/dispatcher.js +111 -0
  8. package/dist/cli/dispatcher.js.map +1 -0
  9. package/dist/cli/help.d.ts +8 -0
  10. package/dist/cli/help.d.ts.map +1 -0
  11. package/dist/cli/help.js +55 -0
  12. package/dist/cli/help.js.map +1 -0
  13. package/dist/cli/index.d.ts +8 -0
  14. package/dist/cli/index.d.ts.map +1 -0
  15. package/dist/cli/index.js +8 -0
  16. package/dist/cli/index.js.map +1 -0
  17. package/dist/cli/parser.d.ts +14 -0
  18. package/dist/cli/parser.d.ts.map +1 -0
  19. package/dist/cli/parser.js +113 -0
  20. package/dist/cli/parser.js.map +1 -0
  21. package/dist/cli/parser.test.d.ts +5 -0
  22. package/dist/cli/parser.test.d.ts.map +1 -0
  23. package/dist/cli/parser.test.js +221 -0
  24. package/dist/cli/parser.test.js.map +1 -0
  25. package/dist/cli.d.ts +6 -0
  26. package/dist/cli.d.ts.map +1 -0
  27. package/dist/cli.js +6 -0
  28. package/dist/cli.js.map +1 -0
  29. package/dist/commands/build.d.ts +11 -0
  30. package/dist/commands/build.d.ts.map +1 -0
  31. package/dist/commands/build.js +214 -0
  32. package/dist/commands/build.js.map +1 -0
  33. package/dist/commands/emit.d.ts +12 -0
  34. package/dist/commands/emit.d.ts.map +1 -0
  35. package/dist/commands/emit.js +311 -0
  36. package/dist/commands/emit.js.map +1 -0
  37. package/dist/commands/init.d.ts +15 -0
  38. package/dist/commands/init.d.ts.map +1 -0
  39. package/dist/commands/init.js +270 -0
  40. package/dist/commands/init.js.map +1 -0
  41. package/dist/commands/pack.d.ts +11 -0
  42. package/dist/commands/pack.d.ts.map +1 -0
  43. package/dist/commands/pack.js +92 -0
  44. package/dist/commands/pack.js.map +1 -0
  45. package/dist/commands/run.d.ts +11 -0
  46. package/dist/commands/run.d.ts.map +1 -0
  47. package/dist/commands/run.js +40 -0
  48. package/dist/commands/run.js.map +1 -0
  49. package/dist/config.d.ts +17 -0
  50. package/dist/config.d.ts.map +1 -0
  51. package/dist/config.js +160 -0
  52. package/dist/config.js.map +1 -0
  53. package/dist/config.test.d.ts +5 -0
  54. package/dist/config.test.d.ts.map +1 -0
  55. package/dist/config.test.js +283 -0
  56. package/dist/config.test.js.map +1 -0
  57. package/dist/index.d.ts +8 -0
  58. package/dist/index.d.ts.map +1 -0
  59. package/dist/index.js +20 -0
  60. package/dist/index.js.map +1 -0
  61. package/dist/types.d.ts +111 -0
  62. package/dist/types.d.ts.map +1 -0
  63. package/dist/types.js +5 -0
  64. package/dist/types.js.map +1 -0
  65. package/package.json +38 -0
  66. package/src/cli/constants.ts +5 -0
  67. package/src/cli/dispatcher.ts +129 -0
  68. package/src/cli/help.ts +56 -0
  69. package/src/cli/index.ts +8 -0
  70. package/src/cli/parser.test.ts +259 -0
  71. package/src/cli/parser.ts +128 -0
  72. package/src/cli.ts +6 -0
  73. package/src/commands/build.ts +264 -0
  74. package/src/commands/emit.ts +406 -0
  75. package/src/commands/init.ts +334 -0
  76. package/src/commands/pack.ts +114 -0
  77. package/src/commands/run.ts +51 -0
  78. package/src/config.test.ts +337 -0
  79. package/src/config.ts +205 -0
  80. package/src/index.ts +23 -0
  81. package/src/types.ts +121 -0
  82. package/tsconfig.json +18 -0
@@ -0,0 +1,56 @@
1
+ /**
2
+ * CLI help message
3
+ */
4
+
5
+ import { VERSION } from "./constants.js";
6
+
7
+ /**
8
+ * Show help message
9
+ */
10
+ export const showHelp = (): void => {
11
+ console.log(`
12
+ Tsonic - TypeScript to C# to NativeAOT compiler v${VERSION}
13
+
14
+ USAGE:
15
+ tsonic <command> [options]
16
+
17
+ COMMANDS:
18
+ project init Initialize a new Tsonic project
19
+ emit [entry] Generate C# code only
20
+ build [entry] Build native executable
21
+ run [entry] [-- args...] Build and run executable
22
+
23
+ GLOBAL OPTIONS:
24
+ -h, --help Show help
25
+ -v, --version Show version
26
+ -V, --verbose Verbose output
27
+ -q, --quiet Suppress output
28
+ -c, --config <file> Config file path (default: tsonic.json)
29
+ --runtime <mode> Runtime mode: js (default) or dotnet
30
+
31
+ EMIT/BUILD/RUN OPTIONS:
32
+ -s, --src <dir> Source root directory
33
+ -o, --out <path> Output directory (emit) or file (build)
34
+ -n, --namespace <ns> Root namespace override
35
+ -r, --rid <rid> Runtime identifier (e.g., linux-x64)
36
+ -O, --optimize <level> Optimization: size or speed
37
+ -k, --keep-temp Keep build artifacts
38
+ --no-strip Keep debug symbols
39
+
40
+ PROJECT INIT OPTIONS:
41
+ --runtime <mode> Runtime mode: js (default) or dotnet
42
+ --skip-types Skip installing type declarations
43
+ --types-version <ver> Version of type declarations to install
44
+
45
+ EXAMPLES:
46
+ tsonic project init
47
+ tsonic project init --runtime dotnet
48
+ tsonic emit src/app.ts
49
+ tsonic build src/app.ts --rid linux-x64
50
+ tsonic run src/app.ts -- --arg1 value1
51
+
52
+ LEARN MORE:
53
+ Documentation: https://tsonic.dev/docs
54
+ GitHub: https://github.com/tsoniclang/tsonic
55
+ `);
56
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * CLI - Public API
3
+ */
4
+
5
+ export { VERSION } from "./constants.js";
6
+ export { showHelp } from "./help.js";
7
+ export { parseArgs } from "./parser.js";
8
+ export { runCli } from "./dispatcher.js";
@@ -0,0 +1,259 @@
1
+ /**
2
+ * Tests for CLI argument parser
3
+ */
4
+
5
+ import { describe, it } from "mocha";
6
+ import { expect } from "chai";
7
+ import { parseArgs } from "./parser.js";
8
+
9
+ describe("CLI Parser", () => {
10
+ describe("parseArgs", () => {
11
+ describe("Commands", () => {
12
+ it("should parse build command", () => {
13
+ const result = parseArgs(["build"]);
14
+ expect(result.command).to.equal("build");
15
+ });
16
+
17
+ it("should parse run command", () => {
18
+ const result = parseArgs(["run"]);
19
+ expect(result.command).to.equal("run");
20
+ });
21
+
22
+ it("should parse emit command", () => {
23
+ const result = parseArgs(["emit"]);
24
+ expect(result.command).to.equal("emit");
25
+ });
26
+
27
+ it("should parse project:init as two-word command", () => {
28
+ const result = parseArgs(["project", "init"]);
29
+ expect(result.command).to.equal("project:init");
30
+ });
31
+
32
+ it("should parse help command from --help", () => {
33
+ const result = parseArgs(["--help"]);
34
+ expect(result.command).to.equal("help");
35
+ });
36
+
37
+ it("should parse help command from -h", () => {
38
+ const result = parseArgs(["-h"]);
39
+ expect(result.command).to.equal("help");
40
+ });
41
+
42
+ it("should parse version command from --version", () => {
43
+ const result = parseArgs(["--version"]);
44
+ expect(result.command).to.equal("version");
45
+ });
46
+
47
+ it("should parse version command from -v", () => {
48
+ const result = parseArgs(["-v"]);
49
+ expect(result.command).to.equal("version");
50
+ });
51
+ });
52
+
53
+ describe("Entry File", () => {
54
+ it("should parse entry file after command", () => {
55
+ const result = parseArgs(["run", "index.ts"]);
56
+ expect(result.command).to.equal("run");
57
+ expect(result.entryFile).to.equal("index.ts");
58
+ });
59
+
60
+ it("should parse entry file with path", () => {
61
+ const result = parseArgs(["build", "src/main.ts"]);
62
+ expect(result.command).to.equal("build");
63
+ expect(result.entryFile).to.equal("src/main.ts");
64
+ });
65
+
66
+ it("should handle no entry file", () => {
67
+ const result = parseArgs(["build"]);
68
+ expect(result.entryFile).to.equal(undefined);
69
+ });
70
+ });
71
+
72
+ describe("Options", () => {
73
+ it("should parse --verbose option", () => {
74
+ const result = parseArgs(["build", "--verbose"]);
75
+ expect(result.options.verbose).to.equal(true);
76
+ });
77
+
78
+ it("should parse -V short option for verbose", () => {
79
+ const result = parseArgs(["build", "-V"]);
80
+ expect(result.options.verbose).to.equal(true);
81
+ });
82
+
83
+ it("should parse --quiet option", () => {
84
+ const result = parseArgs(["build", "--quiet"]);
85
+ expect(result.options.quiet).to.equal(true);
86
+ });
87
+
88
+ it("should parse -q short option for quiet", () => {
89
+ const result = parseArgs(["build", "-q"]);
90
+ expect(result.options.quiet).to.equal(true);
91
+ });
92
+
93
+ it("should parse --config option with value", () => {
94
+ const result = parseArgs(["build", "--config", "tsonic.json"]);
95
+ expect(result.options.config).to.equal("tsonic.json");
96
+ });
97
+
98
+ it("should parse -c short option for config", () => {
99
+ const result = parseArgs(["build", "-c", "custom.json"]);
100
+ expect(result.options.config).to.equal("custom.json");
101
+ });
102
+
103
+ it("should parse --src option with value", () => {
104
+ const result = parseArgs(["build", "--src", "source"]);
105
+ expect(result.options.src).to.equal("source");
106
+ });
107
+
108
+ it("should parse -s short option for src", () => {
109
+ const result = parseArgs(["build", "-s", "app"]);
110
+ expect(result.options.src).to.equal("app");
111
+ });
112
+
113
+ it("should parse --out option with value", () => {
114
+ const result = parseArgs(["build", "--out", "output"]);
115
+ expect(result.options.out).to.equal("output");
116
+ });
117
+
118
+ it("should parse -o short option for out", () => {
119
+ const result = parseArgs(["build", "-o", "dist"]);
120
+ expect(result.options.out).to.equal("dist");
121
+ });
122
+
123
+ it("should parse --namespace option with value", () => {
124
+ const result = parseArgs(["build", "--namespace", "MyApp"]);
125
+ expect(result.options.namespace).to.equal("MyApp");
126
+ });
127
+
128
+ it("should parse -n short option for namespace", () => {
129
+ const result = parseArgs(["build", "-n", "App"]);
130
+ expect(result.options.namespace).to.equal("App");
131
+ });
132
+
133
+ it("should parse --rid option with value", () => {
134
+ const result = parseArgs(["build", "--rid", "linux-x64"]);
135
+ expect(result.options.rid).to.equal("linux-x64");
136
+ });
137
+
138
+ it("should parse -r short option for rid", () => {
139
+ const result = parseArgs(["build", "-r", "win-x64"]);
140
+ expect(result.options.rid).to.equal("win-x64");
141
+ });
142
+
143
+ it("should parse --optimize option with speed", () => {
144
+ const result = parseArgs(["build", "--optimize", "speed"]);
145
+ expect(result.options.optimize).to.equal("speed");
146
+ });
147
+
148
+ it("should parse --optimize option with size", () => {
149
+ const result = parseArgs(["build", "--optimize", "size"]);
150
+ expect(result.options.optimize).to.equal("size");
151
+ });
152
+
153
+ it("should parse -O short option for optimize", () => {
154
+ const result = parseArgs(["build", "-O", "speed"]);
155
+ expect(result.options.optimize).to.equal("speed");
156
+ });
157
+
158
+ it("should parse --keep-temp option", () => {
159
+ const result = parseArgs(["build", "--keep-temp"]);
160
+ expect(result.options.keepTemp).to.equal(true);
161
+ });
162
+
163
+ it("should parse -k short option for keep-temp", () => {
164
+ const result = parseArgs(["build", "-k"]);
165
+ expect(result.options.keepTemp).to.equal(true);
166
+ });
167
+
168
+ it("should parse --no-strip option", () => {
169
+ const result = parseArgs(["build", "--no-strip"]);
170
+ expect(result.options.noStrip).to.equal(true);
171
+ });
172
+ });
173
+
174
+ describe("Program Arguments", () => {
175
+ it("should parse program arguments after --", () => {
176
+ const result = parseArgs(["run", "index.ts", "--", "arg1", "arg2"]);
177
+ expect(result.programArgs).to.deep.equal(["arg1", "arg2"]);
178
+ });
179
+
180
+ it("should handle options in program arguments", () => {
181
+ const result = parseArgs([
182
+ "run",
183
+ "index.ts",
184
+ "--",
185
+ "--verbose",
186
+ "-o",
187
+ "out",
188
+ ]);
189
+ expect(result.programArgs).to.deep.equal(["--verbose", "-o", "out"]);
190
+ });
191
+
192
+ it("should handle empty program arguments", () => {
193
+ const result = parseArgs(["run", "index.ts"]);
194
+ expect(result.programArgs).to.deep.equal([]);
195
+ });
196
+ });
197
+
198
+ describe("Complex Scenarios", () => {
199
+ it("should parse command with entry file and multiple options", () => {
200
+ const result = parseArgs([
201
+ "build",
202
+ "src/main.ts",
203
+ "--namespace",
204
+ "MyApp",
205
+ "--out",
206
+ "dist",
207
+ "--verbose",
208
+ ]);
209
+ expect(result.command).to.equal("build");
210
+ expect(result.entryFile).to.equal("src/main.ts");
211
+ expect(result.options.namespace).to.equal("MyApp");
212
+ expect(result.options.out).to.equal("dist");
213
+ expect(result.options.verbose).to.equal(true);
214
+ });
215
+
216
+ it("should parse run command with entry, options, and program args", () => {
217
+ const result = parseArgs([
218
+ "run",
219
+ "index.ts",
220
+ "--verbose",
221
+ "--",
222
+ "programArg1",
223
+ "programArg2",
224
+ ]);
225
+ expect(result.command).to.equal("run");
226
+ expect(result.entryFile).to.equal("index.ts");
227
+ expect(result.options.verbose).to.equal(true);
228
+ expect(result.programArgs).to.deep.equal([
229
+ "programArg1",
230
+ "programArg2",
231
+ ]);
232
+ });
233
+
234
+ it("should handle empty args array", () => {
235
+ const result = parseArgs([]);
236
+ expect(result.command).to.equal("");
237
+ expect(result.entryFile).to.equal(undefined);
238
+ expect(result.options).to.deep.equal({});
239
+ expect(result.programArgs).to.deep.equal([]);
240
+ });
241
+
242
+ it("should handle mixed short and long options", () => {
243
+ const result = parseArgs([
244
+ "build",
245
+ "-n",
246
+ "App",
247
+ "--out",
248
+ "dist",
249
+ "-V",
250
+ "--keep-temp",
251
+ ]);
252
+ expect(result.options.namespace).to.equal("App");
253
+ expect(result.options.out).to.equal("dist");
254
+ expect(result.options.verbose).to.equal(true);
255
+ expect(result.options.keepTemp).to.equal(true);
256
+ });
257
+ });
258
+ });
259
+ });
@@ -0,0 +1,128 @@
1
+ /**
2
+ * CLI argument parser
3
+ */
4
+
5
+ import type { CliOptions } from "../types.js";
6
+
7
+ /**
8
+ * Parse CLI arguments
9
+ */
10
+ export const parseArgs = (
11
+ args: string[]
12
+ ): {
13
+ command: string;
14
+ entryFile?: string;
15
+ options: CliOptions;
16
+ programArgs?: string[];
17
+ } => {
18
+ const options: CliOptions = {};
19
+ let command = "";
20
+ let entryFile: string | undefined;
21
+ const programArgs: string[] = [];
22
+ let captureProgramArgs = false;
23
+
24
+ for (let i = 0; i < args.length; i++) {
25
+ const arg = args[i];
26
+ if (!arg) continue; // Skip if undefined
27
+
28
+ // Separator for program arguments
29
+ if (arg === "--") {
30
+ captureProgramArgs = true;
31
+ continue;
32
+ }
33
+
34
+ if (captureProgramArgs) {
35
+ programArgs.push(arg);
36
+ continue;
37
+ }
38
+
39
+ // Commands
40
+ if (!command && !arg.startsWith("-")) {
41
+ command = arg;
42
+ // Handle "project init" as two-word command
43
+ const nextArg = args[i + 1];
44
+ if (command === "project" && nextArg === "init") {
45
+ command = "project:init";
46
+ i++;
47
+ }
48
+ continue;
49
+ }
50
+
51
+ // Entry file (first non-option after command)
52
+ if (command && !entryFile && !arg.startsWith("-")) {
53
+ entryFile = arg;
54
+ continue;
55
+ }
56
+
57
+ // Options
58
+ switch (arg) {
59
+ case "-h":
60
+ case "--help":
61
+ options.verbose = true; // reuse for help flag
62
+ return { command: "help", options: {} };
63
+ case "-v":
64
+ case "--version":
65
+ return { command: "version", options: {} };
66
+ case "-V":
67
+ case "--verbose":
68
+ options.verbose = true;
69
+ break;
70
+ case "-q":
71
+ case "--quiet":
72
+ options.quiet = true;
73
+ break;
74
+ case "-c":
75
+ case "--config":
76
+ options.config = args[++i] ?? "";
77
+ break;
78
+ case "-s":
79
+ case "--src":
80
+ options.src = args[++i] ?? "";
81
+ break;
82
+ case "-o":
83
+ case "--out":
84
+ options.out = args[++i] ?? "";
85
+ break;
86
+ case "-n":
87
+ case "--namespace":
88
+ options.namespace = args[++i] ?? "";
89
+ break;
90
+ case "-r":
91
+ case "--rid":
92
+ options.rid = args[++i] ?? "";
93
+ break;
94
+ case "--runtime":
95
+ options.runtime = (args[++i] ?? "js") as "js" | "dotnet";
96
+ break;
97
+ case "--skip-types":
98
+ options.skipTypes = true;
99
+ break;
100
+ case "--types-version":
101
+ options.typesVersion = args[++i] ?? "";
102
+ break;
103
+ case "-O":
104
+ case "--optimize":
105
+ options.optimize = (args[++i] ?? "speed") as "size" | "speed";
106
+ break;
107
+ case "-k":
108
+ case "--keep-temp":
109
+ options.keepTemp = true;
110
+ break;
111
+ case "--no-strip":
112
+ options.noStrip = true;
113
+ break;
114
+ case "-L":
115
+ case "--lib":
116
+ {
117
+ const libPath = args[++i] ?? "";
118
+ if (libPath) {
119
+ options.lib = options.lib || [];
120
+ options.lib.push(libPath);
121
+ }
122
+ }
123
+ break;
124
+ }
125
+ }
126
+
127
+ return { command, entryFile, options, programArgs };
128
+ };
package/src/cli.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * CLI argument parsing and command dispatch
3
+ * Main dispatcher - re-exports from cli/ subdirectory
4
+ */
5
+
6
+ export { VERSION, showHelp, parseArgs, runCli } from "./cli/index.js";