3 const fs = require("fs");
4 const hash = require("./hash");
5 const path = require("path");
7 * Return the cacheFile to be used by stylelint, based on whether the provided parameter is
8 * a directory or looks like a directory (ends in `path.sep`), in which case the file
9 * name will be `cacheFile/.cache_hashOfCWD`.
11 * If cacheFile points to a file or looks like a file, then it will just use that file.
13 * @param {string} cacheFile - The name of file to be used to store the cache
14 * @param {string} cwd - Current working directory. Used for tests
15 * @returns {string} Resolved path to the cache file
17 module.exports = function getCacheFile(cacheFile, cwd) {
19 * Make sure path separators are normalized for environment/os.
20 * Also, keep trailing path separator if present.
22 cacheFile = path.normalize(cacheFile);
24 const resolvedCacheFile = path.resolve(cwd, cacheFile);
25 // If the last character passed is a path separator, we assume is a directory.
26 const looksLikeADirectory = cacheFile[cacheFile.length - 1] === path.sep;
29 * Return the default cache file name when provided parameter is a directory.
30 * @returns {string} - Resolved path to the cacheFile
32 function getCacheFileForDirectory() {
33 return path.join(resolvedCacheFile, `.stylelintcache_${hash(cwd)}`);
39 fileStats = fs.lstatSync(resolvedCacheFile);
44 if (looksLikeADirectory || (fileStats && fileStats.isDirectory())) {
45 // Return path to provided directory with generated file name.
46 return getCacheFileForDirectory();
49 // Return normalized path to cache file.
50 return resolvedCacheFile;