smartnpm/ts/smartnpm.classes.npmpackage.ts

188 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-04-04 21:21:49 +00:00
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';
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
/**
2021-05-05 10:54:31 +00:00
* get files from package
2021-04-19 12:55:11 +00:00
*/
2021-05-05 10:54:31 +00:00
public async getFilesFromPackage(
2021-04-26 08:30:02 +00:00
filePath: string,
2021-05-05 10:54:31 +00:00
optionsArg: {
2021-04-26 08:30:02 +00:00
distTag?: string;
version?: string;
2021-05-05 10:54:31 +00:00
},
returnOnFirstArg = false
): Promise<plugins.smartfile.Smartfile[]> {
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
2021-04-19 12:55:11 +00:00
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
2021-05-05 10:54:31 +00:00
let tarballUrl = this.dist?.tarball;
if (optionsArg?.version || optionsArg?.distTag) {
2021-04-26 08:30:02 +00:00
if (optionsArg.distTag && optionsArg.version) {
2021-05-05 10:54:31 +00:00
throw new Error('Please either specify version OR disttag, not both.');
2021-04-26 08:30:02 +00:00
}
let targetVersionString: string;
2021-04-26 08:30:02 +00:00
if (optionsArg.distTag) {
const targetDistTag = this.allDistTags.find((distTag) => {
return distTag.name === optionsArg.distTag;
});
if (targetDistTag) {
targetVersionString = targetDistTag.targetVersion;
2021-04-26 08:30:02 +00:00
}
} else {
targetVersionString = optionsArg.version;
2021-04-26 08:30:02 +00:00
}
// lets find the best matching release
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
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);
2021-05-05 10:54:31 +00:00
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
2021-04-19 12:55:11 +00:00
const subscription = fileObservable.subscribe(
(fileArg) => {
2021-05-05 10:54:31 +00:00
// returnOnFirstArg requires exact match
if (returnOnFirstArg && fileArg.path === wantedFilePath) {
2021-04-26 08:30:02 +00:00
// lets resolve with the wanted file
2021-05-05 10:54:31 +00:00
done.resolve([fileArg]);
2021-04-19 12:55:11 +00:00
subscription.unsubscribe();
2021-05-05 11:56:12 +00:00
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
2021-05-05 10:54:31 +00:00
allMatchingFiles.push(fileArg);
2021-04-19 12:55:11 +00:00
}
},
(err) => {
console.log(err);
},
() => {
2021-05-05 10:54:31 +00:00
done.resolve(allMatchingFiles);
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
2021-05-05 10:54:31 +00:00
/**
* get files from package
*/
public async getFileFromPackage(
filePath: string,
optionsArg?: {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.Smartfile> {
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
2021-06-07 11:59:56 +00:00
return result[0] || null;
2021-05-05 10:54:31 +00:00
}
2021-04-26 08:30:02 +00:00
/**
* updates the package with information from the registry
*/
update() {}
/** */
public getBestMatchingVersion(versionArg: string): string {
// lets find the best matching release
const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
if (!bestMatchingVersion) {
return null;
}
2021-05-06 10:34:44 +00:00
return bestMatchingVersion;
}
2017-08-14 15:50:48 +00:00
}