Skip to content

Commit

Permalink
feat: update tsconfig command
Browse files Browse the repository at this point in the history
`ags types --tsconfig` updates/writes tsconfig.json and env.d.ts
  • Loading branch information
Aylur committed Nov 11, 2024
1 parent 821c38c commit c64f8d3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 37 deletions.
14 changes: 8 additions & 6 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import (
"github.com/spf13/cobra"
)

var (
tsconfig string
tsconfigRaw string
)
var tsconfig string

var bundleCommand = &cobra.Command{
Use: "bundle [entryfile] [outfile]",
Expand All @@ -34,9 +31,14 @@ var bundleCommand = &cobra.Command{
}

if info.IsDir() {
lib.Bundle(getAppEntry(path), outfile)
lib.Bundle(getAppEntry(path), outfile, tsconfig)
} else {
lib.Bundle(path, outfile)
lib.Bundle(path, outfile, tsconfig)
}
},
}

func init() {
f := bundleCommand.Flags()
f.StringVar(&tsconfig, "tsconfig", "", "path to tsconfig.json")
}
22 changes: 8 additions & 14 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
f := initCommand.Flags()

f.IntVarP(&gtk, "gtk", "g", 3, "gtk version to use")
f.BoolVarP(&force, "force", "f", false, "override existing files")
f.StringVarP(&initDirectory, "directory", "d", defaultConfigDir(), "target directory")
}

Expand All @@ -39,9 +40,8 @@ func getDataFile(name string) string {
return string(content)
}

// TODO: write basic config when dir is empty
// option to override tsconfig.json and env.d.ts
func initConfig(cmd *cobra.Command, args []string) {
// TODO: gtk4 template
if gtk != 3 {
lib.Err("currently only gtk3 is supported")
}
Expand All @@ -57,26 +57,20 @@ func initConfig(cmd *cobra.Command, args []string) {
configDir = defaultConfigDir()
}

tsconfig := getDataFile("tsconfig.json")
envdts := getDataFile("env.d.ts")
stylescss := getDataFile("style.scss")
bartsx := getDataFile("gtk3/Bar.tsx")
appts := getDataFile("gtk3/app.ts")

if info, err := os.Stat(configDir); err == nil && info.IsDir() && !force {
lib.Err("could not initialize: " + lib.Cyan(configDir) + " already exists")
}

tsconf := strings.ReplaceAll(tsconfig, "@ASTAL_GJS@", astalGjs)
tsconf = strings.ReplaceAll(tsconf, "@GTK_VERSION@", "gtk3") // TODO: gtk4 flag
tsconf := strings.ReplaceAll(getDataFile("tsconfig.json"), "@ASTAL_GJS@", astalGjs)
tsconf = strings.ReplaceAll(tsconf, "@GTK_VERSION@", "gtk3")
lib.Mkdir(configDir + "/widget")

lib.WriteFile(configDir+"/.gitignore", "@girs/\nnode_modules/")
lib.WriteFile(configDir+"/tsconfig.json", tsconf)
lib.WriteFile(configDir+"/env.d.ts", envdts)
lib.WriteFile(configDir+"/style.scss", stylescss)
lib.WriteFile(configDir+"/widget/Bar.tsx", bartsx)
lib.WriteFile(configDir+"/app.ts", appts)
lib.WriteFile(configDir+"/env.d.ts", getDataFile("env.d.ts"))
lib.WriteFile(configDir+"/style.scss", getDataFile("style.scss"))
lib.WriteFile(configDir+"/widget/Bar.tsx", getDataFile("gtk3/Bar.tsx"))
lib.WriteFile(configDir+"/app.ts", getDataFile("gtk3/app.ts"))

genTypes(configDir, "*")
fmt.Println(lib.Green("project ready") + " at " + lib.Cyan(configDir))
Expand Down
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func init() {
`directory to search for an "app" entry file
when no positional argument is given
`+"\b")

f.StringVar(&tsconfig, "tsconfig", "", "path to tsconfig.json")
f.MarkHidden("tsconfig")
}

func getOutfile() string {
Expand Down Expand Up @@ -85,7 +88,7 @@ func getAppEntry(dir string) string {

func run(infile string) {
outfile := getOutfile()
lib.Bundle(infile, outfile)
lib.Bundle(infile, outfile, tsconfig)

if gtk4 {
os.Setenv("LD_PRELOAD", gtk4LayerShell)
Expand Down
18 changes: 14 additions & 4 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
typesTonfigDir string
ignoreModules []string
updateTsconfig bool
)

var typesCommand = &cobra.Command{
Expand All @@ -23,18 +23,28 @@ var typesCommand = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Example: ` ags types Astal* --ignore Gtk3 --ignore Astal3`,
Run: func(cmd *cobra.Command, args []string) {
if updateTsconfig {
lib.WriteFile(targetDir+"/tsconfig.json", lib.GetTsconfig(targetDir))

envdts := targetDir + "/env.d.ts"
if !lib.FileExists(envdts) {
lib.WriteFile(envdts, getDataFile("env.d.ts"))
}
}

if len(args) > 0 {
genTypes(typesTonfigDir, args[0])
genTypes(targetDir, args[0])
} else {
genTypes(typesTonfigDir, "*")
genTypes(targetDir, "*")
}
},
}

func init() {
f := typesCommand.Flags()

f.StringVarP(&typesTonfigDir, "directory", "d", defaultConfigDir(), "target directory")
f.BoolVar(&updateTsconfig, "tsconfig", false, "update tsconfig.json")
f.StringVarP(&targetDir, "directory", "d", defaultConfigDir(), "target directory")
f.StringArrayVarP(&ignoreModules, "ignore", "i", []string{}, "modules that should be ignored")
}

Expand Down
21 changes: 19 additions & 2 deletions lib/esbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,25 @@ var blpPlugin api.Plugin = api.Plugin{
// svg loader
// other css preproceccors
// http plugin with caching
func Bundle(infile, outfile string) {
func Bundle(infile, outfile, tsconfig string) {
srcdir := filepath.Dir(infile)

if tsconfig != "" {
path, err := filepath.Abs(tsconfig)
if err != nil {
Err(err)
}

data, err := os.ReadFile(path)
if err != nil {
Err(err)
}

tsconfig = string(data)
} else {
tsconfig = GetTsconfig(srcdir)
}

result := api.Build(api.BuildOptions{
Color: api.ColorAlways,
LogLevel: api.LogLevelWarning,
Expand All @@ -132,7 +149,7 @@ func Bundle(infile, outfile string) {
Outfile: outfile,
Format: api.FormatESModule,
Platform: api.PlatformNeutral,
TsconfigRaw: getTsconfig(srcdir),
TsconfigRaw: tsconfig,
Write: true,
AbsWorkingDir: srcdir,
Define: map[string]string{
Expand Down
15 changes: 9 additions & 6 deletions lib/tsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ func Initialize(_astalGjs string) {
}

type tsCompilerOptions struct {
Paths map[string][]string `json:"paths"`
JsxImportSource string `json:"jsxImportSource"`
Jsx string `json:"jsx"`
Module string `json:"module"`
Target string `json:"target"`
ExperimentalDecorators bool `json:"experimentalDecorators"`
Target string `json:"target"`
Module string `json:"module"`
ModuleResolution string `json:"moduleResolution"`
Jsx string `json:"jsx"`
JsxImportSource string `json:"jsxImportSource"`
Paths map[string][]string `json:"paths"`
}

type tsconfig struct {
Expand All @@ -36,6 +37,7 @@ func defaultTsconfig() tsconfig {
ExperimentalDecorators: true,
Module: "ES2022",
Target: "ES2022",
ModuleResolution: "Bundler",
Jsx: "react-jsx",
JsxImportSource: astalGjs + "/" + defaultGtkVersion,
Paths: map[string][]string{
Expand All @@ -59,6 +61,7 @@ func (tsconfig *tsconfig) updateTsconfig() {
tsconfig.CompilerOptions.ExperimentalDecorators = true
tsconfig.CompilerOptions.Module = "ES2022"
tsconfig.CompilerOptions.Target = "ES2022"
tsconfig.CompilerOptions.ModuleResolution = "Bundler"
tsconfig.CompilerOptions.Jsx = "react-jsx"
tsconfig.CompilerOptions.JsxImportSource = astalGjs + gtk
tsconfig.CompilerOptions.Paths["astal"] = []string{astalGjs}
Expand All @@ -67,7 +70,7 @@ func (tsconfig *tsconfig) updateTsconfig() {

// if tsconfig.json exists in srcdir returns an updated config
// otherwise returns a default config
func getTsconfig(srcdir string) string {
func GetTsconfig(srcdir string) string {
path := srcdir + "/tsconfig.json"

var tsconfig tsconfig
Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ var (
//go:embed data
data embed.FS

// should be overriden with -ldflags
gtk4LayerShell = "/usr/lib/libgtk4-layer-shell.so"

astalGjs = "/usr/share/astal/gjs"

tsForGir = "@ts-for-gir/[email protected]"
astalGjs = "/usr/share/astal/gjs"
tsForGir = "@ts-for-gir/[email protected]"
)

func main() {
Expand Down

0 comments on commit c64f8d3

Please sign in to comment.