.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / getCacheFile.js
1 "use strict";
2
3 const fs = require("fs");
4 const hash = require("./hash");
5 const path = require("path");
6 /**
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`.
10  *
11  * If cacheFile points to a file or looks like a file, then it will just use that file.
12  *
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
16  */
17 module.exports = function getCacheFile(cacheFile, cwd) {
18   /*
19    * Make sure path separators are normalized for environment/os.
20    * Also, keep trailing path separator if present.
21    */
22   cacheFile = path.normalize(cacheFile);
23
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;
27
28   /**
29    * Return the default cache file name when provided parameter is a directory.
30    * @returns {string} - Resolved path to the cacheFile
31    */
32   function getCacheFileForDirectory() {
33     return path.join(resolvedCacheFile, `.stylelintcache_${hash(cwd)}`);
34   }
35
36   let fileStats;
37
38   try {
39     fileStats = fs.lstatSync(resolvedCacheFile);
40   } catch (ex) {
41     fileStats = null;
42   }
43
44   if (looksLikeADirectory || (fileStats && fileStats.isDirectory())) {
45     // Return path to provided directory with generated file name.
46     return getCacheFileForDirectory();
47   }
48
49   // Return normalized path to cache file.
50   return resolvedCacheFile;
51 };