Sha256: 47fefb7463123057a2d5db555ee3c8f3b359ed41bc33b3e50f4f255a9acc2996

Contents?: true

Size: 1.64 KB

Versions: 23

Compression:

Stored size: 1.64 KB

Contents

'use strict';

const fs = require('fs');
const hash = require('./hash');
const path = require('path');

/**
 * Return the cacheFile to be used by stylelint, based on whether the provided parameter is
 * a directory or looks like a directory (ends in `path.sep`), in which case the file
 * name will be `cacheFile/.cache_hashOfCWD`.
 *
 * If cacheFile points to a file or looks like a file, then it will just use that file.
 *
 * @param {string} cacheFile - The name of file to be used to store the cache
 * @param {string} cwd - Current working directory. Used for tests
 * @returns {string} Resolved path to the cache file
 */
module.exports = function getCacheFile(cacheFile, cwd) {
	/*
	 * Make sure path separators are normalized for environment/os.
	 * Also, keep trailing path separator if present.
	 */
	cacheFile = path.normalize(cacheFile);

	const resolvedCacheFile = path.resolve(cwd, cacheFile);
	// If the last character passed is a path separator, we assume is a directory.
	const looksLikeADirectory = cacheFile[cacheFile.length - 1] === path.sep;

	/**
	 * Return the default cache file name when provided parameter is a directory.
	 * @returns {string} - Resolved path to the cacheFile
	 */
	function getCacheFileForDirectory() {
		return path.join(resolvedCacheFile, `.stylelintcache_${hash(cwd)}`);
	}

	let fileStats;

	try {
		fileStats = fs.lstatSync(resolvedCacheFile);
	} catch (ex) {
		fileStats = null;
	}

	if (looksLikeADirectory || (fileStats && fileStats.isDirectory())) {
		// Return path to provided directory with generated file name.
		return getCacheFileForDirectory();
	}

	// Return normalized path to cache file.
	return resolvedCacheFile;
};

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.6 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.5 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.4 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.3 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.2 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.1 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3.5 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-5.0.0 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3.4 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3.3 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3.2 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3.1 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.3 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.2.3 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.2.2 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.2.1 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.2 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.1.9 node_modules/stylelint/lib/utils/getCacheFile.js
trusty-cms-4.1.8 node_modules/stylelint/lib/utils/getCacheFile.js