Skip to content

Commit

Permalink
swc again for parse typescript and custom graphql
Browse files Browse the repository at this point in the history
uses https://github.com/swc-project/swc-node

fixes #792

cannot use ts-node anymore because it doesn't provide an option for .swcrc
TypeStrong/ts-node#1856

TODO: depends on swc-project/swc#7358 to actually be used

TODO: remove swc-node in examples/simple

TODO: move .swcrc write in generate_ts_code.go into central location
  • Loading branch information
lolopinto committed May 5, 2023
1 parent a9260b7 commit 3eb9141
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 79 deletions.
154 changes: 144 additions & 10 deletions examples/simple/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@snowtop/ent-passport": "^0.1.0-alpha3",
"@snowtop/ent-password": "^0.1.0-alpha1",
"@snowtop/ent-phonenumber": "^0.1.0-alpha1",
"@swc-node/register": "^1.6.5",
"@swc/core": "^1.3.56",
"@types/express-session": "^1.17.3",
"@types/graphql-upload": "^8.0.11",
Expand Down
77 changes: 74 additions & 3 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"os"
"path/filepath"

"github.com/lolopinto/ent/internal/testingutils"
"github.com/lolopinto/ent/internal/util"
)

Expand All @@ -13,14 +15,83 @@ func GetTsconfigPaths() string {
// initial args for ts-node-script
// we need tsconfig.json referenced because of relative paths like src/ent/generated/types.ts
func GetArgsForScript(rootPath string) []string {
return append(
getArgsForScriptBoth(rootPath),
"-r",
"@swc-node/register",
)
}

func GetArgsForScriptSwc(rootPath string) []string {
return append(
getArgsForScriptBoth(rootPath),
"-r",
GetTsconfigPaths(),
)
}

func getArgsForScriptBoth(rootPath string) []string {
return []string{
// this seems to let the errors pass through s opposed to giving compile error
// this seems to let the errors pass through as opposed to giving compile error
"--log-error", // TODO spend more time figuring this out
"--project",
// TODO this should find the tsconfig.json and not assume there's one at the root but fine for now
// same in generate_ts_code.go
filepath.Join(rootPath, "tsconfig.json"),
"-r",
GetTsconfigPaths(),
// "-r",
// GetTsconfigPaths(),
}
}

func UseSwc() bool {
return !util.EnvIsTrue("DISABLE_SWC")
}

// func GetCommand()

type CommandInfo struct {
Name string
Args []string
Env []string
UseSwc bool
}

func GetCommandInfo(dirPath string, fromTest bool) *CommandInfo {
var env []string
cmdName := "ts-node"
var cmdArgs []string
useSwc := false

if UseSwc() {
cmdArgs = append(
getArgsForScriptBoth(dirPath),
"-r", "@swc-node/register",
)

env = append(os.Environ(), "SWCRC=true")

useSwc = true
} else if fromTest {
// TODO if in swc do we need this?
cmdArgs = []string{
"--compiler-options",
testingutils.DefaultCompilerOptions(),
}
} else {
cmdName = "ts-node-script"

cmdArgs = GetArgsForScript(dirPath)
}

// append LOCAL_SCRIPT_PATH so we know. in typescript...
if util.EnvIsTrue("LOCAL_SCRIPT_PATH") {
env = append(env, "LOCAL_SCRIPT_PATH=true")
}

return &CommandInfo{
Name: cmdName,
Args: cmdArgs,
Env: env,
UseSwc: useSwc,
}
}
Loading

0 comments on commit 3eb9141

Please sign in to comment.