From 4c1771ef196d66b740a86582c0f2f3f2cd883acc Mon Sep 17 00:00:00 2001 From: Ola Okelola Date: Sun, 17 Nov 2024 18:34:33 -0800 Subject: [PATCH] fix swc when parsing schema --- internal/cmd/cmd.go | 39 ++++++++++++++++++++++++++++ internal/graphql/generate_ts_code.go | 30 ++------------------- internal/schema/input/parse_ts.go | 7 ++++- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 9b4f3fcea..1f66d4e52 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -39,6 +39,45 @@ type CommandInfo struct { UseSwc bool } +func (cmdInfo *CommandInfo) MaybeSetupSwcrc(dirPath string) func() { + swcPath := filepath.Join(dirPath, ".swcrc") + _, err := os.Stat(swcPath) + + shouldCleanup := false + cleanup := func() { + if shouldCleanup { + os.Remove(swcPath) + } + } + if err != nil && os.IsNotExist(err) { + // temp .swcrc file to be used + // probably need this for parse_ts too + err = os.WriteFile(swcPath, []byte(`{ + "$schema": "http://json.schemastore.org/swcrc", + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2020", + "keepClassNames":true, + "transform": { + "decoratorVersion": "2022-03" + } + }, + "module": { + "type": "commonjs", + } +} + `), os.ModePerm) + + if err == nil { + shouldCleanup = true + } + } + return cleanup +} + func GetCommandInfo(dirPath string, fromTest bool) *CommandInfo { env := os.Environ() cmdName := "ts-node" diff --git a/internal/graphql/generate_ts_code.go b/internal/graphql/generate_ts_code.go index 8edd1b306..b9ab84bca 100644 --- a/internal/graphql/generate_ts_code.go +++ b/internal/graphql/generate_ts_code.go @@ -789,34 +789,8 @@ func ParseRawCustomData(processor *codegen.Processor, fromTest bool) ([]byte, er cmdInfo := cmd.GetCommandInfo(processor.Config.GetAbsPathToRoot(), fromTest) if cmdInfo.UseSwc { - swcPath := filepath.Join(processor.Config.GetAbsPathToRoot(), ".swcrc") - _, err := os.Stat(swcPath) - if err != nil && os.IsNotExist(err) { - // temp .swcrc file to be used - // probably need this for parse_ts too - err = os.WriteFile(swcPath, []byte(`{ - "$schema": "http://json.schemastore.org/swcrc", - "jsc": { - "parser": { - "syntax": "typescript", - "decorators": true - }, - "target": "es2020", - "keepClassNames":true, - "transform": { - "decoratorVersion": "2022-03" - } - }, - "module": { - "type": "commonjs", - } -} - `), os.ModePerm) - - if err == nil { - defer os.Remove(".swcrc") - } - } + cleanup := cmdInfo.MaybeSetupSwcrc(processor.Config.GetAbsPathToRoot()) + defer cleanup() } if fromTest { diff --git a/internal/schema/input/parse_ts.go b/internal/schema/input/parse_ts.go index b466521c7..e3791173f 100644 --- a/internal/schema/input/parse_ts.go +++ b/internal/schema/input/parse_ts.go @@ -50,9 +50,14 @@ func GetRawSchema(dirPath string, fromTest bool) ([]byte, error) { execCmd.Stderr = os.Stderr execCmd.Env = cmdInfo.Env + if cmdInfo.UseSwc { + cleanup := cmdInfo.MaybeSetupSwcrc(dirPath) + defer cleanup() + } + // flags not showing up in command but my guess is it's function of what's passed to process.argv if err := execCmd.Run(); err != nil { - return nil, err + return nil, errors.Wrap(err, "error getting raw schema") } return out.Bytes(), nil