From e8cfa35e8235daf6dbdb8392278e0c6be7feb46b Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 8 Jun 2022 21:19:26 -0400 Subject: [PATCH] feat(misc): add ability to run graph in any workspace --- packages/nx/bin/nx.ts | 6 ++++-- packages/nx/src/utils/find-workspace-root.ts | 12 +++--------- packages/nx/src/utils/workspace-root.ts | 4 ++-- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/nx/bin/nx.ts b/packages/nx/bin/nx.ts index 7b408aba4bcc9..dd11144ce34e2 100644 --- a/packages/nx/bin/nx.ts +++ b/packages/nx/bin/nx.ts @@ -8,15 +8,17 @@ import { initLocal } from './init-local'; import { detectPackageManager } from '../src/utils/package-manager'; import { output } from '../src/utils/output'; +const workspace = findWorkspaceRoot(process.cwd()); // new is a special case because there is no local workspace to load if ( process.argv[2] === 'new' || process.argv[2] === '_migrate' || - process.argv[2] === 'init' + process.argv[2] === 'init' || + (process.argv[2] === 'graph' && !workspace) ) { + process.env.NX_DAEMON = 'false'; require('nx/src/command-line/nx-commands').commandsObject.argv; } else { - const workspace = findWorkspaceRoot(process.cwd()); if (workspace && workspace.type === 'nx') { require('v8-compile-cache'); } diff --git a/packages/nx/src/utils/find-workspace-root.ts b/packages/nx/src/utils/find-workspace-root.ts index bf352f1e1004c..ca6fbb627435b 100644 --- a/packages/nx/src/utils/find-workspace-root.ts +++ b/packages/nx/src/utils/find-workspace-root.ts @@ -11,19 +11,13 @@ import { workspaceRootInner } from './workspace-root'; export function findWorkspaceRoot(dir: string): WorkspaceTypeAndRoot | null { const r = workspaceRootInner(dir, null); + if (r === null) return null; + if (existsSync(path.join(r, 'angular.json'))) { return { type: 'angular', dir: r }; - } - - if (existsSync(path.join(r, 'nx.json'))) { + } else { return { type: 'nx', dir: r }; } - - if (existsSync(path.join(r, 'node_modules', 'nx', 'package.json'))) { - return { type: 'nx', dir: r }; - } - - return null; } export interface WorkspaceTypeAndRoot { diff --git a/packages/nx/src/utils/workspace-root.ts b/packages/nx/src/utils/workspace-root.ts index 42db776784992..99a7d71fe3864 100644 --- a/packages/nx/src/utils/workspace-root.ts +++ b/packages/nx/src/utils/workspace-root.ts @@ -4,7 +4,7 @@ import { fileExists } from './fileutils'; /** * The root of the workspace */ -export const workspaceRoot = workspaceRootInner(__dirname, null); +export const workspaceRoot = workspaceRootInner(process.cwd(), process.cwd()); /** * The root of the workspace. @@ -19,7 +19,7 @@ export function workspaceRootInner( ): string { if (process.env.NX_WORKSPACE_ROOT_PATH) return process.env.NX_WORKSPACE_ROOT_PATH; - if (path.dirname(dir) === dir) return candidateRoot || process.cwd(); + if (path.dirname(dir) === dir) return candidateRoot; if (fileExists(path.join(dir, 'nx.json'))) { return dir; } else if (fileExists(path.join(dir, 'node_modules', 'nx', 'package.json'))) {