nx/packages/workspace/src/utilities/cache-directory.ts
Shihab Uddin eaa0287238
fix(core): always use environment variable for cache directory (#6746)
In 6c16ee0feead3f01c575af2b998fe614c207ac96 the environment variable was used
while reading from `nx.json`. But in `packages/workspace/src/tasks-runner/cache.ts`
the `cacheDirectory` is used without using `readCacheDirectoryProperty`. Now we
check the environment variable in `cacheDirectory`, so that the environment
variable is always used.

ISSUES CLOSED: #6629
2021-08-18 17:56:13 -04:00

28 lines
735 B
TypeScript

import { join } from 'path';
import { readJsonFile } from './fileutils';
export function readCacheDirectoryProperty(root: string) {
try {
const nxJson = readJsonFile(join(root, 'nx.json'));
return nxJson.tasksRunnerOptions.default.options.cacheDirectory;
} catch (e) {
return undefined;
}
}
export function cacheDirectory(root: string, cacheDirectory: string) {
const cacheDirFromEnv = process.env.NX_CACHE_DIRECTORY;
if (cacheDirFromEnv) {
cacheDirectory = cacheDirFromEnv;
}
if (cacheDirectory) {
if (cacheDirectory.startsWith('./')) {
return join(root, cacheDirectory);
} else {
return cacheDirectory;
}
} else {
return join(root, 'node_modules', '.cache', 'nx');
}
}