Skip to content

Conversation

@Richienb
Copy link
Contributor

@Richienb Richienb commented Oct 16, 2019

When the environmental variable CACHE_DIR is specified, the specified directory will be used instead. This is useful in development situations where packages write useful information to the cache.

Closes #16

@Richienb
Copy link
Contributor Author

@sindresorhus

@sindresorhus
Copy link
Owner

Did you see my comment: #10 (comment) ?

@sindresorhus
Copy link
Owner

This is useful in development situations where packages write useful information to the cache.

Can you be more specific about the use-case?

@Richienb
Copy link
Contributor Author

@langri-sha Can you reference the packages you were talking about in #16 (comment)?

@langri-sha
Copy link

langri-sha commented Oct 18, 2019

In my project I have 8 dependencies that use find-cache-dir. Of them two are completely unconfigurable: ava and @storybook/core.

To save ~30s per build (~23$/mo), I have a local find-cache-dir package and use Yarn's resolutions.

Feel free to use this if the PR doesn't get merged.

// package.json

{
  "resolutions": {
    "find-cache-dir": "file:your-workspace/find-cache-dir"
  }
}

// your-workspace/find-cache-dir/src/index.js

const findUp = require('find-up')
const path = require('path')
const { ensureDirSync } = require('fs-extra')

const root = findUp.sync('yarn.lock')

module.exports = (options = {}) => {
  const { name, create, thunk } = options
  
  const ensureDir = create ? ensureDirSync : path => path

  if (thunk) {
    return (...args) => ensureDir(path.join(root, '.cache', name, ...args))
  }

  return ensureDir(path.join('root', '.cache', name))
}

@Richienb
Copy link
Contributor Author

Richienb commented Oct 18, 2019

@sindresorhus ^^

@langri-sha
Copy link

I wish to correct myself on the point of ava as I was looking closely in the sources. It expresses the same behavior of find-cache-dir, but doesn't actually use it.

@sindresorhus
Copy link
Owner

CACHE_DIR is a very generic environment variable, which is fine, but I think we should not put the cache directly in CACHE_DIR, but rather inside $CACHE_DIR/find-cache-dir, to prevent possible conflicts with other projects using that directory.

@sindresorhus sindresorhus changed the title feat: Allow the CACHE_DIR environmental variable to override other dirs for development situations Allow the CACHE_DIR environmental variable to override the cache location Feb 13, 2020
@Richienb
Copy link
Contributor Author

@sindresorhus Ready for re-review!

@sindresorhus
Copy link
Owner

The code looks technically correct, but I feel like it could be refactored a bit to improve readability.

https://github.com/Richienb/find-cache-dir/blob/ea4a195f23faee1f150e7342dd136aefdb603c54/index.js#L20 is kinda awkward, if true, the directory is the actual cache directory, if false, it's the CWD directory.

This could be made a separate method: https://github.com/Richienb/find-cache-dir/blob/ea4a195f23faee1f150e7342dd136aefdb603c54/index.js#L32-L40 which could also include this part: https://github.com/avajs/find-cache-dir/blob/8ec64dcb35b3975e13a08dba2a3b6e7fb599ae30/index.js#L19-L25

That way the main method logic only handles the resolved cache directories.

Richienb and others added 3 commits February 18, 2020 11:16
Signed-off-by: Richie Bendall <[email protected]>
@sindresorhus
Copy link
Owner

I think it could still be made more readable. And this is not resolved:

https://github.com/Richienb/find-cache-dir/blob/ea4a195f23faee1f150e7342dd136aefdb603c54/index.js#L20 is kinda awkward, if true, the directory is the actual cache directory, if false, it's the CWD directory.

Signed-off-by: Richie Bendall <[email protected]>
@Richienb
Copy link
Contributor Author

@sindresorhus I found a refactoring strategy to make the code more readable.

@sindresorhus sindresorhus changed the title Allow the CACHE_DIR environmental variable to override the cache location Allow the CACHE_DIR environment variable to override the cache location Feb 22, 2020
@sindresorhus sindresorhus merged commit 71faea2 into sindresorhus:master Feb 22, 2020
@sindresorhus
Copy link
Owner

Looks good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Controlling cache dir with env variable

3 participants