44from platform import system , machine
55from subprocess import run , CompletedProcess
66from pathlib import Path
7+ from json import loads
78
89import niquests
910
1011from pytailwindcss_extra .logger import log
1112
1213GITHUB_REPO = "dobicinaitis/tailwind-cli-extra"
14+ MAJOR_TAILWIND_CLI_EXTRA_VERSION = 2
1315
1416
1517def main () -> None :
@@ -20,9 +22,11 @@ def main() -> None:
2022 bin_dir_path = Path (temp_bin_dir_path )
2123
2224 # TODO: cache the latest version so it doesn't need to send a request each run
23- version = environ .get ("PYTAILWINDCSS_EXTRA_VERSION" , "latest" )
25+ version = environ .get ("PYTAILWINDCSS_EXTRA_VERSION" )
2426 if version == "latest" :
2527 version = get_latest_version_tag ()
28+ elif not version :
29+ version = get_latest_major_version_tag (MAJOR_TAILWIND_CLI_EXTRA_VERSION )
2630
2731 bin_path = bin_dir_path / f"tailwindcss-extra-{ version .replace ('.' , '-' )} "
2832 if not bin_path .exists ():
@@ -95,6 +99,17 @@ def get_latest_version_tag() -> str:
9599 return response .json ()["tag_name" ]
96100
97101
102+ def get_latest_major_version_tag (major_version : int ) -> str :
103+ log .info (f"Getting latest tailwind-cli-extra version for v{ major_version } ..." )
104+ response = niquests .get (f"https://api.github.com/repos/{ GITHUB_REPO } /releases" )
105+ response .raise_for_status ()
106+ if not response .text :
107+ raise RuntimeError ("No releases found" )
108+ releases = loads (response .text )
109+ matching_releases = [release for release in releases if release ["tag_name" ].startswith (f"v{ major_version } ." )]
110+ return matching_releases [0 ]["tag_name" ]
111+
112+
98113def run_file_with_arguments (file_path : Path , arguments : list [str ]) -> CompletedProcess [bytes ]:
99114 return run ([file_path ] + arguments , check = False )
100115
0 commit comments