From 1d5e94244b821de2a77da997b6e49f0a4de1e9ac Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 6 Sep 2019 13:22:54 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 10 +++---- ts/smartnpm.classes.npmpackage.ts | 48 +++++++++++++++--------------- ts/smartnpm.classes.npmregistry.ts | 13 ++++---- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/test/test.ts b/test/test.ts index 5e93a34..0dd3fa5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -11,15 +11,15 @@ tap.test('should create valid instances', async () => { npmRegistry = new smartnpm.NpmRegistry(); expect(npmRegistry).to.be.instanceof(smartnpm.NpmRegistry); - testPackage = new smartnpm.NpmPackage({}); + testPackage = new smartnpm.NpmPackage(); expect(testPackage).to.be.instanceof(smartnpm.NpmPackage); }); tap.test('should produce a valid search string and this return npmts', async () => { const packages = await npmRegistry.search({ - name: 'npmts' + name: '@pushrocks/smartupdate' }); - expect(packages[0].name).to.equal('npmts'); + expect(packages[0].name).to.equal('@pushrocks/smartupdate'); }); // lets test things with the verdaccio registry @@ -31,8 +31,8 @@ tap.test('should create a verdaccio registry', async () => { }); tap.test('should get package from verdaccui', async () => { - const packageInfo = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate'); - expect(packageInfo.license).to.equal('MIT'); + const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate'); + expect(npmPackage.license).to.equal('MIT'); }); tap.start(); diff --git a/ts/smartnpm.classes.npmpackage.ts b/ts/smartnpm.classes.npmpackage.ts index a449736..8a57f41 100644 --- a/ts/smartnpm.classes.npmpackage.ts +++ b/ts/smartnpm.classes.npmpackage.ts @@ -1,27 +1,35 @@ import * as plugins from './smartnpm.plugins'; -export class NpmPackage { - name: string = null; - scope: string = null; - version: string = null; - description: string = null; - keywords: string[] = null; - date: '2017-08-02T11:22:49.144Z'; - links: { +export class NpmPackage { + public static async createFromFullMetadata(fullMetadata: plugins.packageJson.FullMetadata) { + const npmPackage = new NpmPackage(); + Object.assign(npmPackage, fullMetadata); + return npmPackage; + } + + // INSTANCE + public name: string = null; + public scope: string = null; + public version: string = null; + public description: string = null; + public keywords: string[] = null; + public date: string; + public license: string; + public links: { npm: string; homepage: string; repository: string; bugs: string; - } = null; - author: { + }; + public author: { name: 'Lossless GmbH'; - } = null; - publisher: { + }; + public publisher: { username: 'gitzone'; email: 'npm@git.zone'; - } = null; - maintainers: any = null; - score: { + }; + public maintainers: any = null; + public score: { final: number; detail: { quality: number; @@ -29,13 +37,5 @@ export class NpmPackage { maintenance: number; }; } = null; - searchScore: number = null; - - constructor(descriptionArg) { - for (let key in descriptionArg) { - if (this[key] === null) { - this[key] = descriptionArg[key]; - } - } - } + public searchScore: number = null; } diff --git a/ts/smartnpm.classes.npmregistry.ts b/ts/smartnpm.classes.npmregistry.ts index 0ff7661..596d90a 100644 --- a/ts/smartnpm.classes.npmregistry.ts +++ b/ts/smartnpm.classes.npmregistry.ts @@ -25,12 +25,13 @@ export class NpmRegistry { }; } - public async getPackageInfo(packageName: string) { - const result = await plugins.packageJson(packageName, { + public async getPackageInfo(packageName: string): Promise { + const fullMetadata = await plugins.packageJson(packageName, { registryUrl: this.options.npmRegistryUrl, fullMetadata: true }); - return result; + const npmPackage = await NpmPackage.createFromFullMetadata(fullMetadata); + return npmPackage; } public async search(searchObjectArg: ISearchObject) { @@ -121,9 +122,9 @@ export class NpmRegistry { return packageArray; } - for (const packageArg of body.results) { - const localPackage = new NpmPackage(packageArg.package); - packageArray.push(localPackage); + for (const packageSearchInfoArg of body.results) { + const npmPackage = await this.getPackageInfo(packageSearchInfoArg.package.name); + packageArray.push(npmPackage); } return packageArray;