Skip to content

Commit 521ef30

Browse files
author
John Doe
committed
refactor: fix file not found of for uninstalled packages
1 parent eff86ed commit 521ef30

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

packages/nx-plugin/src/internal/versions.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@ import type { PackageJson } from 'nx/src/utils/package-json';
55
const workspaceRoot = path.join(__dirname, '../../');
66
const projectsFolder = path.join(__dirname, '../../../');
77

8-
export const cpNxPluginVersion = loadPackageJson(workspaceRoot).version;
9-
export const cpModelVersion = loadPackageJson(
8+
export const cpNxPluginVersion = readDependencyVersion(workspaceRoot);
9+
export const cpModelVersion = readDependencyVersion(
1010
path.join(projectsFolder, 'cli'),
11-
).version;
12-
export const cpUtilsVersion = loadPackageJson(
11+
);
12+
export const cpUtilsVersion = readDependencyVersion(
1313
path.join(projectsFolder, 'utils'),
14-
).version;
15-
export const cpCliVersion = loadPackageJson(
14+
);
15+
export const cpCliVersion = readDependencyVersion(
1616
path.join(projectsFolder, 'models'),
17-
).version;
17+
);
1818

1919
/**
20-
* Load the package.json file from the given folder path.
21-
* @param folderPath
20+
* Load the package.json file from the given folder path and returns the package version.
21+
* If the version is not given of the package.json file does not exist it returns the fallback value.
2222
*/
23-
function loadPackageJson(folderPath: string): PackageJson {
24-
return readJsonFile<PackageJson>(path.join(folderPath, 'package.json'));
23+
function readDependencyVersion(
24+
folderPath: string,
25+
fallbackVersion = 'latest',
26+
): string {
27+
try {
28+
return (
29+
readJsonFile<PackageJson>(path.join(folderPath, 'package.json'))
30+
.version ?? fallbackVersion
31+
);
32+
} catch {
33+
return fallbackVersion;
34+
}
2535
}

0 commit comments

Comments
 (0)