ttsc 0.7.1 → 0.7.3

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 (53) hide show
  1. package/README.md +5 -6
  2. package/cmd/platform/main.go +86 -76
  3. package/cmd/ttsc/api_compile.go +122 -105
  4. package/cmd/ttsc/api_transform.go +57 -49
  5. package/cmd/ttsc/build.go +116 -103
  6. package/cmd/ttsc/main.go +117 -117
  7. package/cmd/ttsc/main_test.go +191 -191
  8. package/driver/host.go +9 -9
  9. package/driver/program.go +285 -285
  10. package/driver/rewrite.go +225 -219
  11. package/lib/compiler/internal/compileProjectInMemory.js +3 -3
  12. package/lib/compiler/internal/compileProjectInMemory.js.map +1 -1
  13. package/lib/compiler/internal/resolveEmittedJavaScript.js +2 -1
  14. package/lib/compiler/internal/resolveEmittedJavaScript.js.map +1 -1
  15. package/lib/compiler/internal/resolveTsgo.js.map +1 -1
  16. package/lib/compiler/internal/runBuild.js.map +1 -1
  17. package/lib/launcher/internal/getCompilerVersionText.js +1 -2
  18. package/lib/launcher/internal/getCompilerVersionText.js.map +1 -1
  19. package/lib/launcher/internal/prepareExecution.js +92 -9
  20. package/lib/launcher/internal/prepareExecution.js.map +1 -1
  21. package/lib/launcher/internal/runTtsc.js +30 -4
  22. package/lib/launcher/internal/runTtsc.js.map +1 -1
  23. package/lib/launcher/internal/runTtsx.js +7 -2
  24. package/lib/launcher/internal/runTtsx.js.map +1 -1
  25. package/lib/plugin/internal/buildSourcePlugin.js.map +1 -1
  26. package/package.json +9 -9
  27. package/shim/ast/lint.go +171 -171
  28. package/shim/ast/shim.go +83 -83
  29. package/shim/bundled/shim.go +5 -1
  30. package/shim/checker/shim.go +79 -79
  31. package/shim/compiler/shim.go +16 -1
  32. package/shim/core/shim.go +10 -10
  33. package/shim/diagnosticwriter/lint.go +79 -79
  34. package/shim/diagnosticwriter/shim.go +19 -19
  35. package/shim/parser/shim.go +7 -1
  36. package/shim/scanner/shim.go +39 -1
  37. package/shim/tsoptions/shim.go +51 -1
  38. package/shim/tspath/shim.go +72 -1
  39. package/shim/vfs/cachedvfs/shim.go +1 -1
  40. package/shim/vfs/osvfs/shim.go +1 -1
  41. package/shim/vfs/shim.go +4 -1
  42. package/src/compiler/internal/compileProjectInMemory.ts +6 -3
  43. package/src/compiler/internal/resolveEmittedJavaScript.ts +2 -1
  44. package/src/compiler/internal/resolveTsgo.ts +0 -1
  45. package/src/compiler/internal/runBuild.ts +4 -1
  46. package/src/launcher/internal/getCompilerVersionText.ts +1 -2
  47. package/src/launcher/internal/prepareExecution.ts +115 -9
  48. package/src/launcher/internal/runTtsc.ts +35 -4
  49. package/src/launcher/internal/runTtsx.ts +16 -5
  50. package/src/plugin/internal/buildSourcePlugin.ts +7 -2
  51. package/test/platform.test.ts +1 -3
  52. package/test/tsgo.test.ts +6 -1
  53. package/tools/gen_shims/main.go +409 -409
package/README.md CHANGED
@@ -41,17 +41,16 @@ npx ttsc --noEmit
41
41
  npx ttsc --watch
42
42
  ```
43
43
 
44
- Clear cached transformer binaries when developing or debugging a plugin:
44
+ Prebuild or clear cached source-plugin binaries:
45
45
 
46
46
  ```bash
47
+ npx ttsc prepare
47
48
  npx ttsc clean
48
49
  ```
49
50
 
50
51
  ## Plugin Configuration
51
52
 
52
- Open your project's `tsconfig.json`, then add plugin entries under `compilerOptions.plugins`. If the file already has `compilerOptions`, merge the `plugins` array into the existing object.
53
-
54
- Each plugin module is a JavaScript descriptor for an ordered native transformer backend; the transform implementation itself runs in Go.
53
+ Add plugin entries under `compilerOptions.plugins` in your `tsconfig.json`.
55
54
 
56
55
  ```json
57
56
  {
@@ -137,14 +136,14 @@ It is transformed into dedicated JavaScript:
137
136
  > console.log(matched); // true
138
137
  > ```
139
138
 
140
- `ttsc` runs this transform during build, and `ttsx` runs through the same transform path during execution.
139
+ `ttsc` applies this transform at build time; `ttsx` applies the same transform when running the file.
141
140
 
142
141
  ## List of Plugins
143
142
 
144
143
  `ttsc` ships a few small utility plugins in this repository.
145
144
 
146
145
  - [`@ttsc/banner`](https://github.com/samchon/ttsc/tree/master/packages/banner): adds banner comments to emitted JS and declarations.
147
- - [`@ttsc/lint`](https://github.com/samchon/ttsc/tree/master/packages/lint): fails compilation on lint violations.
146
+ - [`@ttsc/lint`](https://github.com/samchon/ttsc/tree/master/packages/lint): type-check + lint in one compile pass, ~20x faster than `eslint` in theory.
148
147
  - [`@ttsc/paths`](https://github.com/samchon/ttsc/tree/master/packages/paths): rewrites path aliases in emitted imports.
149
148
  - [`@ttsc/strip`](https://github.com/samchon/ttsc/tree/master/packages/strip): removes configured calls and `debugger` statements.
150
149
 
@@ -1,73 +1,83 @@
1
+ // Platform helper binary shipped by the per-OS `ttsc-*` packages.
2
+ //
3
+ // The real compiler and runner commands live in the JavaScript launchers so
4
+ // they can resolve the consuming project's `@typescript/native-preview` and
5
+ // plugin descriptors. This binary only supplies version/platform metadata and
6
+ // a tiny demo command for package smoke tests.
1
7
  package main
2
8
 
3
9
  import (
4
- "flag"
5
- "fmt"
6
- "io"
7
- "os"
8
- "runtime"
9
- "strings"
10
+ "flag"
11
+ "fmt"
12
+ "io"
13
+ "os"
14
+ "runtime"
15
+ "strings"
10
16
  )
11
17
 
12
18
  var (
13
- version = "0.0.0-dev"
14
- commit = "dev"
15
- date = "unknown"
19
+ // These values are overwritten by release builds through -ldflags. The
20
+ // defaults keep local `go run` and fixture builds deterministic.
21
+ version = "0.0.0-dev"
22
+ commit = "dev"
23
+ date = "unknown"
16
24
  )
17
25
 
18
26
  var (
19
- stdout io.Writer = os.Stdout
20
- stderr io.Writer = os.Stderr
27
+ // Package-level writers keep CLI tests simple and avoid global os.Stdout /
28
+ // os.Stderr patching.
29
+ stdout io.Writer = os.Stdout
30
+ stderr io.Writer = os.Stderr
21
31
  )
22
32
 
23
33
  func main() {
24
- os.Exit(run(os.Args[1:]))
34
+ os.Exit(run(os.Args[1:]))
25
35
  }
26
36
 
27
37
  func run(args []string) int {
28
- if len(args) == 0 {
29
- printHelp(stdout)
30
- return 0
31
- }
38
+ if len(args) == 0 {
39
+ printHelp(stdout)
40
+ return 0
41
+ }
32
42
 
33
- switch args[0] {
34
- case "-h", "--help", "help":
35
- printHelp(stdout)
36
- return 0
37
- case "-v", "--version", "version":
38
- printVersion(stdout)
39
- return 0
40
- case "demo":
41
- return runDemo(args[1:])
42
- case "build", "check":
43
- fmt.Fprintf(
44
- stderr,
45
- "ttsc platform helper: %s is provided by the JavaScript ttsc CLI using the consuming project's @typescript/native-preview, or by a plugin-selected native sidecar.\n",
46
- args[0],
47
- )
48
- return 2
49
- default:
50
- fmt.Fprintf(stderr, "ttsc platform helper: unknown command %q\n", args[0])
51
- fmt.Fprintln(stderr, `ttsc platform helper: run "ttsc --help" through the JavaScript CLI for compiler usage.`)
52
- return 2
53
- }
43
+ switch args[0] {
44
+ case "-h", "--help", "help":
45
+ printHelp(stdout)
46
+ return 0
47
+ case "-v", "--version", "version":
48
+ printVersion(stdout)
49
+ return 0
50
+ case "demo":
51
+ return runDemo(args[1:])
52
+ case "build", "check":
53
+ fmt.Fprintf(
54
+ stderr,
55
+ "ttsc platform helper: %s is provided by the JavaScript ttsc CLI using the consuming project's @typescript/native-preview, or by a plugin-selected native sidecar.\n",
56
+ args[0],
57
+ )
58
+ return 2
59
+ default:
60
+ fmt.Fprintf(stderr, "ttsc platform helper: unknown command %q\n", args[0])
61
+ fmt.Fprintln(stderr, `ttsc platform helper: run "ttsc --help" through the JavaScript CLI for compiler usage.`)
62
+ return 2
63
+ }
54
64
  }
55
65
 
56
66
  func printVersion(w io.Writer) {
57
- fmt.Fprintf(
58
- w,
59
- "ttsc platform helper %s (commit %s, built %s, %s/%s, go %s)\n",
60
- version,
61
- commit,
62
- date,
63
- runtime.GOOS,
64
- runtime.GOARCH,
65
- runtime.Version(),
66
- )
67
+ fmt.Fprintf(
68
+ w,
69
+ "ttsc platform helper %s (commit %s, built %s, %s/%s, go %s)\n",
70
+ version,
71
+ commit,
72
+ date,
73
+ runtime.GOOS,
74
+ runtime.GOARCH,
75
+ runtime.Version(),
76
+ )
67
77
  }
68
78
 
69
79
  func printHelp(w io.Writer) {
70
- fmt.Fprintln(w, strings.TrimSpace(`
80
+ fmt.Fprintln(w, strings.TrimSpace(`
71
81
  ttsc platform helper.
72
82
 
73
83
  This binary is a small compatibility helper shipped by @ttsc platform packages.
@@ -82,37 +92,37 @@ Usage:
82
92
  }
83
93
 
84
94
  func runDemo(args []string) int {
85
- fs := flag.NewFlagSet("demo", flag.ContinueOnError)
86
- fs.SetOutput(stderr)
87
- typ := fs.String("type", "string", "atomic type to simulate")
88
- if err := fs.Parse(args); err != nil {
89
- return 2
90
- }
95
+ fs := flag.NewFlagSet("demo", flag.ContinueOnError)
96
+ fs.SetOutput(stderr)
97
+ typ := fs.String("type", "string", "atomic type to simulate")
98
+ if err := fs.Parse(args); err != nil {
99
+ return 2
100
+ }
91
101
 
92
- arrow, err := demoArrow(*typ)
93
- if err != nil {
94
- fmt.Fprintf(stderr, "ttsc platform helper demo: %v\n", err)
95
- return 2
96
- }
102
+ arrow, err := demoArrow(*typ)
103
+ if err != nil {
104
+ fmt.Fprintf(stderr, "ttsc platform helper demo: %v\n", err)
105
+ return 2
106
+ }
97
107
 
98
- fmt.Fprintf(stdout, "// demo<%s> -> emitted by ttsc platform helper %s\n", *typ, version)
99
- fmt.Fprintln(stdout, arrow)
100
- return 0
108
+ fmt.Fprintf(stdout, "// demo<%s> -> emitted by ttsc platform helper %s\n", *typ, version)
109
+ fmt.Fprintln(stdout, arrow)
110
+ return 0
101
111
  }
102
112
 
103
113
  func demoArrow(name string) (string, error) {
104
- switch strings.ToLower(name) {
105
- case "any":
106
- return "(input) => true", nil
107
- case "boolean":
108
- return `(input) => "boolean" === typeof input`, nil
109
- case "number":
110
- return `(input) => "number" === typeof input`, nil
111
- case "bigint":
112
- return `(input) => "bigint" === typeof input`, nil
113
- case "string", "":
114
- return `(input) => "string" === typeof input`, nil
115
- default:
116
- return "", fmt.Errorf("unknown --type value %q (want: string|number|boolean|bigint|any)", name)
117
- }
114
+ switch strings.ToLower(name) {
115
+ case "any":
116
+ return "(input) => true", nil
117
+ case "boolean":
118
+ return `(input) => "boolean" === typeof input`, nil
119
+ case "number":
120
+ return `(input) => "number" === typeof input`, nil
121
+ case "bigint":
122
+ return `(input) => "bigint" === typeof input`, nil
123
+ case "string", "":
124
+ return `(input) => "string" === typeof input`, nil
125
+ default:
126
+ return "", fmt.Errorf("unknown --type value %q (want: string|number|boolean|bigint|any)", name)
127
+ }
118
128
  }
@@ -1,134 +1,151 @@
1
+ // JSON API adapter for in-process-style project compilation.
2
+ //
3
+ // `api-compile` is consumed by the TypeScript wrapper when it needs a stable,
4
+ // machine-readable result from the native compiler host. The command always
5
+ // emits into memory and serializes diagnostics plus output text; it never
6
+ // writes generated files into the caller's project tree.
1
7
  package main
2
8
 
3
9
  import (
4
- "encoding/json"
5
- "flag"
6
- "fmt"
7
- "os"
8
- "path/filepath"
9
- "strings"
10
+ "encoding/json"
11
+ "flag"
12
+ "fmt"
13
+ "os"
14
+ "path/filepath"
15
+ "strings"
10
16
 
11
- shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
17
+ shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
12
18
 
13
- "github.com/samchon/ttsc/packages/ttsc/driver"
19
+ "github.com/samchon/ttsc/packages/ttsc/driver"
14
20
  )
15
21
 
16
22
  type apiCompileResult struct {
17
- Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
18
- Output map[string]string `json:"output"`
23
+ // Diagnostics is omitted only when the compiler produced no diagnostics.
24
+ // Output is still returned in diagnostic cases because TypeScript-Go may
25
+ // produce partial emit text before a build-failing error is reported.
26
+ Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
27
+ Output map[string]string `json:"output"`
19
28
  }
20
29
 
30
+ // apiCompileDiagnostic mirrors the public TypeScript-side diagnostic DTO.
31
+ // The JSON keys intentionally use TypeScript naming (`messageText`,
32
+ // `character`) instead of Go naming so callers can pass the data through
33
+ // without remapping.
21
34
  type apiCompileDiagnostic struct {
22
- File *string `json:"file"`
23
- Category string `json:"category"`
24
- Code int32 `json:"code"`
25
- Start *int `json:"start,omitempty"`
26
- Length *int `json:"length,omitempty"`
27
- Line int `json:"line,omitempty"`
28
- Character int `json:"character,omitempty"`
29
- MessageText string `json:"messageText"`
35
+ File *string `json:"file"`
36
+ Category string `json:"category"`
37
+ Code int32 `json:"code"`
38
+ Start *int `json:"start,omitempty"`
39
+ Length *int `json:"length,omitempty"`
40
+ Line int `json:"line,omitempty"`
41
+ Character int `json:"character,omitempty"`
42
+ MessageText string `json:"messageText"`
30
43
  }
31
44
 
32
45
  func runAPICompile(args []string) int {
33
- fs := flag.NewFlagSet("api-compile", flag.ContinueOnError)
34
- fs.SetOutput(stderr)
35
- tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
36
- cwdOverride := fs.String("cwd", "", "override the working directory")
37
- if err := fs.Parse(args); err != nil {
38
- return 2
39
- }
46
+ fs := flag.NewFlagSet("api-compile", flag.ContinueOnError)
47
+ fs.SetOutput(stderr)
48
+ tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
49
+ cwdOverride := fs.String("cwd", "", "override the working directory")
50
+ if err := fs.Parse(args); err != nil {
51
+ return 2
52
+ }
40
53
 
41
- cwd, ok := resolveBuildCwd(*cwdOverride)
42
- if !ok {
43
- return 2
44
- }
54
+ cwd, ok := resolveBuildCwd(*cwdOverride)
55
+ if !ok {
56
+ return 2
57
+ }
45
58
 
46
- prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
47
- ForceEmit: true,
48
- })
49
- if err != nil {
50
- fmt.Fprintf(stderr, "ttsc api-compile: %v\n", err)
51
- return 2
52
- }
53
- if prog != nil {
54
- defer prog.Close()
55
- diags = append(diags, prog.Diagnostics()...)
56
- }
59
+ prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
60
+ ForceEmit: true,
61
+ })
62
+ if err != nil {
63
+ fmt.Fprintf(stderr, "ttsc api-compile: %v\n", err)
64
+ return 2
65
+ }
66
+ if prog != nil {
67
+ defer prog.Close()
68
+ diags = append(diags, prog.Diagnostics()...)
69
+ }
57
70
 
58
- output := map[string]string{}
59
- if prog != nil {
60
- rewrites := driver.NewRewriteSet()
61
- writeFile := shimcompiler.WriteFile(
62
- func(fileName, text string, _ *shimcompiler.WriteFileData) error {
63
- output[apiOutputKey(cwd, fileName)] = text
64
- return nil
65
- },
66
- )
67
- _, emitDiags, err := prog.EmitAll(rewrites, writeFile)
68
- if err != nil {
69
- fmt.Fprintf(stderr, "ttsc api-compile: emit failed: %v\n", err)
70
- return 3
71
- }
72
- diags = append(diags, emitDiags...)
73
- }
71
+ output := map[string]string{}
72
+ if prog != nil {
73
+ rewrites := driver.NewRewriteSet()
74
+ // Capture WriteFile output in a map keyed by project-relative paths. This
75
+ // gives the JS API a deterministic object and avoids touching outDir.
76
+ writeFile := shimcompiler.WriteFile(
77
+ func(fileName, text string, _ *shimcompiler.WriteFileData) error {
78
+ output[apiOutputKey(cwd, fileName)] = text
79
+ return nil
80
+ },
81
+ )
82
+ _, emitDiags, err := prog.EmitAll(rewrites, writeFile)
83
+ if err != nil {
84
+ fmt.Fprintf(stderr, "ttsc api-compile: emit failed: %v\n", err)
85
+ return 3
86
+ }
87
+ diags = append(diags, emitDiags...)
88
+ }
74
89
 
75
- result := apiCompileResult{
76
- Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
77
- Output: output,
78
- }
79
- for _, diag := range diags {
80
- result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
81
- }
90
+ result := apiCompileResult{
91
+ Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
92
+ Output: output,
93
+ }
94
+ for _, diag := range diags {
95
+ result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
96
+ }
82
97
 
83
- data, err := json.Marshal(result)
84
- if err != nil {
85
- fmt.Fprintf(stderr, "ttsc api-compile: result marshal failed: %v\n", err)
86
- return 3
87
- }
88
- fmt.Fprintln(stdout, string(data))
89
- if driver.CountErrors(diags) > 0 {
90
- return 2
91
- }
92
- return 0
98
+ data, err := json.Marshal(result)
99
+ if err != nil {
100
+ fmt.Fprintf(stderr, "ttsc api-compile: result marshal failed: %v\n", err)
101
+ return 3
102
+ }
103
+ fmt.Fprintln(stdout, string(data))
104
+ if driver.CountErrors(diags) > 0 {
105
+ return 2
106
+ }
107
+ return 0
93
108
  }
94
109
 
95
110
  func resolveBuildCwd(cwdOverride string) (string, bool) {
96
- if cwdOverride != "" {
97
- return cwdOverride, true
98
- }
99
- cwd, err := os.Getwd()
100
- if err != nil {
101
- fmt.Fprintf(stderr, "ttsc: could not get working directory: %v\n", err)
102
- return "", false
103
- }
104
- return cwd, true
111
+ if cwdOverride != "" {
112
+ return cwdOverride, true
113
+ }
114
+ cwd, err := os.Getwd()
115
+ if err != nil {
116
+ fmt.Fprintf(stderr, "ttsc: could not get working directory: %v\n", err)
117
+ return "", false
118
+ }
119
+ return cwd, true
105
120
  }
106
121
 
107
122
  func toAPICompileDiagnostic(diag driver.Diagnostic) apiCompileDiagnostic {
108
- var file *string
109
- if diag.File != "" {
110
- value := diag.File
111
- file = &value
112
- }
113
- category := "error"
114
- if diag.Severity == driver.SeverityWarning {
115
- category = "warning"
116
- }
117
- return apiCompileDiagnostic{
118
- File: file,
119
- Category: category,
120
- Code: diag.Code,
121
- Start: diag.Start,
122
- Length: diag.Length,
123
- Line: diag.Line,
124
- Character: diag.Column,
125
- MessageText: diag.Message,
126
- }
123
+ var file *string
124
+ if diag.File != "" {
125
+ value := diag.File
126
+ file = &value
127
+ }
128
+ category := "error"
129
+ if diag.Severity == driver.SeverityWarning {
130
+ category = "warning"
131
+ }
132
+ return apiCompileDiagnostic{
133
+ File: file,
134
+ Category: category,
135
+ Code: diag.Code,
136
+ Start: diag.Start,
137
+ Length: diag.Length,
138
+ Line: diag.Line,
139
+ Character: diag.Column,
140
+ MessageText: diag.Message,
141
+ }
127
142
  }
128
143
 
129
144
  func apiOutputKey(cwd, fileName string) string {
130
- if rel, err := filepath.Rel(cwd, fileName); err == nil && !strings.HasPrefix(rel, "..") {
131
- return filepath.ToSlash(rel)
132
- }
133
- return filepath.ToSlash(fileName)
145
+ // Relative keys are the API contract for files inside the project. Absolute
146
+ // keys are preserved for uncommon compiler outputs outside cwd.
147
+ if rel, err := filepath.Rel(cwd, fileName); err == nil && !strings.HasPrefix(rel, "..") {
148
+ return filepath.ToSlash(rel)
149
+ }
150
+ return filepath.ToSlash(fileName)
134
151
  }
@@ -1,64 +1,72 @@
1
+ // JSON API adapter for source-level transformation results.
2
+ //
3
+ // `api-transform` returns the TypeScript source files that TypeScript-Go
4
+ // parsed for the owning project. It is the no-emit companion to api-compile:
5
+ // diagnostics are still collected, but the result surface is TypeScript text
6
+ // rather than emitted JavaScript.
1
7
  package main
2
8
 
3
9
  import (
4
- "encoding/json"
5
- "flag"
6
- "fmt"
10
+ "encoding/json"
11
+ "flag"
12
+ "fmt"
7
13
 
8
- "github.com/samchon/ttsc/packages/ttsc/driver"
14
+ "github.com/samchon/ttsc/packages/ttsc/driver"
9
15
  )
10
16
 
11
17
  type apiTransformResult struct {
12
- Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
13
- TypeScript map[string]string `json:"typescript"`
18
+ // TypeScript contains every non-library source file visible through the
19
+ // Program facade, keyed the same way api-compile keys emitted files.
20
+ Diagnostics []apiCompileDiagnostic `json:"diagnostics,omitempty"`
21
+ TypeScript map[string]string `json:"typescript"`
14
22
  }
15
23
 
16
24
  func runAPITransform(args []string) int {
17
- fs := flag.NewFlagSet("api-transform", flag.ContinueOnError)
18
- fs.SetOutput(stderr)
19
- tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
20
- cwdOverride := fs.String("cwd", "", "override the working directory")
21
- if err := fs.Parse(args); err != nil {
22
- return 2
23
- }
25
+ fs := flag.NewFlagSet("api-transform", flag.ContinueOnError)
26
+ fs.SetOutput(stderr)
27
+ tsconfigPath := fs.String("tsconfig", "tsconfig.json", "path to tsconfig.json")
28
+ cwdOverride := fs.String("cwd", "", "override the working directory")
29
+ if err := fs.Parse(args); err != nil {
30
+ return 2
31
+ }
24
32
 
25
- cwd, ok := resolveBuildCwd(*cwdOverride)
26
- if !ok {
27
- return 2
28
- }
33
+ cwd, ok := resolveBuildCwd(*cwdOverride)
34
+ if !ok {
35
+ return 2
36
+ }
29
37
 
30
- prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
31
- ForceNoEmit: true,
32
- })
33
- if err != nil {
34
- fmt.Fprintf(stderr, "ttsc api-transform: %v\n", err)
35
- return 2
36
- }
37
- typescript := map[string]string{}
38
- if prog != nil {
39
- defer prog.Close()
40
- for _, file := range prog.SourceFiles() {
41
- typescript[apiOutputKey(cwd, file.FileName())] = file.Text()
42
- }
43
- diags = append(diags, prog.Diagnostics()...)
44
- }
38
+ prog, diags, err := driver.LoadProgram(cwd, *tsconfigPath, driver.LoadProgramOptions{
39
+ ForceNoEmit: true,
40
+ })
41
+ if err != nil {
42
+ fmt.Fprintf(stderr, "ttsc api-transform: %v\n", err)
43
+ return 2
44
+ }
45
+ typescript := map[string]string{}
46
+ if prog != nil {
47
+ defer prog.Close()
48
+ for _, file := range prog.SourceFiles() {
49
+ typescript[apiOutputKey(cwd, file.FileName())] = file.Text()
50
+ }
51
+ diags = append(diags, prog.Diagnostics()...)
52
+ }
45
53
 
46
- result := apiTransformResult{
47
- Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
48
- TypeScript: typescript,
49
- }
50
- for _, diag := range diags {
51
- result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
52
- }
54
+ result := apiTransformResult{
55
+ Diagnostics: make([]apiCompileDiagnostic, 0, len(diags)),
56
+ TypeScript: typescript,
57
+ }
58
+ for _, diag := range diags {
59
+ result.Diagnostics = append(result.Diagnostics, toAPICompileDiagnostic(diag))
60
+ }
53
61
 
54
- data, err := json.Marshal(result)
55
- if err != nil {
56
- fmt.Fprintf(stderr, "ttsc api-transform: result marshal failed: %v\n", err)
57
- return 3
58
- }
59
- fmt.Fprintln(stdout, string(data))
60
- if driver.CountErrors(diags) > 0 {
61
- return 2
62
- }
63
- return 0
62
+ data, err := json.Marshal(result)
63
+ if err != nil {
64
+ fmt.Fprintf(stderr, "ttsc api-transform: result marshal failed: %v\n", err)
65
+ return 3
66
+ }
67
+ fmt.Fprintln(stdout, string(data))
68
+ if driver.CountErrors(diags) > 0 {
69
+ return 2
70
+ }
71
+ return 0
64
72
  }