fix(misc): nx version should report global version outside of an nx repo (#16918)

This commit is contained in:
Craigory Coppola 2023-05-10 18:09:19 -04:00 committed by GitHub
parent 548f8d7fb1
commit 09525e8b30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 146 additions and 99 deletions

View File

@ -1,6 +1,7 @@
import type { NxJsonConfiguration } from '@nx/devkit';
import {
cleanupProject,
createNonNxProjectDirectory,
e2eCwd,
getPackageManagerCommand,
getPublishedVersion,
@ -588,8 +589,6 @@ describe('global installation', () => {
let oldPath: string;
beforeAll(() => {
newProject();
ensureDirSync(globalsPath);
writeFileSync(
path.join(path.dirname(path.dirname(globalsPath)), 'package.json'),
@ -618,6 +617,11 @@ describe('global installation', () => {
process.env.PATH = oldPath;
});
describe('inside nx directory', () => {
beforeAll(() => {
newProject();
});
it('should invoke Nx commands from local repo', () => {
const nxJsContents = readFile('node_modules/nx/bin/nx.js');
updateFile('node_modules/nx/bin/nx.js', `console.log('local install');`);
@ -681,3 +685,27 @@ describe('global installation', () => {
updateFile('node_modules/nx/package.json', packageJsonContents);
});
});
describe('non-nx directory', () => {
beforeAll(() => {
createNonNxProjectDirectory();
});
it('--version should report global version and local not found', () => {
let output: string;
expect(() => {
output = runCommand(`nx --version`);
}).not.toThrow();
expect(output).toContain(`- Local: Not found`);
expect(output).toContain(`- Global: v${getPublishedVersion()}`);
});
it('graph should work in npm workspaces repo', () => {
expect(() => {
runCommand(`nx graph --file graph.json`);
}).not.toThrow();
const { graph } = readJson('graph.json');
expect(graph).toHaveProperty('nodes');
});
});
});

View File

@ -35,47 +35,27 @@ function main() {
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 an existing workspace with a workspace-specific nx.json, run:`,
chalk.bold.white(`npx nx@latest init`),
],
});
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);
localNx = workspace && resolveNx(workspace);
} catch {
localNx = null;
}
const isLocalInstall = localNx === resolveNx(null);
const LOCAL_NX_VERSION: string | null = localNx
? getLocalNxVersion(workspace)
: null;
const GLOBAL_NX_VERSION: string | null = isLocalInstall
? null
: require('../package.json').version;
globalThis.GLOBAL_NX_VERSION ??= GLOBAL_NX_VERSION;
const { LOCAL_NX_VERSION, GLOBAL_NX_VERSION } = determineNxVersions(
localNx,
workspace,
isLocalInstall
);
if (process.argv[2] === '--version') {
console.log(stripIndents`Nx Version:
- Local: ${LOCAL_NX_VERSION ? 'v' + LOCAL_NX_VERSION : 'Not found'}
- Global: ${GLOBAL_NX_VERSION ? 'v' + GLOBAL_NX_VERSION : 'Not found'}`);
process.exit(0);
handleNxVersionCommand(LOCAL_NX_VERSION, GLOBAL_NX_VERSION);
}
if (!workspace) {
handleNoWorkspace();
}
if (!localNx) {
@ -98,6 +78,50 @@ function main() {
}
}
function handleNoWorkspace() {
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 an existing workspace with a workspace-specific nx.json, run:`,
chalk.bold.white(`npx nx@latest init`),
],
});
output.note({
title: `For more information please visit https://nx.dev/`,
});
process.exit(1);
}
function handleNxVersionCommand(
LOCAL_NX_VERSION: string,
GLOBAL_NX_VERSION: string
) {
console.log(stripIndents`Nx Version:
- Local: ${LOCAL_NX_VERSION ? 'v' + LOCAL_NX_VERSION : 'Not found'}
- Global: ${GLOBAL_NX_VERSION ? 'v' + GLOBAL_NX_VERSION : 'Not found'}`);
process.exit(0);
}
function determineNxVersions(
localNx: string,
workspace: WorkspaceTypeAndRoot,
isLocalInstall: boolean
) {
const LOCAL_NX_VERSION: string | null = localNx
? getLocalNxVersion(workspace)
: null;
const GLOBAL_NX_VERSION: string | null = isLocalInstall
? null
: require('../package.json').version;
globalThis.GLOBAL_NX_VERSION ??= GLOBAL_NX_VERSION;
return { LOCAL_NX_VERSION, GLOBAL_NX_VERSION };
}
function resolveNx(workspace: WorkspaceTypeAndRoot | null) {
// prefer Nx installed in .nx/installation
try {
@ -167,9 +191,6 @@ function warnIfUsingOutdatedGlobalInstall(
}
function getLocalNxVersion(workspace: WorkspaceTypeAndRoot): string | null {
// TODO(v17): Remove @nrwl/cli from this list
const localNxPackages = ['nx', '@nrwl/tao', '@nrwl/cli'];
for (const pkg of localNxPackages) {
try {
const { packageJson } = readModulePackageJson(
'nx',
@ -178,7 +199,6 @@ function getLocalNxVersion(workspace: WorkspaceTypeAndRoot): string | null {
return packageJson.version;
} catch {}
}
}
function _getLatestVersionOfNx(): string {
try {

View File

@ -6,7 +6,6 @@ import {
} from '../config/workspace-json-project-json';
import { readJsonFile } from './fileutils';
import { getNxRequirePaths } from './installation-directory';
import { workspaceRoot } from './workspace-root';
export type PackageJsonTargetConfiguration = Omit<
TargetConfiguration,