package client import ( "context" "git.reya.zone/reya/hexmap/build" "git.reya.zone/reya/hexmap/proto" "github.com/magefile/mage/mg" "github.com/magefile/mage/target" "os" "os/exec" "path/filepath" ) func NPMInstall(ctx context.Context) error { var packageLockOutOfDate, nodeModulesOutOfDate bool var err error if packageLockOutOfDate, err = target.Path("client/package-lock.json", "client/package-lock.json"); err != nil { return err } if nodeModulesOutOfDate, err = target.Dir("client/node_modules", "client/package.json"); err != nil { return err } if !(packageLockOutOfDate || nodeModulesOutOfDate) { return nil } cmd := exec.CommandContext(ctx, "npm", "install") cmd.Dir, err = filepath.Abs("client") if err != nil { return err } cmd.Stderr = os.Stderr if mg.Verbose() { cmd.Stdout = os.Stdout } return nil } func ProtocFlags() ([]string, error) { buildPath, err := filepath.Abs(filepath.Join(build.ToolsDir, "protoc-gen-ts_proto")) if err != nil { return nil, err } return []string{ "--plugin=" + buildPath, "--ts_proto_out=client/src/proto/", "--ts_proto_opt=env=browser", "--ts_proto_opt=esModuleInterop=true", }, nil } type Protobuf mg.Namespace const TSPluginNPMLocation = "client/node_modules/.bin/protoc-gen-ts_proto" func (Protobuf) InstallTSPlugin(ctx context.Context) error { mg.CtxDeps(ctx, NPMInstall) buildPath, err := filepath.Abs(filepath.Join(build.ToolsDir, "protoc-gen-ts_proto")) if err != nil { return err } sourcePath, err := filepath.Abs(TSPluginNPMLocation) if err != nil { return err } // Errors here just mean we move on to the next step - this could not exist or not be a link, we don't care. // The important part is the later parts. if linkPath, err := os.Readlink(buildPath); err == nil && linkPath == sourcePath { return nil } // Remove whatever's in the way, if necessary. err = os.Remove(buildPath) if err != nil && !os.IsNotExist(err) { return err } err = os.Symlink(sourcePath, buildPath) if err != nil && !os.IsExist(err) { return err } return nil } func (Protobuf) MakeProtoDir() error { if err := os.Mkdir("client/src/proto", 0755); err != nil && !os.IsExist(err) { return err } return nil } func (Protobuf) InstallPlugins(ctx context.Context) error { mg.CtxDeps(ctx, Protobuf.InstallTSPlugin, Protobuf.MakeProtoDir) return nil } func (Protobuf) Build(ctx context.Context) error { mg.SerialCtxDeps(ctx, Protobuf.Clean, Protobuf.InstallPlugins) return proto.Compile(ctx, []proto.ProtocFlagsFunc{ProtocFlags}) } func (Protobuf) Clean() error { err := os.RemoveAll("client/src/proto") if err != nil && !os.IsNotExist(err) { return err } return nil } // TODO: Build // TODO: Clean // TODO: Serve