smartnpm/ts/smartnpm.classes.npmpackage.ts

160 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-09-01 14:40:42 +00:00
import * as plugins from './smartnpm.plugins';
2020-03-08 19:34:36 +00:00
import { NpmRegistry } from './smartnpm.classes.npmregistry';
2021-04-26 08:30:02 +00:00
import { PackageDisttag } from './smartnpm.classes.packagedisttag';
import { PackageVersion, IVersionData } from './smartnpm.classes.packageversion';
2017-08-14 15:50:48 +00:00
2019-09-06 14:29:45 +00:00
export class NpmPackage {
2021-04-26 08:30:02 +00:00
public static async createFromFullMetadataAndVersionData(
2020-03-17 00:38:58 +00:00
npmRegistryArg: NpmRegistry,
2021-04-26 08:30:02 +00:00
fullMetadataArg: plugins.packageJson.FullMetadata,
versionsDataArg: {
name: string;
'dist-tags': { [key: string]: string };
versions: { [key: string]: IVersionData };
}
2020-03-17 00:38:58 +00:00
) {
const npmPackage = new NpmPackage(npmRegistryArg);
2021-04-26 08:30:02 +00:00
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);
}
2019-09-06 11:22:54 +00:00
return npmPackage;
}
// INSTANCE
public name: string = null;
public scope: string = null;
public version: string = null;
2021-04-26 08:30:02 +00:00
public allVersions: PackageVersion[];
public allDistTags: PackageDisttag[];
2019-09-06 11:22:54 +00:00
public description: string = null;
public keywords: string[] = null;
public date: string;
public license: string;
public links: {
2018-09-01 14:40:42 +00:00
npm: string;
homepage: string;
repository: string;
bugs: string;
2019-09-06 11:22:54 +00:00
};
public author: {
2018-09-01 14:40:42 +00:00
name: 'Lossless GmbH';
2019-09-06 11:22:54 +00:00
};
public publisher: {
2018-09-01 14:40:42 +00:00
username: 'gitzone';
email: 'npm@git.zone';
2019-09-06 11:22:54 +00:00
};
public maintainers: any = null;
2020-03-17 00:38:58 +00:00
public dist: {
integrity: string;
shasum: string;
tarball: string;
};
2019-09-06 11:22:54 +00:00
public score: {
2018-09-01 14:40:42 +00:00
final: number;
2017-08-15 15:03:21 +00:00
detail: {
2018-09-01 14:40:42 +00:00
quality: number;
popularity: number;
maintenance: number;
};
} = null;
2019-09-06 11:22:54 +00:00
public searchScore: number = null;
2020-03-17 00:38:58 +00:00
2021-04-19 12:55:11 +00:00
public npmRegistryRef: NpmRegistry;
2020-03-17 00:38:58 +00:00
constructor(npmRegistryArg: NpmRegistry) {
2021-04-19 12:55:11 +00:00
this.npmRegistryRef = npmRegistryArg;
2020-03-17 00:38:58 +00:00
}
/**
* saves the package to disk
*/
public async saveToDisk(targetDir: string) {
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
2021-04-19 12:55:11 +00:00
await smartarchiveInstance.extractArchiveFromUrlToFs(this.dist.tarball, targetDir);
}
/**
2021-04-26 08:30:02 +00:00
* saves the complete package to cache
2021-04-19 12:55:11 +00:00
*/
2021-04-26 08:30:02 +00:00
public async saveToCache() {}
2021-04-19 12:55:11 +00:00
/**
* get file from package
*/
2021-04-26 08:30:02 +00:00
public async getFileFromPackage(
filePath: string,
optionsArg?: {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
2021-04-19 12:55:11 +00:00
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile>();
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
2021-04-26 08:30:02 +00:00
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);
2021-04-29 16:59:58 +00:00
if (!bestMatchingVersion) {
return null;
}
2021-04-26 08:30:02 +00:00
tarballUrl = this.allVersions.find(
(packageVersion) => packageVersion.version === bestMatchingVersion
).dist.tarball;
}
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
2021-04-19 12:55:11 +00:00
const wantedFilePath = plugins.path.join('package', filePath);
const subscription = fileObservable.subscribe(
(fileArg) => {
if (fileArg.path === wantedFilePath) {
2021-04-26 08:30:02 +00:00
// lets care about caching
// lets resolve with the wanted file
2021-04-19 12:55:11 +00:00
done.resolve(fileArg);
subscription.unsubscribe();
}
},
(err) => {
console.log(err);
},
() => {
2021-04-29 16:59:58 +00:00
done.resolve(null);
2021-04-19 12:55:11 +00:00
subscription.unsubscribe();
}
);
return done.promise;
2020-03-17 00:38:58 +00:00
}
2021-04-26 08:30:02 +00:00
/**
* updates the package with information from the registry
*/
update() {}
2017-08-14 15:50:48 +00:00
}