From 1d0c7bf3fda99c044e5d480c508163b98de52ba6 Mon Sep 17 00:00:00 2001 From: hackerchai Date: Sun, 29 Oct 2023 21:40:49 +0800 Subject: [PATCH] fix(zbpack): implement static provider generate static files Signed-off-by: hackerchai --- .gitignore | 1 + internal/static/TransformServerless.go | 15 +++++++++++---- pkg/zeaburpack/main.go | 18 +++++++++++++++++- tests/static-raw-html/index.html | 9 +++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 tests/static-raw-html/index.html diff --git a/.gitignore b/.gitignore index e47985d7..f5b31634 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea bin .DS_Store +tests/*/.zeabur diff --git a/internal/static/TransformServerless.go b/internal/static/TransformServerless.go index 6da6ee1d..09e9e53b 100644 --- a/internal/static/TransformServerless.go +++ b/internal/static/TransformServerless.go @@ -10,10 +10,17 @@ import ( ) // TransformServerless copies the static files from output to .zeabur/output/static and creates a config.json file for SPA -func TransformServerless(image, workdir string, meta types.PlanMeta) error { - err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static")) - if err != nil { - return err +func TransformServerless(image, workdir string, meta types.PlanMeta, planType types.PlanType) error { + if planType == types.PlanTypeStatic { + err := utils.CopyFromImage(image, "/usr/share/nginx/html/static"+"/.", path.Join(workdir, ".zeabur/output/static")) + if err != nil { + return err + } + } else { + err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static")) + if err != nil { + return err + } } config := types.ZeaburOutputConfig{Containerized: false, Routes: make([]types.ZeaburOutputConfigRoute, 0)} diff --git a/pkg/zeaburpack/main.go b/pkg/zeaburpack/main.go index 8235f554..eff73b6b 100644 --- a/pkg/zeaburpack/main.go +++ b/pkg/zeaburpack/main.go @@ -191,6 +191,9 @@ func Build(opt *BuildOptions) error { } _ = os.RemoveAll(".zeabur") + if wd != *opt.Path { + _ = os.RemoveAll(*opt.Path + "/.zeabur") + } if t == types.PlanTypeNodejs && m["framework"] == string(types.NodeProjectFrameworkNextJs) && m["serverless"] == "true" { println("Transforming build output to serverless format ...") @@ -204,7 +207,17 @@ func Build(opt *BuildOptions) error { if t == types.PlanTypeNodejs && m["outputDir"] != "" { println("Transforming build output to serverless format ...") - err = static.TransformServerless(*opt.ResultImage, *opt.Path, m) + err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, t) + if err != nil { + println("Failed to transform serverless: " + err.Error()) + handleBuildFailed(err) + return err + } + } + + if t == types.PlanTypeStatic { + println("Transforming build output to serverless format ...") + err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, t) if err != nil { println("Failed to transform serverless: " + err.Error()) handleBuildFailed(err) @@ -217,6 +230,9 @@ func Build(opt *BuildOptions) error { handleLog("\033[90m" + "To run the image, use the following command:" + "\033[0m") if t == types.PlanTypeNodejs && m["outputDir"] != "" { handleLog("npx serve .zeabur/output/static") + } else if t == types.PlanTypeStatic { + handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage) + handleLog("or you can find the static files in .zeabur/output/static") } else { handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage) } diff --git a/tests/static-raw-html/index.html b/tests/static-raw-html/index.html new file mode 100644 index 00000000..c755ccf9 --- /dev/null +++ b/tests/static-raw-html/index.html @@ -0,0 +1,9 @@ + + + + Hello world! + + +

Hello world!

+ +