fix(version matching): now respecting packages without latest tag

This commit is contained in:
Philipp Kunz 2021-05-06 10:04:51 +00:00
parent 77d515d915
commit b4100688ac
2 changed files with 31 additions and 5 deletions

View File

@ -108,21 +108,20 @@ export class NpmPackage {
if (optionsArg.distTag && optionsArg.version) { if (optionsArg.distTag && optionsArg.version) {
throw new Error('Please either specify version OR disttag, not both.'); throw new Error('Please either specify version OR disttag, not both.');
} }
let targetVersion: plugins.smartversion.SmartVersion; let targetVersionString: string;
if (optionsArg.distTag) { if (optionsArg.distTag) {
const targetDistTag = this.allDistTags.find((distTag) => { const targetDistTag = this.allDistTags.find((distTag) => {
return distTag.name === optionsArg.distTag; return distTag.name === optionsArg.distTag;
}); });
if (targetDistTag) { if (targetDistTag) {
targetVersion = new plugins.smartversion.SmartVersion(targetDistTag.targetVersion); targetVersionString = targetDistTag.targetVersion;
} }
} else { } else {
targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(optionsArg.version); targetVersionString = optionsArg.version;
} }
// lets find the best matching release // lets find the best matching release
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version); const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
if (!bestMatchingVersion) { if (!bestMatchingVersion) {
return null; return null;
} }
@ -173,4 +172,15 @@ export class NpmPackage {
* updates the package with information from the registry * updates the package with information from the registry
*/ */
update() {} update() {}
/** */
public getBestMatchingVersion(versionArg: string): string {
// lets find the best matching release
const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
if (!bestMatchingVersion) {
return null;
}
}
} }

View File

@ -70,6 +70,14 @@ export class NpmRegistry {
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(fileId); const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(fileId);
if (!cachedFile) { if (!cachedFile) {
const npmPackage = await this.getPackageInfo(packageNameArg); const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageArg => packageArg.name === 'latest');
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
};
}
}
const fileResult = await npmPackage.getFileFromPackage(filePath, optionsArg); const fileResult = await npmPackage.getFileFromPackage(filePath, optionsArg);
this.registryCache.cacheSmartFile(fileId, fileResult); this.registryCache.cacheSmartFile(fileId, fileResult);
return fileResult; return fileResult;
@ -83,6 +91,14 @@ export class NpmRegistry {
version?: string; version?: string;
}): Promise<plugins.smartfile.Smartfile[]> { }): Promise<plugins.smartfile.Smartfile[]> {
const npmPackage = await this.getPackageInfo(packageNameArg); const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageArg => packageArg.name === 'latest');
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
};
}
}
return npmPackage.getFilesFromPackage(filePath, optionsArg); return npmPackage.getFilesFromPackage(filePath, optionsArg);
} }