Skip to content

Commit 1e48f6a

Browse files
authoredMar 21, 2023
fix(core): use a main function inside nx.ts to avoid future hoisting issues (#15806)
1 parent 247e8a2 commit 1e48f6a

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed
 

‎packages/nx/bin/nx.ts

+64-60
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,73 @@ import { major } from 'semver';
1414
import { readJsonFile } from '../src/utils/fileutils';
1515
import { execSync } from 'child_process';
1616

17-
const workspace = findWorkspaceRoot(process.cwd());
18-
// new is a special case because there is no local workspace to load
19-
if (
20-
process.argv[2] === 'new' ||
21-
process.argv[2] === '_migrate' ||
22-
process.argv[2] === 'init' ||
23-
(process.argv[2] === 'graph' && !workspace)
24-
) {
25-
process.env.NX_DAEMON = 'false';
26-
require('nx/src/command-line/nx-commands').commandsObject.argv;
27-
} else {
28-
if (workspace && workspace.type === 'nx') {
29-
require('v8-compile-cache');
30-
}
31-
// polyfill rxjs observable to avoid issues with multiple version of Observable installed in node_modules
32-
// https://twitter.com/BenLesh/status/1192478226385428483?s=20
33-
if (!(Symbol as any).observable)
34-
(Symbol as any).observable = Symbol('observable polyfill');
17+
function main() {
18+
const workspace = findWorkspaceRoot(process.cwd());
19+
// new is a special case because there is no local workspace to load
20+
if (
21+
process.argv[2] === 'new' ||
22+
process.argv[2] === '_migrate' ||
23+
process.argv[2] === 'init' ||
24+
(process.argv[2] === 'graph' && !workspace)
25+
) {
26+
process.env.NX_DAEMON = 'false';
27+
require('nx/src/command-line/nx-commands').commandsObject.argv;
28+
} else {
29+
if (workspace && workspace.type === 'nx') {
30+
require('v8-compile-cache');
31+
}
32+
// polyfill rxjs observable to avoid issues with multiple version of Observable installed in node_modules
33+
// https://twitter.com/BenLesh/status/1192478226385428483?s=20
34+
if (!(Symbol as any).observable)
35+
(Symbol as any).observable = Symbol('observable polyfill');
3536

36-
if (!workspace) {
37-
output.log({
38-
title: `The current directory isn't part of an Nx workspace.`,
39-
bodyLines: [
40-
`To create a workspace run:`,
41-
chalk.bold.white(`npx create-nx-workspace@latest <workspace name>`),
42-
'',
43-
`To add Nx to existing workspace run with a workspace-specific nx.json:`,
44-
chalk.bold.white(`npx add-nx-to-monorepo@latest`),
45-
'',
46-
`To add the default nx.json file run:`,
47-
chalk.bold.white(`nx init`),
48-
],
49-
});
37+
if (!workspace) {
38+
output.log({
39+
title: `The current directory isn't part of an Nx workspace.`,
40+
bodyLines: [
41+
`To create a workspace run:`,
42+
chalk.bold.white(`npx create-nx-workspace@latest <workspace name>`),
43+
'',
44+
`To add Nx to existing workspace run with a workspace-specific nx.json:`,
45+
chalk.bold.white(`npx add-nx-to-monorepo@latest`),
46+
'',
47+
`To add the default nx.json file run:`,
48+
chalk.bold.white(`nx init`),
49+
],
50+
});
5051

51-
output.note({
52-
title: `For more information please visit https://nx.dev/`,
53-
});
54-
process.exit(1);
55-
}
52+
output.note({
53+
title: `For more information please visit https://nx.dev/`,
54+
});
55+
process.exit(1);
56+
}
5657

57-
// Make sure that a local copy of Nx exists in workspace
58-
let localNx: string;
59-
try {
60-
localNx = resolveNx(workspace);
61-
} catch {
62-
// If we can't resolve a local copy of Nx, we must be global.
63-
warnIfUsingOutdatedGlobalInstall();
64-
output.error({
65-
title: `Could not find Nx modules in this workspace.`,
66-
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
67-
});
68-
process.exit(1);
69-
}
58+
// Make sure that a local copy of Nx exists in workspace
59+
let localNx: string;
60+
try {
61+
localNx = resolveNx(workspace);
62+
} catch {
63+
// If we can't resolve a local copy of Nx, we must be global.
64+
warnIfUsingOutdatedGlobalInstall();
65+
output.error({
66+
title: `Could not find Nx modules in this workspace.`,
67+
bodyLines: [`Have you run ${chalk.bold.white(`npm/yarn install`)}?`],
68+
});
69+
process.exit(1);
70+
}
7071

71-
// this file is already in the local workspace
72-
if (localNx === resolveNx(null)) {
73-
initLocal(workspace);
74-
} else {
75-
// Nx is being run from globally installed CLI - hand off to the local
76-
warnIfUsingOutdatedGlobalInstall(getLocalNxVersion(workspace));
77-
if (localNx.includes('.nx')) {
78-
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
79-
require(nxWrapperPath);
72+
// this file is already in the local workspace
73+
if (localNx === resolveNx(null)) {
74+
initLocal(workspace);
8075
} else {
81-
require(localNx);
76+
// Nx is being run from globally installed CLI - hand off to the local
77+
warnIfUsingOutdatedGlobalInstall(getLocalNxVersion(workspace));
78+
if (localNx.includes('.nx')) {
79+
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
80+
require(nxWrapperPath);
81+
} else {
82+
require(localNx);
83+
}
8284
}
8385
}
8486
}
@@ -159,3 +161,5 @@ const getLatestVersionOfNx = ((fn: () => string) => {
159161
let cache: string = null;
160162
return () => cache || (cache = fn());
161163
})(_getLatestVersionOfNx);
164+
165+
main();

1 commit comments

Comments
 (1)

vercel[bot] commented on Mar 21, 2023

@vercel[bot]

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.