Use user cache directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/CHANGELOG.md b/CHANGELOG.md
index db51c23..104e424 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
## v0.3.1
+ * Use user cache directory (h/t @josevalim)
* Add support for 32bit linux (h/t @derek-zhou)
* Support `HTTP_PROXY/HTTPS_PROXY` to fetch esbuild (h/t @iaddict)
* Update Sass version to `1.43.4`
diff --git a/lib/dart_sass.ex b/lib/dart_sass.ex
index 12dfd4f..d388356 100644
--- a/lib/dart_sass.ex
+++ b/lib/dart_sass.ex
@@ -237,9 +237,12 @@ defmodule DartSass do
"""
def install do
version = configured_version()
- tmp_dir = Path.join(System.tmp_dir!(), "cs-dart-sass")
- File.rm_rf!(tmp_dir)
- File.mkdir_p!(tmp_dir)
+ tmp_opts = if System.get_env("MIX_XDG"), do: %{os: :linux}, else: %{}
+
+ tmp_dir =
+ freshdir_p(:filename.basedir(:user_cache, "cs-sass", tmp_opts)) ||
+ freshdir_p(Path.join(System.tmp_dir!(), "cs-sass")) ||
+ raise "could not install sass. Set MIX_XDG=1 and then set XDG_CACHE_HOME to the path you want to use as cache"
platform = detect_platform()
name = "dart-sass-#{version}-#{target(platform)}"
@@ -270,6 +273,15 @@ defmodule DartSass do
end
end
+ defp freshdir_p(path) do
+ with {:ok, _} <- File.rm_rf(path),
+ :ok <- File.mkdir_p(path) do
+ path
+ else
+ _ -> nil
+ end
+ end
+
defp unpack_archive(".zip", zip, cwd) do
with {:ok, _} <- :zip.unzip(zip, cwd: to_charlist(cwd)), do: :ok
end