Skip to content

Commit

Permalink
fix #757: use XDG_CACHE_HOME in install script
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 6, 2021
1 parent c04240a commit 560a6a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

* Support the `XDG_CACHE_HOME` environment variable ([#757](https://github.com/evanw/esbuild/issues/757))

On Linux, the install script for esbuild currently caches downloaded binary executables in `~/.cache/esbuild/bin`. This change means esbuild will now try installing to `$XDG_CACHE_HOME/esbuild/bin` instead of the `XDG_CACHE_HOME` environment variable exists. This allows you to customize the cache directory on Linux. The specification that defines `XDG_CACHE_HOME` is [here](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).

## 0.8.42

* Fix crash with block-level function declaration and `--keep-names` ([#755](https://github.com/evanw/esbuild/issues/755))
Expand Down
11 changes: 10 additions & 1 deletion lib/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ async function installBinaryFromPackage(name: string, fromPath: string, toPath:

// Also try to cache the file to speed up future installs
try {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
fs.mkdirSync(path.dirname(cachePath), {
recursive: true,
mode: 0o700, // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
});
fs.copyFileSync(toPath, cachePath);
cleanCacheLRU(cachePath);
} catch {
Expand Down Expand Up @@ -107,6 +110,12 @@ function getCachePath(name: string): string {
const common = ['esbuild', 'bin', `${name}@${version}`];
if (process.platform === 'darwin') return path.join(home, 'Library', 'Caches', ...common);
if (process.platform === 'win32') return path.join(home, 'AppData', 'Local', 'Cache', ...common);

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
if (process.platform === 'linux' && XDG_CACHE_HOME && path.isAbsolute(XDG_CACHE_HOME))
return path.join(XDG_CACHE_HOME, ...common);

return path.join(home, '.cache', ...common);
}

Expand Down

0 comments on commit 560a6a7

Please sign in to comment.