import * as plugins from './smartnpm.plugins'; import { NpmRegistry } from './smartnpm.classes.npmregistry'; import { PackageDisttag } from './smartnpm.classes.packagedisttag'; import { PackageVersion, IVersionData } from './smartnpm.classes.packageversion'; export class NpmPackage { public static async createFromFullMetadataAndVersionData( npmRegistryArg: NpmRegistry, fullMetadataArg: plugins.packageJson.FullMetadata, versionsDataArg: { name: string; 'dist-tags': { [key: string]: string }; versions: { [key: string]: IVersionData }; } ) { const npmPackage = new NpmPackage(npmRegistryArg); Object.assign(npmPackage, fullMetadataArg); npmPackage.allVersions = []; npmPackage.allDistTags = []; for (const versionArg of Object.keys(versionsDataArg.versions)) { const packageVersion = PackageVersion.createFromVersionData( versionsDataArg.versions[versionArg] ); npmPackage.allVersions.push(packageVersion); } for (const distTagArg of Object.keys(versionsDataArg['dist-tags'])) { const packageDistTag = new PackageDisttag( distTagArg, versionsDataArg['dist-tags'][distTagArg] ); npmPackage.allDistTags.push(packageDistTag); } return npmPackage; } // INSTANCE public name: string = null; public scope: string = null; public version: string = null; public allVersions: PackageVersion[]; public allDistTags: PackageDisttag[]; public description: string = null; public keywords: string[] = null; public date: string; public license: string; public links: { npm: string; homepage: string; repository: string; bugs: string; }; public author: { name: 'Lossless GmbH'; }; public publisher: { username: 'gitzone'; email: 'npm@git.zone'; }; public maintainers: any = null; public dist: { integrity: string; shasum: string; tarball: string; }; public score: { final: number; detail: { quality: number; popularity: number; maintenance: number; }; } = null; public searchScore: number = null; public npmRegistryRef: NpmRegistry; constructor(npmRegistryArg: NpmRegistry) { this.npmRegistryRef = npmRegistryArg; } /** * saves the package to disk */ public async saveToDisk(targetDir: string) { const smartarchiveInstance = new plugins.smartarchive.SmartArchive(); await smartarchiveInstance.extractArchiveFromUrlToFs(this.dist.tarball, targetDir); } /** * saves the complete package to cache */ public async saveToCache() {} /** * get file from package */ public async getFileFromPackage( filePath: string, optionsArg?: { distTag?: string; version?: string; } ): Promise { const done = plugins.smartpromise.defer(); const smartarchiveInstance = new plugins.smartarchive.SmartArchive(); let tarballUrl = this.dist.tarball; if (optionsArg && (optionsArg.version || optionsArg.distTag)) { if (optionsArg.distTag && optionsArg.version) { throw new Error('Please either sepcify version OR disttag, not both.'); } let targetVersion: plugins.smartversion.SmartVersion; if (optionsArg.distTag) { const targetDistTag = this.allDistTags.find((distTag) => { return distTag.name === optionsArg.distTag; }); if (targetDistTag) { targetVersion = new plugins.smartversion.SmartVersion(targetDistTag.targetVersion); } } else { const smartversion = plugins.smartversion.SmartVersion.fromFuzzyString(optionsArg.version); } // lets find the best matching release const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version); const bestMatchingVersion = targetVersion.getBestMatch(versionStrings); if (!bestMatchingVersion) { return null; } tarballUrl = this.allVersions.find( (packageVersion) => packageVersion.version === bestMatchingVersion ).dist.tarball; } const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl); const wantedFilePath = plugins.path.join('package', filePath); const subscription = fileObservable.subscribe( (fileArg) => { if (fileArg.path === wantedFilePath) { // lets care about caching // lets resolve with the wanted file done.resolve(fileArg); subscription.unsubscribe(); } }, (err) => { console.log(err); }, () => { done.resolve(null); subscription.unsubscribe(); } ); return done.promise; } /** * updates the package with information from the registry */ update() {} }