Skip to content

Commit

Permalink
fix(core): encapsulated nx repos should invoke wrapper when invoked v…
Browse files Browse the repository at this point in the history
…ia npx or global install (#15337)
  • Loading branch information
AgentEnder authored Mar 1, 2023
1 parent 2791444 commit c9244d2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ if (

// this file is already in the local workspace
if (localNx === resolveNx(null)) {
if (localNx.includes('.nx') && !process.env.NX_WRAPPER_SET) {
const nxWrapperPath = localNx.replace(/\.nx.*/, '.nx/') + 'nxw.js';
require(nxWrapperPath);
}
initLocal(workspace);
} else {
// Nx is being run from globally installed CLI - hand off to the local
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"version": "15.0.12-beta.1",
"description": "Set project names in project.json files",
"implementation": "./src/migrations/update-15-1-0/set-project-names"
},
"15.8.2-update-nx-wrapper": {
"cli": "nx",
"version": "15.8.2-beta.0",
"description": "Updates the nx wrapper in encapsulated repos.",
"implementation": "./src/migrations/update-15-8-2/update-nxw"
}
}
}
13 changes: 13 additions & 0 deletions packages/nx/src/migrations/update-15-8-2/update-nxw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
getNxWrapperContents,
nxWrapperPath,
} from 'nx/src/nx-init/encapsulated/add-nx-scripts';
import { normalizePath } from '../../utils/path';
import { Tree } from '../../generators/tree';

export default async function (tree: Tree) {
const wrapperPath = normalizePath(nxWrapperPath());
if (tree.exists(wrapperPath)) {
tree.write(wrapperPath, getNxWrapperContents());
}
}
6 changes: 3 additions & 3 deletions packages/nx/src/nx-init/encapsulated/add-nx-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../generators/tree';
import { writeJson } from '../../generators/utils/json';

const nxWrapperPath = (p: typeof import('path') = path) =>
export const nxWrapperPath = (p: typeof import('path') = path) =>
p.join('.nx', 'nxw.js');

const NODE_MISSING_ERR =
Expand All @@ -36,7 +36,7 @@ export function generateEncapsulatedNxSetup(version?: string) {
const host = new FsTree(process.cwd(), false);
writeMinimalNxJson(host, version);
updateGitIgnore(host);
host.write(nxWrapperPath(), getNodeScriptContents());
host.write(nxWrapperPath(), getNxWrapperContents());
host.write('nx.bat', BATCH_SCRIPT_CONTENTS);
host.write('nx', SHELL_SCRIPT_CONTENTS, {
mode: FsConstants.S_IXUSR | FsConstants.S_IRUSR | FsConstants.S_IWUSR,
Expand Down Expand Up @@ -75,7 +75,7 @@ export function updateGitIgnore(host: Tree) {
);
}

function getNodeScriptContents() {
export function getNxWrapperContents() {
// Read nxw.js, but remove any empty comments or comments that start with `//#: `
// This removes the sourceMapUrl since it is invalid, as well as any internal comments.
return readFileSync(path.join(__dirname, 'nxw.js'), 'utf-8').replace(
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/nx-init/encapsulated/nxw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function ensureUpToDateInstallation() {
}
}

if (require.main === module) {
if (require.main === module && !process.env.NX_WRAPPER_SET) {
process.env.NX_WRAPPER_SET = 'true';
ensureUpToDateInstallation();
require('./installation/node_modules/nx/bin/nx');
}

1 comment on commit c9244d2

@vercel
Copy link

@vercel vercel bot commented on c9244d2 Mar 1, 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-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.