From d6657ad0622e90b43890721a75ea4071e7666f10 Mon Sep 17 00:00:00 2001 From: Tasos Bekos Date: Fri, 30 Oct 2020 19:21:32 +0200 Subject: [PATCH] fix(core): use `cwd` for locating workspace root Closes #3484 Closes #3909 --- packages/cli/bin/nx.ts | 39 +++++++++++++++++++++++++++---- packages/cli/lib/init-global.ts | 41 --------------------------------- 2 files changed, 35 insertions(+), 45 deletions(-) delete mode 100644 packages/cli/lib/init-global.ts diff --git a/packages/cli/bin/nx.ts b/packages/cli/bin/nx.ts index b20201e33a2c7a..523f812eb7da70 100644 --- a/packages/cli/bin/nx.ts +++ b/packages/cli/bin/nx.ts @@ -3,14 +3,45 @@ // polyfill rxjs observable to avoid issues with multiple version fo Observable installed in node_modules // https://twitter.com/BenLesh/status/1192478226385428483?s=20 (Symbol as any).observable = Symbol('observable polyfill'); +import chalk from 'chalk'; import { findWorkspaceRoot } from '../lib/find-workspace-root'; -import { initGlobal } from '../lib/init-global'; import { initLocal } from '../lib/init-local'; +import { output } from '../lib/output'; -const workspace = findWorkspaceRoot(__dirname); +const workspace = findWorkspaceRoot(process.cwd()); -if (workspace) { +if (!workspace) { + output.log({ + title: `The current directory isn't part of an Nx workspace.`, + bodyLines: [ + `To create a workspace run:`, + chalk.bold.white(`npx create-nx-workspace@latest `), + ], + }); + + output.note({ + title: `For more information please visit https://nx.dev/`, + }); + process.exit(1); +} + +// Make sure that a local copy of Nx exists in workspace +let localNx: string; +try { + localNx = require.resolve('@nrwl/cli/bin/nx.js', { + paths: [workspace.dir], + }); +} catch (e) { + output.error({ + title: `Could not find Nx modules in this workspace.`, + bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`], + }); + process.exit(1); +} + +if (localNx === require.resolve('@nrwl/cli/bin/nx.js')) { initLocal(workspace); } else { - initGlobal(); + // Nx is being run from globally installed CLI - hand off to the local + require(localNx); } diff --git a/packages/cli/lib/init-global.ts b/packages/cli/lib/init-global.ts deleted file mode 100644 index a21e6c9e3d0867..00000000000000 --- a/packages/cli/lib/init-global.ts +++ /dev/null @@ -1,41 +0,0 @@ -import chalk from 'chalk'; -import * as fs from 'fs'; -import * as path from 'path'; -import { findWorkspaceRoot } from './find-workspace-root'; -import { output } from './output'; - -/** - * Nx is being run from outside a workspace - */ -export function initGlobal() { - const workspace = findWorkspaceRoot(process.cwd()); - - if (workspace) { - // Found a workspace root - hand off to the local copy of Nx - try { - const localNx = require.resolve('@nrwl/cli/bin/nx.js', { - paths: [workspace.dir], - }); - require(localNx); - } catch (e) { - output.error({ - title: `Could not find @nrwl/cli module in this workspace.`, - bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`], - }); - process.exit(1); - } - } else { - output.log({ - title: `The current directory isn't part of an Nx workspace.`, - bodyLines: [ - `To create a workspace run:`, - chalk.bold.white(`npx create-nx-workspace@latest `), - ], - }); - - output.note({ - title: `For more information please visit https://nx.dev/`, - }); - process.exit(1); - } -}