Skip to content

Commit

Permalink
fix: check lerna version for nx workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Oct 11, 2022
1 parent 585fc9d commit 247b6f1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
20 changes: 12 additions & 8 deletions libs/shared/npm/src/lib/nx-version.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { findNxPackagePath } from './find-nx-package-path';
import { coerce, SemVer } from 'semver';

declare function __non_webpack_require__(importPath: string): any;

let nxWorkspacePackageJson: { version: string };
let loadedNxPackage = false;
export async function nxVersion(workspacePath: string): Promise<number> {

const defaultSemver = new SemVer('0.0.0');

export async function nxVersion(workspacePath: string): Promise<SemVer> {
if (!loadedNxPackage) {
const packagePath = await findNxPackagePath(workspacePath, 'package.json');

if (!packagePath) {
return 0;
return defaultSemver;
}

nxWorkspacePackageJson = __non_webpack_require__(packagePath);
loadedNxPackage = true;
}

if (!nxWorkspacePackageJson) {
return 0;
return defaultSemver;
}
const nxPackageVersion = nxWorkspacePackageJson.version;
const majorVersion = nxPackageVersion.split('.')[0];
if (!majorVersion) {
return 0;
const nxVersion = coerce(nxWorkspacePackageJson.version);
if (!nxVersion) {
return defaultSemver;
}
return +majorVersion;

return nxVersion;
}
2 changes: 1 addition & 1 deletion libs/shared/npm/src/lib/workspace-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async function localDependencies(
}

// Local plugins do not work with nxVersion less than 13
if ((await nxVersion(workspacePath)) < 13) {
if ((await nxVersion(workspacePath)).major < 13) {
return [];
}

Expand Down
20 changes: 18 additions & 2 deletions libs/shared/utils/src/lib/check-is-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@ import {
fileExists,
readAndCacheJsonFile,
} from '@nx-console/shared/file-system';
import { workspaceDependencyPath } from '@nx-console/shared/npm';
import { coerce } from 'semver';

export async function checkIsNxWorkspace(
workspacePath: string
): Promise<boolean> {
let isNxWorkspace = await fileExists(join(workspacePath, 'nx.json'));

if (!isNxWorkspace) {
const lerna = await readAndCacheJsonFile('lerna.json', workspacePath);
isNxWorkspace = lerna.json.useNx ?? false;
const lernaPath = await workspaceDependencyPath(workspacePath, 'lerna');
if (!lernaPath) {
return false;
}

const lernaPackageJson = await readAndCacheJsonFile(
join(lernaPath, 'package.json')
);
const lernaVersion = coerce(lernaPackageJson.json.version);

if (lernaVersion?.major ?? 0 >= 6) {
isNxWorkspace = true;
} else {
const lerna = await readAndCacheJsonFile('lerna.json', workspacePath);
isNxWorkspace = lerna.json.useNx ?? false;
}
}

return isNxWorkspace;
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function getNxWorkspaceConfig(
}> {
const version = await nxVersion(workspacePath);

if (version < 12) {
if (version.major < 12) {
return readWorkspaceConfigs(format, workspacePath);
}

Expand Down Expand Up @@ -68,7 +68,7 @@ export async function getNxWorkspaceConfig(
console.warn('process.exit called with code', code);
} as (code?: number) => never;

if (version < 13) {
if (version.major < 13) {
projectGraph = (nxProjectGraph as any).createProjectGraph();
} else {
projectGraph = await nxProjectGraph.createProjectGraphAsync({
Expand Down
2 changes: 1 addition & 1 deletion libs/vscode/tasks/src/lib/cli-task-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function registerCliTaskCommands(
* move and remove were release in patch 8.11
*/
const version = await nxVersion(cliTaskProvider.getWorkspacePath());
if (version >= 8) {
if (version.major >= 8) {
commands.registerCommand(`${cli}.move.fileexplorer`, async (uri: Uri) => {
/**
* Bit of a hack - always runs angular/move if it is installed.
Expand Down

0 comments on commit 247b6f1

Please sign in to comment.