Compare commits

...

8 Commits

Author SHA1 Message Date
0ef6d0ccb2 1.0.34 2021-05-06 10:22:15 +00:00
8e538fd84d 1.0.33 2021-05-06 10:05:50 +00:00
6745115db7 fix(core): update 2021-05-06 10:05:49 +00:00
5ebce389d3 1.0.32 2021-05-06 10:04:51 +00:00
b4100688ac fix(version matching): now respecting packages without latest tag 2021-05-06 10:04:51 +00:00
77d515d915 1.0.31 2021-05-05 12:37:27 +00:00
aa71105b2d 1.0.30 2021-05-05 11:56:12 +00:00
e0ccb6c076 fix(core): update 2021-05-05 11:56:12 +00:00
5 changed files with 49 additions and 12 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartnpm", "name": "@pushrocks/smartnpm",
"version": "1.0.29", "version": "1.0.34",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartnpm", "name": "@pushrocks/smartnpm",
"version": "1.0.29", "version": "1.0.34",
"private": false, "private": false,
"description": "interface with npm to retrieve package information", "description": "interface with npm to retrieve package information",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

View File

@ -37,11 +37,21 @@ tap.test('should get package from verdaccio', async () => {
}); });
tap.test('should get a specific file from a package', async () => { tap.test('should get a specific file from a package', async () => {
const bundleFile = await verdaccioRegistry.getFileFromPackage( const wantedFile = await verdaccioRegistry.getFileFromPackage(
'@pushrocks/websetup', '@pushrocks/websetup',
'ts/index.ts' 'ts/index.ts'
); );
console.log(bundleFile.contentBuffer.toString()); console.log(wantedFile.contentBuffer.toString());
});
tap.test('should get a specific file from a package', async () => {
const wantedFiles = await verdaccioRegistry.getFilesFromPackage(
'@pushrocks/websetup',
'ts/'
);
for(const file of wantedFiles) {
console.log(file.path);
}
}); });
tap.start(); tap.start();

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;
} }
@ -140,7 +139,7 @@ export class NpmPackage {
// lets resolve with the wanted file // lets resolve with the wanted file
done.resolve([fileArg]); done.resolve([fileArg]);
subscription.unsubscribe(); subscription.unsubscribe();
} else if(fileArg.path.startsWith(wantedFilePath)) { } else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
allMatchingFiles.push(fileArg); allMatchingFiles.push(fileArg);
} }
}, },
@ -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;
@ -78,11 +86,20 @@ export class NpmRegistry {
} }
} }
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg: { public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
distTag?: string; distTag?: string;
version?: string; version?: string;
}): Promise<plugins.smartfile.Smartfile[]> { }): Promise<plugins.smartfile.Smartfile[]> {
return []; const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageDistTagArg => packageDistTagArg.name === 'latest');
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
};
}
}
return npmPackage.getFilesFromPackage(filePath, optionsArg);
} }
public async getPackageAsSmartfileVirtualDir(packageNameArg: string): Promise<plugins.smartfile.VirtualDirectory> { public async getPackageAsSmartfileVirtualDir(packageNameArg: string): Promise<plugins.smartfile.VirtualDirectory> {