Skip to content

Commit

Permalink
port .swcrc logic into central location and reuse (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Nov 30, 2024
1 parent b3e6fad commit aa049d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
39 changes: 39 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 2 additions & 28 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion internal/schema/input/parse_ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa049d5

Please sign in to comment.