index.js 693 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
'use strict';
const path = require('path');
const commonDir = require('commondir');
const pkgDir = require('pkg-dir');
const makeDir = require('make-dir');

module.exports = (options = {}) => {
	const {name} = options;
	let directory = options.cwd;

	if (options.files) {
		directory = commonDir(directory, options.files);
	} else {
		directory = directory || process.cwd();
	}

	directory = pkgDir.sync(directory);

	if (directory) {
		directory = path.join(directory, 'node_modules', '.cache', name);

		if (directory && options.create) {
			makeDir.sync(directory);
		}

		if (options.thunk) {
			return (...arguments_) => path.join(directory, ...arguments_);
		}
	}

	return directory;
};