All files / src/version-service version-service.ts

100% Statements 29/29
100% Branches 9/9
100% Functions 7/7
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 7617x       17x 17x   17x   17x     1x 1x       6x       8x       1x       1x 1x       6x 6x       6x       6x 1x         5x 10x     5x 5x       5x 3x 2x   2x 2x 2x       5x 5x            
import { ZeedhiCoreError } from '..';
import { IDictionary } from '../utils';
import { IVersionType, Package, PackageLock } from './interfaces';
 
export class VersionService {
	private static appVersion: IVersionType = {};
 
	private static packageVersions: IDictionary<string> = {};
 
	private static PREFIX = '@zeedhi';
 
	public static addAppVersion(name: string, version: string) {
		VersionService.appVersion.name = name;
		VersionService.appVersion.version = version;
	}
 
	public static addPackageVersion(name: string, version: string) {
		VersionService.packageVersions[name] = version;
	}
 
	public static getPackageVersions(): object {
		return VersionService.packageVersions;
	}
 
	public static getAppVersion(): IVersionType {
		return VersionService.appVersion;
	}
 
	public static populateVersionsFromFile(packageData: Package, packageLockData: PackageLock) {
		VersionService.addAppVersion(packageData.name, packageData.version);
		VersionService.populateDependencyVersions(packageLockData);
	}
 
	public static clearPackageVersions() {
		VersionService.appVersion = {};
		VersionService.packageVersions = {};
	}
 
	public static populateDependencyVersions(lockData: PackageLock) {
		const prefix = VersionService.PREFIX;
 
		// The 'packages' key is used in modern package-lock.json (npm v7+).
		// It provides a flatter, more comprehensive list of dependencies.
		if (!lockData.packages) {
			throw new ZeedhiCoreError("VersionService could not find the 'packages' key in package-lock.json. The file format might be outdated.");
		}
 
		// Iterate over each entry in the 'packages' object.
		// The key is the package path, e.g., "node_modules/@something/my-package".
		for (const packagePath in lockData.packages) {
			if (packagePath.startsWith(`node_modules/${prefix}`)) {
				// Extract the package name from the path.
				// e.g., "node_modules/@something/my-package" -> "@something/my-package"
				const packageName = packagePath.substring('node_modules/'.length);
				const packageInfo = lockData.packages[packagePath];
 
				let version: string | undefined;
 
				if (packageInfo.version) {
					version = packageInfo.version;
				} else if (packageInfo.link && packageInfo.resolved) {
					// The 'resolved' property points to the key of the source package definition.
					const sourcePackageInfo = lockData.packages[packageInfo.resolved];
					if (sourcePackageInfo?.version) {
						version = sourcePackageInfo.version;
					}
				}
 
				if (version) {
					VersionService.addPackageVersion(packageName, version);
				}
			}
		}
	}
}