fix(core): update

This commit is contained in:
2023-07-25 18:14:51 +02:00
parent e0a9e9702a
commit afa511550d
17 changed files with 1736 additions and 14631 deletions

View File

@ -2,7 +2,7 @@
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartnpm',
version: '2.0.3',
name: '@push.rocks/smartnpm',
version: '2.0.4',
description: 'interface with npm to retrieve package information'
}

View File

@ -1,7 +1,7 @@
import * as plugins from './smartnpm.plugins.js';
import { NpmRegistry } from './smartnpm.classes.npmregistry.js';
import { PackageDisttag } from './smartnpm.classes.packagedisttag.js';
import { PackageVersion, IVersionData } from './smartnpm.classes.packageversion.js';
import { PackageVersion, type IVersionData } from './smartnpm.classes.packageversion.js';
export class NpmPackage {
public static async createFromFullMetadataAndVersionData(
@ -139,7 +139,7 @@ export class NpmPackage {
// lets resolve with the wanted file
done.resolve([fileArg]);
subscription.unsubscribe();
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
} else if (!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
allMatchingFiles.push(fileArg);
}
},
@ -157,7 +157,7 @@ export class NpmPackage {
/**
* get files from package
*/
public async getFileFromPackage(
public async getFileFromPackage(
filePath: string,
optionsArg?: {
distTag?: string;

View File

@ -2,11 +2,11 @@ import * as plugins from './smartnpm.plugins.js';
import * as paths from './smartnpm.paths.js';
// interfaces
import { ISearchObject } from './smartnpm.interfaces.js';
import { type ISearchObject } from './smartnpm.interfaces.js';
// classes
import { NpmPackage } from './smartnpm.classes.npmpackage.js';
import { ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache.js';
import { type ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache.js';
export interface INpmRegistryConstructorOptions {
npmRegistryUrl?: string;
@ -33,18 +33,24 @@ export class NpmRegistry {
* @param packageName
*/
public async getPackageInfo(packageName: string): Promise<NpmPackage> {
const fullMetadata = await plugins.packageJson(packageName, {
const fullMetadata = await plugins
.packageJson.default(packageName, {
registryUrl: this.options.npmRegistryUrl,
fullMetadata: true,
})
.catch((err) => {
console.log(err);
return null;
});
const versionData = await plugins.packageJson.default(packageName, {
registryUrl: this.options.npmRegistryUrl,
fullMetadata: true,
}).catch(err => {
console.log(err);
return null;
allVersions: true,
});
const versionData = await plugins.packageJson(packageName, {
registryUrl: this.options.npmRegistryUrl,
allVersions: true
});
const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(this, fullMetadata, versionData as any);
const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(
this,
fullMetadata,
versionData as any
);
return npmPackage;
}
@ -62,30 +68,38 @@ export class NpmRegistry {
/**
* gets a file from a package as Smartfile
*/
public async getFileFromPackage(packageNameArg: string, filePathArg: string, optionsArg?: {
distTag?: string;
version?: string;
}): Promise<plugins.smartfile.Smartfile> {
public async getFileFromPackage(
packageNameArg: string,
filePathArg: string,
optionsArg?: {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
// lets create a cache descriptor
const cacheDescriptor: ICacheDescriptor = {
registryUrl: this.options.npmRegistryUrl,
packageName: packageNameArg,
filePath: filePathArg,
distTag: optionsArg?.distTag,
version: optionsArg?.version
version: optionsArg?.version,
};
// lets see if we have something cached
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(cacheDescriptor);
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(
cacheDescriptor
);
// lets handle both occasions
if (!cachedFile) {
const npmPackage = await this.getPackageInfo(packageNameArg);
if (!optionsArg?.version && !optionsArg?.distTag) {
const latestAvailable = npmPackage.allDistTags.find(packageArg => packageArg.name === 'latest');
const latestAvailable = npmPackage.allDistTags.find(
(packageArg) => packageArg.name === 'latest'
);
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
version: npmPackage.getBestMatchingVersion('*'),
};
}
}
@ -99,23 +113,31 @@ export class NpmRegistry {
}
}
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
distTag?: string;
version?: string;
}): Promise<plugins.smartfile.Smartfile[]> {
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');
const latestAvailable = npmPackage.allDistTags.find(
(packageDistTagArg) => packageDistTagArg.name === 'latest'
);
if (!latestAvailable) {
optionsArg = {
version: npmPackage.getBestMatchingVersion('*')
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> {
/**
* TODO: rewrite as memory only
*/

View File

@ -8,4 +8,4 @@ export class PackageDisttag {
this.name = nameArg;
this.targetVersion = targetVersionArg;
}
}
}

View File

@ -1,7 +1,6 @@
import { NpmRegistry } from './smartnpm.classes.npmregistry.js';
import * as plugins from './smartnpm.plugins.js';
export interface ICacheDescriptor {
registryUrl: string;
packageName: string;
@ -18,33 +17,47 @@ export class RegistryCache {
this.npmregistryRef = npmRegistryRefArg;
this.levelCache = new plugins.levelcache.LevelCache({
cacheId: encodeURIComponent(this.npmregistryRef.options.npmRegistryUrl),
});
}
public async getCachedFile (cacheDescriptorArg: ICacheDescriptor): Promise<plugins.smartfile.Smartfile> {
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg));
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 (cacheDescriptorArg: ICacheDescriptor, smartfileArg: plugins.smartfile.Smartfile) {
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: plugins.smarttime.getMilliSecondsFromUnits({hours: 1})
}));
await this.levelCache.storeCacheEntryByKey(
this.getCacheDescriptorAsString(cacheDescriptorArg),
new plugins.levelcache.CacheEntry({
contents: Buffer.from(smartfileArg.foldToJson()),
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})
}));
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}`;
return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${
cacheDescriptorArg.filePath
}//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
}
}
}

View File

@ -1,4 +1,7 @@
import * as plugins from './smartnpm.plugins.js';
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
export const packageDir = plugins.path.join(
plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../'
);
export const nogitDir = plugins.path.join(packageDir, '.nogit/');

View File

@ -4,19 +4,29 @@ import * as path from 'path';
export { path };
// @pushrocks scope
import * as consolecolor from '@pushrocks/consolecolor';
import * as levelcache from '@pushrocks/levelcache';
import * as smartarchive from '@pushrocks/smartarchive';
import * as smartfile from '@pushrocks/smartfile';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartversion from '@pushrocks/smartversion';
import * as smarttime from '@pushrocks/smarttime';
import * as consolecolor from '@push.rocks/consolecolor';
import * as levelcache from '@push.rocks/levelcache';
import * as smartarchive from '@push.rocks/smartarchive';
import * as smartfile from '@push.rocks/smartfile';
import * as smartpath from '@push.rocks/smartpath';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrequest from '@push.rocks/smartrequest';
import * as smartversion from '@push.rocks/smartversion';
import * as smarttime from '@push.rocks/smarttime';
export { consolecolor, levelcache, smartarchive, smartfile, smartpath, smartpromise, smartrequest, smartversion, smarttime };
export {
consolecolor,
levelcache,
smartarchive,
smartfile,
smartpath,
smartpromise,
smartrequest,
smartversion,
smarttime,
};
// third party scope
import packageJson from 'package-json';
import * as packageJson from 'package-json';
export { packageJson };