builder

package
v0.0.0-...-b7a4fd5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BuildActions = []action{
	{
		Name: "Create build template",
		Func: func() {
			if state.ServerMeta.Frontend.FrontendProvider.Provider == "" {
				panic("Frontend provider is empty")
			}

			cp, ok := state.Assets["frontend"]

			if !ok {
				panic("Core plugins not found")
			}

			subbed, err := fs.Sub(cp, "frontend")

			if err != nil {
				panic(err)
			}

			os.RemoveAll("sm-build")

			err = CopyProvider(subbed, state.ServerMeta.Frontend.FrontendProvider, "sm-build")

			if err != nil {
				panic(err)
			}

			os.RemoveAll("sm-build/build")

		},
	},
	{
		Name: "Copy plugins",
		Func: func() {
			cp, ok := state.Assets["frontend"]

			if !ok {
				panic("Core plugins not found")
			}

			os.MkdirAll("sm-build/src/routes/plugins", 0755)

			for _, plugin := range state.ServerMeta.Plugins {
				if plugin.ID == "" {
					panic("Plugin ID is empty")
				}

				if plugin.BuildScript != nil {
					fmt.Println("=> Running build script for", plugin.ID)
					err := plugin.BuildScript(&types.BuildScript{
						RootBuildDir: "sm-build",
						BuildDir:     "sm-build/src/routes/plugins/" + plugin.ID,
					})

					if err != nil {
						panic(err)
					}
				}

				if plugin.Frontend.Provider == "" {
					continue
				}

				fmt.Println("=> Copying plugin", plugin.ID)

				subbed, err := fs.Sub(cp, "frontend/coreplugins/"+plugin.ID)

				if err != nil {
					panic(err)
				}

				dstPath := "src/routes/plugins/" + plugin.ID
				err = CopyProvider(subbed, plugin.Frontend, "sm-build/"+dstPath)

				if err != nil {
					panic(err)
				}
			}
		},
	},
	{
		Name: "Copy components",
		Func: func() {
			cp, ok := state.Assets["frontend"]

			if !ok {
				panic("Components not found")
			}

			subbed, err := fs.Sub(cp, "frontend/src/lib/components")

			if err != nil {
				panic(err)
			}

			err = os.RemoveAll("sm-build/src/lib/components")

			if err != nil {
				panic(err)
			}

			err = os.MkdirAll("sm-build/src/lib/components", 0755)

			if err != nil {
				panic(err)
			}

			fmt.Println("=> Copying components to build")

			err = CopyProvider(subbed, state.ServerMeta.Frontend.ComponentProvider, "sm-build/src/lib/components")

			if err != nil {
				panic(err)
			}
		},
	},
	{
		Name: "Copy corelib",
		Func: func() {
			cp, ok := state.Assets["frontend"]

			if !ok {
				panic("Corelib not found")
			}

			subbed, err := fs.Sub(cp, "frontend/src/lib/corelib")

			if err != nil {
				panic(err)
			}

			err = os.RemoveAll("sm-build/src/lib/corelib")

			if err != nil {
				panic(err)
			}

			err = os.MkdirAll("sm-build/src/lib/corelib", 0755)

			if err != nil {
				panic(err)
			}

			fmt.Println("=> Copying corelib to build")

			err = CopyProvider(subbed, state.ServerMeta.Frontend.CorelibProvider, "sm-build/src/lib/corelib")

			if err != nil {
				panic(err)
			}
		},
	},
	{
		Name: "Copy extra files",
		Func: func() {
			for src, dst := range state.ServerMeta.Frontend.ExtraFiles {
				if src == "" || strings.HasPrefix(src, "@core") {
					panic("Invalid file path, file paths must be absolute: " + src)
				}

				var out = "sm-build/" + dst

				if strings.HasPrefix(dst, "$lib/") {
					out = "sm-build/src/lib/" + strings.Replace(dst, "$lib/", "", 1)
				}

				err := CopyDir(out, src)

				if err != nil {
					panic(err)
				}
			}
		},
	},
	{
		Name: "Sanity checks",
		Func: func() {

			f, err := os.Open("sm-build/src/routes/+layout.js")

			if err != nil {
				fmt.Println("Remove any layout.ts file you may have in sm-build/src/routes")
				fmt.Println("Then set it to the following")
				fmt.Println("")
				fmt.Println("export const prerender = true")
				fmt.Println("export const trailingSlash = 'always';")
				panic(err)
			}

			fStr, err := io.ReadAll(f)

			if err != nil {
				panic(err)
			}

			if !strings.Contains(string(fStr), "export const trailingSlash = 'always';") {
				fmt.Println("Trailing slash config is INVALID. Please add the below to sm-build/src/routes/layout.js")
				fmt.Println("")
				fmt.Println("export const trailingSlash = 'always';")
				os.Exit(1)
			}
		},
	},
	{
		Name: "Build frontend",
		Func: func() {
			cmd := exec.Command("npm", "install")
			cmd.Dir = "sm-build"
			cmd.Stdin = os.Stdin
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			cmd.Env = os.Environ()
			cmd.Env = append(cmd.Env, "FORCE_COLOR=true")
			cmd.Env = append(cmd.Env, "COLOR=always")

			err := cmd.Run()

			if err != nil {
				panic(err)
			}

			cmd = exec.Command("npm", "run", "build")
			cmd.Dir = "sm-build"
			cmd.Stdin = os.Stdin
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			cmd.Env = os.Environ()
			cmd.Env = append(cmd.Env, "FORCE_COLOR=true")
			cmd.Env = append(cmd.Env, "COLOR=always")

			err = cmd.Run()

			if err != nil {
				panic(err)
			}
		},
	},
	{
		Name: "Copy build to out",
		Func: func() {

			err := os.MkdirAll("frontend/build", 0755)

			if err != nil {
				panic(err)
			}

			err = os.RemoveAll("frontend/build")

			if err != nil {
				panic(err)
			}

			err = os.Rename("sm-build/build", "frontend/build")

			if err != nil {
				panic(err)
			}
		},
	},
}

Functions

func CopyDir

func CopyDir(dst, src string) error

CopyDir copies the content of src to dst. src should be a full path.

func CopyProvider

func CopyProvider(
	sp fs.FS,
	src types.Provider,
	dst string,
) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL