diff --git a/README.md b/README.md index 4ed0c49..1f0dc23 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # Tailo -Tailo is a Go wrapper for the common operations with the TailwindCSS binary. It is intended to automate the process of installing the TailwindCSS binary, running it, and cleaning up the generated files. \ No newline at end of file +Tailo is a Go wrapper for the common operations with the TailwindCSS binary. It is intended to automate the process of installing the TailwindCSS binary, running it, and cleaning up the generated files. + +## Setup Command +The setup command is an easy way to invoke tailo from your application without creating a dependency on this package. + +```sh +go run github.com/paganotoni/tailo/cmd/setup@latest // Using Latest Tailwind + +// Or Using a specific version +go run github.com/paganotoni/tailo/cmd/setup@latest -version=v3.4.6 +``` + +## Build Command +Like the Setup command the Build Command allows to specify the version of Tailwind to be used. + +```sh +go run ./cmd/build -version=v3.4.6 +``` diff --git a/build.go b/build.go index 21af38e..ff39b83 100644 --- a/build.go +++ b/build.go @@ -10,19 +10,18 @@ import ( // CSS file and generate compiled CSS it expects to find // the options in the config file. func Build(options ...Option) { - err := Setup() - if err != nil { + // Applying passed options + for _, option := range options { + option() + } + + if err := Setup(); err != nil { fmt.Println("Error running the setup:", err.Error()) os.Exit(1) return } - // Applying passed options - for _, option := range options { - option() - } - if _, err := os.Stat(binaryPath); os.IsNotExist(err) { err := Setup() if err != nil { @@ -40,8 +39,7 @@ func Build(options ...Option) { fmt.Println("[tailo] Running:", cmd.String()) - err = cmd.Run() - if err != nil { - panic(err) + if err := cmd.Run(); err != nil { + fmt.Println("[tailo] Error running tailwindcss:", err) } } diff --git a/cmd/build/main.go b/cmd/build/main.go index 703a1ee..8776075 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -7,10 +7,11 @@ import ( ) var ( - input string - output string - config string - binary string + input string + output string + config string + binary string + version string ) func init() { @@ -18,13 +19,14 @@ func init() { flag.StringVar(&output, "output", "public/application.css", "The CSS output file path.") flag.StringVar(&config, "config", "tailwind.config.js", "The TailwindCSS configuration file path.") flag.StringVar(&binary, "binary", "bin/tailwindcss", "The TailwindCSS CLI binary path.") + flag.StringVar(&version, "version", "", "The TailwindCSS version to use, defaults to empty which means latest.") } func main() { flag.Parse() - tailo.Setup() tailo.Build( + tailo.UseVersion(version), tailo.UseInputPath(input), tailo.UseOutputPath(output), tailo.UseConfigPath(config), diff --git a/cmd/setup/main.go b/cmd/setup/main.go index d5decee..049bac3 100644 --- a/cmd/setup/main.go +++ b/cmd/setup/main.go @@ -1,8 +1,27 @@ package main -import "github.com/paganotoni/tailo" +import ( + "flag" + + "github.com/paganotoni/tailo" +) + +var ( + input string + output string + config string + binary string + version string +) + +func init() { + flag.StringVar(&version, "version", "", "The TailwindCSS version to use, defaults to empty which means latest.") +} func main() { - //TODO: allow flags? - tailo.Setup() + flag.Parse() + + tailo.Setup( + tailo.UseVersion(version), + ) } diff --git a/options.go b/options.go index 326a35c..5e20703 100644 --- a/options.go +++ b/options.go @@ -47,3 +47,9 @@ func UseBinaryPath(path string) Option { binaryPath = path } } + +func UseVersion(v string) Option { + return func() { + version = v + } +} diff --git a/setup.go b/setup.go index 0835135..68cd186 100644 --- a/setup.go +++ b/setup.go @@ -12,21 +12,30 @@ import ( ) var ( + version string binaries = map[string]string{ - "darwin_amd64": "tailwindcss-macos-x64", - "darwin_arm64": "tailwindcss-macos-arm64", - "linux_amd64": "tailwindcss-linux-x64", - "linux_arm64": "tailwindcss-linux-arm64", - "linux_arm": "tailwindcss-linux-armv7", - "windows_amd64": "tailwindcss-windows-x64", - "windows_arm64": "tailwindcss-windows-arm64", + "darwin_amd64": "macos-x64", + "darwin_arm64": "macos-arm64", + "linux_amd64": "linux-x64", + "linux_arm64": "linux-arm64", + "linux_arm": "linux-armv7", + "windows_amd64": "windows-x64", + "windows_arm64": "windows-arm64", } + + latestURL = "https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-%s" + versionedURL = "https://github.com/tailwindlabs/tailwindcss/releases/download/%s/tailwindcss-%s" ) // Setup downloads the Tailwind CSS CLI binary for the // given operating system and architecture. It makes the // binary executable and places it in the bin/ directory. -func Setup() error { +func Setup(options ...Option) error { + // Running options + for _, option := range options { + option() + } + if _, err := os.Stat("bin/tailwindcss"); err == nil { fmt.Println("Tailwind CSS CLI binary already exists.") @@ -39,7 +48,15 @@ func Setup() error { return fmt.Errorf("Unsupported operating system and architecture: %s", currentArch) } - url := fmt.Sprintf("https://github.com/tailwindlabs/tailwindcss/releases/latest/download/%v", binary) + url := latestURL + args := []any{binary} + + if version != "" { + url = versionedURL + args = []any{version, binary} + } + + url = fmt.Sprintf(url, args...) fmt.Println("Downloading from:", url) resp, err := http.Get(url)