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 { remove } from 'fs-extra';
import { removeSync } from 'fs-extra';
import { stop as stopDaemon } from '../core/project-graph/daemon/client/client';
import {
cacheDirectory,
readCacheDirectoryProperty,
} from '../utilities/cache-directory';
import { cacheDir } from '../utilities/cache-directory';
import { output } from '../utilities/output';
export async function resetHandler() {
export function resetHandler() {
output.note({
title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.',
bodyLines: [`This might take a few minutes.`],
});
const dir = cacheDirectory(
appRootPath,
readCacheDirectoryProperty(appRootPath)
);
await Promise.all([stopDaemon(), remove(dir)]);
stopDaemon();
removeSync(cacheDir);
output.success({
title: 'Successful reset the Nx workspace.',
});

View File

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

View File

@ -1,6 +1,7 @@
import { logger, ProjectGraph } from '@nrwl/devkit';
import { ChildProcess, spawn, spawnSync } from 'child_process';
import { openSync, readFileSync } from 'fs';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { connect } from 'net';
import { performance } from 'perf_hooks';
import {
@ -8,10 +9,15 @@ import {
writeDaemonJsonProcessCache,
} from '../cache';
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']> {
await safelyCleanUpExistingProcess();
ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE);
ensureFileSync(DAEMON_OUTPUT_LOG_FILE);
try {
const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a');

View File

@ -5,10 +5,9 @@
*/
import { normalizePath } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { ensureDirSync, ensureFileSync } from 'fs-extra';
import { tmpdir } from 'os';
import { join } from 'path';
import { createHash } from 'crypto';
import { cacheDir } from '../../../utilities/cache-directory';
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
function escapeRegExp(string) {
@ -48,22 +47,16 @@ const subDirForCurrentWorkspace = createHash('sha1')
.digest('hex')
.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(
tmpdir(),
cacheDir,
'nx-daemon',
subDirForCurrentWorkspace
);
ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE);
export const DAEMON_OUTPUT_LOG_FILE = join(
DAEMON_DIR_FOR_CURRENT_WORKSPACE,
'server.log'
);
ensureFileSync(DAEMON_OUTPUT_LOG_FILE);
export const DAEMON_SOCKET_PATH = join(
DAEMON_DIR_FOR_CURRENT_WORKSPACE,

View File

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

View File

@ -1,7 +1,8 @@
import { NxJsonConfiguration, readJsonFile } from '@nrwl/devkit';
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 {
const nxJson = readJsonFile<NxJsonConfiguration>(join(root, 'nx.json'));
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;
if (cacheDirFromEnv) {
cacheDirectory = cacheDirFromEnv;
@ -25,3 +26,8 @@ export function cacheDirectory(root: string, cacheDirectory: string) {
return join(root, 'node_modules', '.cache', 'nx');
}
}
export const cacheDir = cacheDirectory(
appRootPath,
readCacheDirectoryProperty(appRootPath)
);