Skip to content

Commit

Permalink
fix(core): use a main function inside nx.ts to avoid future hoisting …
Browse files Browse the repository at this point in the history
…issues (#15806)
  • Loading branch information
AgentEnder committed Mar 21, 2023
1 parent 247e8a2 commit 1e48f6a
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions packages/nx/bin/nx.ts
Expand Up @@ -14,71 +14,73 @@ import { major } from 'semver';
import { readJsonFile } from '../src/utils/fileutils';
import { execSync } from 'child_process';

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] === 'graph' && !workspace)
) {
process.env.NX_DAEMON = 'false';
require('nx/src/command-line/nx-commands').commandsObject.argv;
} else {
if (workspace && workspace.type === 'nx') {
require('v8-compile-cache');
}
// polyfill rxjs observable to avoid issues with multiple version of Observable installed in node_modules
// https://twitter.com/BenLesh/status/1192478226385428483?s=20
if (!(Symbol as any).observable)
(Symbol as any).observable = Symbol('observable polyfill');
function main() {
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] === 'graph' && !workspace)
) {
process.env.NX_DAEMON = 'false';
require('nx/src/command-line/nx-commands').commandsObject.argv;
} else {
if (workspace && workspace.type === 'nx') {
require('v8-compile-cache');
}
// polyfill rxjs observable to avoid issues with multiple version of Observable installed in node_modules
// https://twitter.com/BenLesh/status/1192478226385428483?s=20
if (!(Symbol as any).observable)
(Symbol as any).observable = Symbol('observable polyfill');

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 <workspace name>`),
'',
`To add Nx to existing workspace run with a workspace-specific nx.json:`,
chalk.bold.white(`npx add-nx-to-monorepo@latest`),
'',
`To add the default nx.json file run:`,
chalk.bold.white(`nx init`),
],
});
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 <workspace name>`),
'',
`To add Nx to existing workspace run with a workspace-specific nx.json:`,
chalk.bold.white(`npx add-nx-to-monorepo@latest`),
'',
`To add the default nx.json file run:`,
chalk.bold.white(`nx init`),
],
});

output.note({
title: `For more information please visit https://nx.dev/`,
});
process.exit(1);
}
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 = resolveNx(workspace);
} catch {
// If we can't resolve a local copy of Nx, we must be global.
warnIfUsingOutdatedGlobalInstall();
output.error({
title: `Could not find Nx modules in this workspace.`,
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
});
process.exit(1);
}
// Make sure that a local copy of Nx exists in workspace
let localNx: string;
try {
localNx = resolveNx(workspace);
} catch {
// If we can't resolve a local copy of Nx, we must be global.
warnIfUsingOutdatedGlobalInstall();
output.error({
title: `Could not find Nx modules in this workspace.`,
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
});
process.exit(1);
}

// this file is already in the local workspace
if (localNx === resolveNx(null)) {
initLocal(workspace);
} else {
// Nx is being run from globally installed CLI - hand off to the local
warnIfUsingOutdatedGlobalInstall(getLocalNxVersion(workspace));
if (localNx.includes('.nx')) {
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
require(nxWrapperPath);
// this file is already in the local workspace
if (localNx === resolveNx(null)) {
initLocal(workspace);
} else {
require(localNx);
// Nx is being run from globally installed CLI - hand off to the local
warnIfUsingOutdatedGlobalInstall(getLocalNxVersion(workspace));
if (localNx.includes('.nx')) {
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
require(nxWrapperPath);
} else {
require(localNx);
}
}
}
}
Expand Down Expand Up @@ -159,3 +161,5 @@ const getLatestVersionOfNx = ((fn: () => string) => {
let cache: string = null;
return () => cache || (cache = fn());
})(_getLatestVersionOfNx);

main();

1 comment on commit 1e48f6a

@vercel
Copy link

@vercel vercel bot commented on 1e48f6a Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.