smartnpm/ts/smartnpm.classes.npmpackage.ts
2021-04-19 12:55:11 +00:00

96 lines
2.4 KiB
TypeScript

import * as plugins from './smartnpm.plugins';
import { NpmRegistry } from './smartnpm.classes.npmregistry';
export class NpmPackage {
public static async createFromFullMetadata(
npmRegistryArg: NpmRegistry,
fullMetadata: plugins.packageJson.FullMetadata
) {
const npmPackage = new NpmPackage(npmRegistryArg);
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;
};
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 package to memory
*/
public async saveToMemory() {}
/**
* get file from package
*/
public async getFileFromPackage(filePath: string): Promise<plugins.smartfile.Smartfile> {
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile>();
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(
this.dist.tarball
);
const wantedFilePath = plugins.path.join('package', filePath);
const subscription = fileObservable.subscribe(
(fileArg) => {
if (fileArg.path === wantedFilePath) {
done.resolve(fileArg);
subscription.unsubscribe();
}
},
(err) => {
console.log(err);
},
() => {
subscription.unsubscribe();
}
);
return done.promise;
}
}