Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
634d1c3570 | |||
cd8c9bcdaa | |||
f6d3d0987b | |||
60a1de38ce | |||
0ef6d0ccb2 | |||
8e538fd84d | |||
6745115db7 | |||
5ebce389d3 | |||
b4100688ac | |||
77d515d915 | |||
aa71105b2d | |||
e0ccb6c076 | |||
af0d95f327 | |||
d0ac8f1418 | |||
62f8106fc6 | |||
e87b8c994b | |||
694ba7be25 | |||
952399c26e |
1118
package-lock.json
generated
1118
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartnpm",
|
||||
"version": "1.0.26",
|
||||
"version": "1.0.36",
|
||||
"private": false,
|
||||
"description": "interface with npm to retrieve package information",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -16,17 +16,18 @@
|
||||
"@gitzone/tsrun": "^1.2.12",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^15.0.1",
|
||||
"@types/node": "^15.0.2",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/consolecolor": "^2.0.1",
|
||||
"@pushrocks/levelcache": "^1.0.9",
|
||||
"@pushrocks/levelcache": "^1.0.10",
|
||||
"@pushrocks/smartarchive": "^2.0.4",
|
||||
"@pushrocks/smartfile": "^8.0.10",
|
||||
"@pushrocks/smartpromise": "^3.1.5",
|
||||
"@pushrocks/smartrequest": "^1.1.51",
|
||||
"@pushrocks/smarttime": "^3.0.38",
|
||||
"@pushrocks/smartversion": "^2.0.7",
|
||||
"package-json": "^6.5.0"
|
||||
},
|
||||
|
15
test/test.ts
15
test/test.ts
@ -32,16 +32,25 @@ tap.test('should create a verdaccio registry', async () => {
|
||||
|
||||
tap.test('should get package from verdaccio', async () => {
|
||||
const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate');
|
||||
console.log(npmPackage);
|
||||
expect(npmPackage.license).to.equal('MIT');
|
||||
});
|
||||
|
||||
tap.test('should get a specific file from a package', async () => {
|
||||
const bundleFile = await verdaccioRegistry.getFileFromPackage(
|
||||
const wantedFile = await verdaccioRegistry.getFileFromPackage(
|
||||
'@pushrocks/websetup',
|
||||
'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();
|
||||
|
@ -91,37 +91,37 @@ export class NpmPackage {
|
||||
public async saveToCache() {}
|
||||
|
||||
/**
|
||||
* get file from package
|
||||
* get files from package
|
||||
*/
|
||||
public async getFileFromPackage(
|
||||
public async getFilesFromPackage(
|
||||
filePath: string,
|
||||
optionsArg?: {
|
||||
optionsArg: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.Smartfile> {
|
||||
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile>();
|
||||
},
|
||||
returnOnFirstArg = false
|
||||
): Promise<plugins.smartfile.Smartfile[]> {
|
||||
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
|
||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||
let tarballUrl = this.dist.tarball;
|
||||
if (optionsArg && (optionsArg.version || optionsArg.distTag)) {
|
||||
let tarballUrl = this.dist?.tarball;
|
||||
if (optionsArg?.version || optionsArg?.distTag) {
|
||||
if (optionsArg.distTag && optionsArg.version) {
|
||||
throw new Error('Please either sepcify 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) {
|
||||
const targetDistTag = this.allDistTags.find((distTag) => {
|
||||
return distTag.name === optionsArg.distTag;
|
||||
});
|
||||
if (targetDistTag) {
|
||||
targetVersion = new plugins.smartversion.SmartVersion(targetDistTag.targetVersion);
|
||||
targetVersionString = targetDistTag.targetVersion;
|
||||
}
|
||||
} else {
|
||||
const smartversion = plugins.smartversion.SmartVersion.fromFuzzyString(optionsArg.version);
|
||||
targetVersionString = optionsArg.version;
|
||||
}
|
||||
|
||||
// lets find the best matching release
|
||||
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
|
||||
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
|
||||
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
|
||||
if (!bestMatchingVersion) {
|
||||
return null;
|
||||
}
|
||||
@ -131,29 +131,57 @@ export class NpmPackage {
|
||||
}
|
||||
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
|
||||
const wantedFilePath = plugins.path.join('package', filePath);
|
||||
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
|
||||
const subscription = fileObservable.subscribe(
|
||||
(fileArg) => {
|
||||
if (fileArg.path === wantedFilePath) {
|
||||
// lets care about caching
|
||||
|
||||
// returnOnFirstArg requires exact match
|
||||
if (returnOnFirstArg && fileArg.path === wantedFilePath) {
|
||||
// lets resolve with the wanted file
|
||||
done.resolve(fileArg);
|
||||
done.resolve([fileArg]);
|
||||
subscription.unsubscribe();
|
||||
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
|
||||
allMatchingFiles.push(fileArg);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
console.log(err);
|
||||
},
|
||||
() => {
|
||||
done.resolve(null);
|
||||
done.resolve(allMatchingFiles);
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
);
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* get files from package
|
||||
*/
|
||||
public async getFileFromPackage(
|
||||
filePath: string,
|
||||
optionsArg?: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.Smartfile> {
|
||||
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* updates the package with information from the registry
|
||||
*/
|
||||
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;
|
||||
}
|
||||
return bestMatchingVersion;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import { ISearchObject } from './smartnpm.interfaces';
|
||||
|
||||
// classes
|
||||
import { NpmPackage } from './smartnpm.classes.npmpackage';
|
||||
import { RegistryCache } from './smartnpm.classes.registrycache';
|
||||
import { ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache';
|
||||
|
||||
export interface INpmRegistryConstructorOptions {
|
||||
npmRegistryUrl?: string;
|
||||
@ -36,6 +36,9 @@ export class NpmRegistry {
|
||||
const fullMetadata = await plugins.packageJson(packageName, {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
fullMetadata: true,
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
return null;
|
||||
});
|
||||
const versionData = await plugins.packageJson(packageName, {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
@ -59,22 +62,57 @@ export class NpmRegistry {
|
||||
/**
|
||||
* gets a file from a package as Smartfile
|
||||
*/
|
||||
public async getFileFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
|
||||
public async getFileFromPackage(packageNameArg: string, filePathArg: string, optionsArg?: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}): Promise<plugins.smartfile.Smartfile> {
|
||||
const fileId = `${this.options.npmRegistryUrl}//+//${packageNameArg}//+//${filePath}//+//${optionsArg?.distTag || optionsArg?.version}`;
|
||||
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(fileId);
|
||||
// lets create a cache descriptor
|
||||
const cacheDescriptor: ICacheDescriptor = {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
packageName: packageNameArg,
|
||||
filePath: filePathArg,
|
||||
distTag: optionsArg?.distTag,
|
||||
version: optionsArg?.version
|
||||
};
|
||||
|
||||
// lets see if we have something cached
|
||||
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(cacheDescriptor);
|
||||
|
||||
// lets handle both occasions
|
||||
if (!cachedFile) {
|
||||
const npmPackage = await this.getPackageInfo(packageNameArg);
|
||||
const fileResult = await npmPackage.getFileFromPackage(filePath, optionsArg);
|
||||
this.registryCache.cacheSmartFile(fileId, fileResult);
|
||||
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(filePathArg, optionsArg);
|
||||
this.registryCache.cacheSmartFile(cacheDescriptor, fileResult);
|
||||
return fileResult;
|
||||
} else {
|
||||
return cachedFile;
|
||||
}
|
||||
}
|
||||
|
||||
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}): Promise<plugins.smartfile.Smartfile[]> {
|
||||
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> {
|
||||
/**
|
||||
* TODO: rewrite as memory only
|
||||
|
@ -1,6 +1,15 @@
|
||||
import { NpmRegistry } from './smartnpm.classes.npmregistry';
|
||||
import * as plugins from './smartnpm.plugins';
|
||||
|
||||
|
||||
export interface ICacheDescriptor {
|
||||
registryUrl: string;
|
||||
packageName: string;
|
||||
filePath: string;
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
|
||||
export class RegistryCache {
|
||||
npmregistryRef: NpmRegistry;
|
||||
public levelCache: plugins.levelcache.LevelCache;
|
||||
@ -13,18 +22,29 @@ export class RegistryCache {
|
||||
});
|
||||
}
|
||||
|
||||
public async getCachedFile (fileId: string): Promise<plugins.smartfile.Smartfile> {
|
||||
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(fileId);
|
||||
public async getCachedFile (cacheDescriptorArg: ICacheDescriptor): Promise<plugins.smartfile.Smartfile> {
|
||||
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg));
|
||||
if (cacheEntry) {
|
||||
return plugins.smartfile.Smartfile.fromFoldedJson(cacheEntry.contents.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async cacheSmartFile (fileIdArg: string, smartfileArg: plugins.smartfile.Smartfile) {
|
||||
await this.levelCache.storeCacheEntryByKey(fileIdArg, new plugins.levelcache.CacheEntry({
|
||||
public async cacheSmartFile (cacheDescriptorArg: ICacheDescriptor, smartfileArg: plugins.smartfile.Smartfile) {
|
||||
if (smartfileArg && cacheDescriptorArg.version) {
|
||||
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
|
||||
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||
ttl: 60000
|
||||
ttl: plugins.smarttime.getMilliSecondsFromUnits({hours: 1})
|
||||
}));
|
||||
} else {
|
||||
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
|
||||
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||
ttl: plugins.smarttime.getMilliSecondsFromUnits({minutes: 1})
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public getCacheDescriptorAsString(cacheDescriptorArg?: ICacheDescriptor) {
|
||||
return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${cacheDescriptorArg.filePath}//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
|
||||
}
|
||||
}
|
@ -11,8 +11,9 @@ import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartversion from '@pushrocks/smartversion';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { consolecolor, levelcache, smartarchive, smartfile, smartpromise, smartrequest, smartversion };
|
||||
export { consolecolor, levelcache, smartarchive, smartfile, smartpromise, smartrequest, smartversion, smarttime };
|
||||
|
||||
// third party scope
|
||||
import packageJson from 'package-json';
|
||||
|
Reference in New Issue
Block a user