fix(core): change default folder for daemon assets to cache dir

This commit is contained in:
Victor Savkin 2022-01-10 16:24:41 -05:00 committed by Victor Savkin
parent a40ef21a20
commit 75cc52fd48
6 changed files with 31 additions and 42 deletions

View File

@ -1,22 +1,15 @@
import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { removeSync } from 'fs-extra';
import { remove } from 'fs-extra';
import { stop as stopDaemon } from '../core/project-graph/daemon/client/client'; import { stop as stopDaemon } from '../core/project-graph/daemon/client/client';
import { import { cacheDir } from '../utilities/cache-directory';
cacheDirectory,
readCacheDirectoryProperty,
} from '../utilities/cache-directory';
import { output } from '../utilities/output'; import { output } from '../utilities/output';
export async function resetHandler() { export function resetHandler() {
output.note({ output.note({
title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.', title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.',
bodyLines: [`This might take a few minutes.`], bodyLines: [`This might take a few minutes.`],
}); });
const dir = cacheDirectory( stopDaemon();
appRootPath, removeSync(cacheDir);
readCacheDirectoryProperty(appRootPath)
);
await Promise.all([stopDaemon(), remove(dir)]);
output.success({ output.success({
title: 'Successful reset the Nx workspace.', title: 'Successful reset the Nx workspace.',
}); });

View File

@ -10,15 +10,11 @@ import type {
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { readJsonFile, writeJsonFile } from '@nrwl/devkit'; import { readJsonFile, writeJsonFile } from '@nrwl/devkit';
import { join } from 'path'; import { join } from 'path';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
import { ensureDirSync } from 'fs-extra'; import { ensureDirSync } from 'fs-extra';
import { directoryExists, fileExists } from '../../utilities/fileutils'; import { directoryExists, fileExists } from '../../utilities/fileutils';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
import { import { cacheDir } from '../../utilities/cache-directory';
cacheDirectory,
readCacheDirectoryProperty,
} from '../../utilities/cache-directory';
export interface ProjectGraphCache { export interface ProjectGraphCache {
version: string; version: string;
@ -33,16 +29,12 @@ export interface ProjectGraphCache {
dependencies: Record<string, ProjectGraphDependency[]>; dependencies: Record<string, ProjectGraphDependency[]>;
} }
export const nxDepsDir = cacheDirectory( export const nxDepsPath = join(cacheDir, 'nxdeps.json');
appRootPath,
readCacheDirectoryProperty(appRootPath)
);
export const nxDepsPath = join(nxDepsDir, 'nxdeps.json');
export function ensureCacheDirectory(): void { export function ensureCacheDirectory(): void {
try { try {
if (!existsSync(nxDepsDir)) { if (!existsSync(cacheDir)) {
ensureDirSync(nxDepsDir); ensureDirSync(cacheDir);
} }
} catch (e) { } catch (e) {
/* /*
@ -55,8 +47,8 @@ export function ensureCacheDirectory(): void {
* In this case, we're creating the directory. If the operation failed, we ensure that the directory * In this case, we're creating the directory. If the operation failed, we ensure that the directory
* exists before continuing (or raise an exception). * exists before continuing (or raise an exception).
*/ */
if (!directoryExists(nxDepsDir)) { if (!directoryExists(cacheDir)) {
throw new Error(`Failed to create directory: ${nxDepsDir}`); throw new Error(`Failed to create directory: ${cacheDir}`);
} }
} }
} }

View File

@ -1,6 +1,7 @@
import { logger, ProjectGraph } from '@nrwl/devkit'; import { logger, ProjectGraph } from '@nrwl/devkit';
import { ChildProcess, spawn, spawnSync } from 'child_process'; import { ChildProcess, spawn, spawnSync } from 'child_process';
import { openSync, readFileSync } from 'fs'; import { openSync, readFileSync } from 'fs';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { connect } from 'net'; import { connect } from 'net';
import { performance } from 'perf_hooks'; import { performance } from 'perf_hooks';
import { import {
@ -8,10 +9,15 @@ import {
writeDaemonJsonProcessCache, writeDaemonJsonProcessCache,
} from '../cache'; } from '../cache';
import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils'; import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils';
import { DAEMON_OUTPUT_LOG_FILE } from '../tmp-dir'; import {
DAEMON_DIR_FOR_CURRENT_WORKSPACE,
DAEMON_OUTPUT_LOG_FILE,
} from '../tmp-dir';
export async function startInBackground(): Promise<ChildProcess['pid']> { export async function startInBackground(): Promise<ChildProcess['pid']> {
await safelyCleanUpExistingProcess(); await safelyCleanUpExistingProcess();
ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE);
ensureFileSync(DAEMON_OUTPUT_LOG_FILE);
try { try {
const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a');

View File

@ -5,10 +5,9 @@
*/ */
import { normalizePath } from '@nrwl/devkit'; import { normalizePath } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { tmpdir } from 'os';
import { join } from 'path'; import { join } from 'path';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import { cacheDir } from '../../../utilities/cache-directory';
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
function escapeRegExp(string) { function escapeRegExp(string) {
@ -48,22 +47,16 @@ const subDirForCurrentWorkspace = createHash('sha1')
.digest('hex') .digest('hex')
.slice(0, 32); .slice(0, 32);
/**
* E.g. on a mac the value for this will be something like:
* /var/folders/zk/9ff5snsj71j2r07qht44w_nr0000gn/T/nx-daemon/{{subDirForCurrentWorkspace}}
*/
export const DAEMON_DIR_FOR_CURRENT_WORKSPACE = join( export const DAEMON_DIR_FOR_CURRENT_WORKSPACE = join(
tmpdir(), cacheDir,
'nx-daemon', 'nx-daemon',
subDirForCurrentWorkspace subDirForCurrentWorkspace
); );
ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE);
export const DAEMON_OUTPUT_LOG_FILE = join( export const DAEMON_OUTPUT_LOG_FILE = join(
DAEMON_DIR_FOR_CURRENT_WORKSPACE, DAEMON_DIR_FOR_CURRENT_WORKSPACE,
'server.log' 'server.log'
); );
ensureFileSync(DAEMON_OUTPUT_LOG_FILE);
export const DAEMON_SOCKET_PATH = join( export const DAEMON_SOCKET_PATH = join(
DAEMON_DIR_FOR_CURRENT_WORKSPACE, DAEMON_DIR_FOR_CURRENT_WORKSPACE,

View File

@ -14,7 +14,7 @@ import {
import { dirname, join, resolve, sep } from 'path'; import { dirname, join, resolve, sep } from 'path';
import { DefaultTasksRunnerOptions } from './default-tasks-runner'; import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { spawn, exec } from 'child_process'; import { spawn, exec } from 'child_process';
import { cacheDirectory } from '../utilities/cache-directory'; import { cacheDir } from '../utilities/cache-directory';
const util = require('util'); const util = require('util');
@ -320,9 +320,8 @@ export class Cache {
} }
private createCacheDir() { private createCacheDir() {
const dir = cacheDirectory(this.root, this.options.cacheDirectory); ensureDirSync(cacheDir);
ensureDirSync(dir); return cacheDir;
return dir;
} }
private createTerminalOutputsDir() { private createTerminalOutputsDir() {

View File

@ -1,7 +1,8 @@
import { NxJsonConfiguration, readJsonFile } from '@nrwl/devkit'; import { NxJsonConfiguration, readJsonFile } from '@nrwl/devkit';
import { join } from 'path'; import { join } from 'path';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
export function readCacheDirectoryProperty(root: string): string | undefined { function readCacheDirectoryProperty(root: string): string | undefined {
try { try {
const nxJson = readJsonFile<NxJsonConfiguration>(join(root, 'nx.json')); const nxJson = readJsonFile<NxJsonConfiguration>(join(root, 'nx.json'));
return nxJson.tasksRunnerOptions.default.options.cacheDirectory; return nxJson.tasksRunnerOptions.default.options.cacheDirectory;
@ -10,7 +11,7 @@ export function readCacheDirectoryProperty(root: string): string | undefined {
} }
} }
export function cacheDirectory(root: string, cacheDirectory: string) { function cacheDirectory(root: string, cacheDirectory: string) {
const cacheDirFromEnv = process.env.NX_CACHE_DIRECTORY; const cacheDirFromEnv = process.env.NX_CACHE_DIRECTORY;
if (cacheDirFromEnv) { if (cacheDirFromEnv) {
cacheDirectory = cacheDirFromEnv; cacheDirectory = cacheDirFromEnv;
@ -25,3 +26,8 @@ export function cacheDirectory(root: string, cacheDirectory: string) {
return join(root, 'node_modules', '.cache', 'nx'); return join(root, 'node_modules', '.cache', 'nx');
} }
} }
export const cacheDir = cacheDirectory(
appRootPath,
readCacheDirectoryProperty(appRootPath)
);