Files
smartnpm/ts/smartnpm.classes.npmpackage.ts
T

177 lines
5.2 KiB
TypeScript
Raw Permalink Normal View History

2022-04-04 23:21:49 +02:00
import * as plugins from './smartnpm.plugins.js';
import { NpmRegistry } from './smartnpm.classes.npmregistry.js';
import { PackageDisttag } from './smartnpm.classes.packagedisttag.js';
2023-07-25 18:14:51 +02:00
import { PackageVersion, type IVersionData } from './smartnpm.classes.packageversion.js';
2017-08-14 17:50:48 +02:00
2019-09-06 16:29:45 +02: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,
fullMetadataArg: Record<string, unknown>,
2021-04-26 08:30:02 +00:00
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 13:22:54 +02:00
return npmPackage;
}
// INSTANCE
public name: string | null = null;
public scope: string | null = null;
public version: string | null = null;
public allVersions: PackageVersion[] = [];
public allDistTags: PackageDisttag[] = [];
public description: string | null = null;
public keywords: string[] | null = null;
public date!: string;
public license!: string;
public links!: {
2018-09-01 16:40:42 +02:00
npm: string;
homepage: string;
repository: string;
bugs: string;
2019-09-06 13:22:54 +02:00
};
public author!: {
2018-09-01 16:40:42 +02:00
name: 'Lossless GmbH';
2019-09-06 13:22:54 +02:00
};
public publisher!: {
2018-09-01 16:40:42 +02:00
username: 'gitzone';
email: 'npm@git.zone';
2019-09-06 13:22:54 +02:00
};
public maintainers: unknown = null;
public dist!: {
2020-03-17 00:38:58 +00:00
integrity: string;
shasum: string;
tarball: string;
};
2019-09-06 13:22:54 +02:00
public score: {
2018-09-01 16:40:42 +02:00
final: number;
2017-08-15 17:03:21 +02:00
detail: {
2018-09-01 16:40:42 +02:00
quality: number;
popularity: number;
maintenance: number;
};
} | null = null;
public searchScore: number | null = 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) {
await plugins.smartarchive.SmartArchive.create().url(this.dist.tarball).extract(targetDir);
2021-04-19 12:55:11 +00:00
}
/**
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[] | null> {
let tarballUrl: string | undefined = this.dist?.tarball;
2021-05-05 10:54:31 +00:00
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 | undefined;
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
}
if (!targetVersionString) {
return null;
}
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;
}
if (!tarballUrl) {
return null;
2021-04-26 08:30:02 +00:00
}
2021-04-19 12:55:11 +00:00
const wantedFilePath = plugins.path.join('package', filePath);
const allFiles = await plugins.smartarchive.SmartArchive.create().url(tarballUrl).toSmartFiles();
const allMatchingFiles = allFiles.filter((fileArg) => {
if (returnOnFirstArg) {
return fileArg.path === wantedFilePath;
2021-04-19 12:55:11 +00:00
}
return fileArg.path.startsWith(wantedFilePath);
});
return returnOnFirstArg ? allMatchingFiles.slice(0, 1) : allMatchingFiles;
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
*/
2023-07-25 18:14:51 +02:00
public async getFileFromPackage(
2021-05-05 10:54:31 +00:00
filePath: string,
optionsArg?: {
distTag?: string;
version?: string;
}
): Promise<plugins.smartfile.SmartFile | null> {
2021-05-05 10:54:31 +00:00
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
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 | null {
// 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 17:50:48 +02:00
}