feat(registry): modernize npm registry file handling and package extraction APIs
This commit is contained in:
@@ -6,7 +6,7 @@ import { PackageVersion, type IVersionData } from './smartnpm.classes.packagever
|
||||
export class NpmPackage {
|
||||
public static async createFromFullMetadataAndVersionData(
|
||||
npmRegistryArg: NpmRegistry,
|
||||
fullMetadataArg: plugins.packageJson.FullMetadata,
|
||||
fullMetadataArg: Record<string, unknown>,
|
||||
versionsDataArg: {
|
||||
name: string;
|
||||
'dist-tags': { [key: string]: string };
|
||||
@@ -34,30 +34,30 @@ export class NpmPackage {
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public name: string = null;
|
||||
public scope: string = null;
|
||||
public version: string = null;
|
||||
public allVersions: PackageVersion[];
|
||||
public allDistTags: PackageDisttag[];
|
||||
public description: string = null;
|
||||
public keywords: string[] = null;
|
||||
public date: string;
|
||||
public license: string;
|
||||
public links: {
|
||||
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!: {
|
||||
npm: string;
|
||||
homepage: string;
|
||||
repository: string;
|
||||
bugs: string;
|
||||
};
|
||||
public author: {
|
||||
public author!: {
|
||||
name: 'Lossless GmbH';
|
||||
};
|
||||
public publisher: {
|
||||
public publisher!: {
|
||||
username: 'gitzone';
|
||||
email: 'npm@git.zone';
|
||||
};
|
||||
public maintainers: any = null;
|
||||
public dist: {
|
||||
public maintainers: unknown = null;
|
||||
public dist!: {
|
||||
integrity: string;
|
||||
shasum: string;
|
||||
tarball: string;
|
||||
@@ -69,8 +69,8 @@ export class NpmPackage {
|
||||
popularity: number;
|
||||
maintenance: number;
|
||||
};
|
||||
} = null;
|
||||
public searchScore: number = null;
|
||||
} | null = null;
|
||||
public searchScore: number | null = null;
|
||||
|
||||
public npmRegistryRef: NpmRegistry;
|
||||
constructor(npmRegistryArg: NpmRegistry) {
|
||||
@@ -81,9 +81,7 @@ export class NpmPackage {
|
||||
* saves the package to disk
|
||||
*/
|
||||
public async saveToDisk(targetDir: string) {
|
||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(this.dist.tarball);
|
||||
await archive.exportToFs(targetDir);
|
||||
await plugins.smartarchive.SmartArchive.create().url(this.dist.tarball).extract(targetDir);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,17 +97,15 @@ export class NpmPackage {
|
||||
optionsArg: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
},
|
||||
} = {},
|
||||
returnOnFirstArg = false
|
||||
): Promise<plugins.smartfile.SmartFile[]> {
|
||||
const done = plugins.smartpromise.defer<plugins.smartfile.SmartFile[]>();
|
||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||
let tarballUrl = this.dist?.tarball;
|
||||
): Promise<plugins.smartfile.SmartFile[] | null> {
|
||||
let tarballUrl: string | undefined = this.dist?.tarball;
|
||||
if (optionsArg?.version || optionsArg?.distTag) {
|
||||
if (optionsArg.distTag && optionsArg.version) {
|
||||
throw new Error('Please either specify version OR disttag, not both.');
|
||||
}
|
||||
let targetVersionString: string;
|
||||
let targetVersionString: string | undefined;
|
||||
if (optionsArg.distTag) {
|
||||
const targetDistTag = this.allDistTags.find((distTag) => {
|
||||
return distTag.name === optionsArg.distTag;
|
||||
@@ -120,6 +116,9 @@ export class NpmPackage {
|
||||
} else {
|
||||
targetVersionString = optionsArg.version;
|
||||
}
|
||||
if (!targetVersionString) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// lets find the best matching release
|
||||
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
|
||||
@@ -128,59 +127,20 @@ export class NpmPackage {
|
||||
}
|
||||
tarballUrl = this.allVersions.find(
|
||||
(packageVersion) => packageVersion.version === bestMatchingVersion
|
||||
).dist.tarball;
|
||||
)?.dist.tarball;
|
||||
}
|
||||
if (!tarballUrl) {
|
||||
return null;
|
||||
}
|
||||
const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(tarballUrl);
|
||||
const streamOfFiles = await archive.exportToStreamOfStreamFiles();
|
||||
const wantedFilePath = plugins.path.join('package', filePath);
|
||||
|
||||
// Collect all stream files first
|
||||
const streamFileList: any[] = [];
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
streamOfFiles.on('data', (streamFile) => {
|
||||
streamFileList.push(streamFile);
|
||||
});
|
||||
|
||||
streamOfFiles.on('end', resolve);
|
||||
streamOfFiles.on('error', reject);
|
||||
});
|
||||
|
||||
// Now process the collected files
|
||||
const allMatchingFiles: plugins.smartfile.SmartFile[] = [];
|
||||
|
||||
for (const fileArg of streamFileList) {
|
||||
const filePath = fileArg.relativeFilePath || fileArg.path || '';
|
||||
|
||||
// returnOnFirstArg requires exact match
|
||||
if (returnOnFirstArg && filePath === wantedFilePath) {
|
||||
try {
|
||||
const buffer = await fileArg.getContentAsBuffer();
|
||||
const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
|
||||
filePath,
|
||||
buffer
|
||||
);
|
||||
done.resolve([smartFile]);
|
||||
return done.promise;
|
||||
} catch (error) {
|
||||
console.error('Error processing file:', error);
|
||||
}
|
||||
} else if (!returnOnFirstArg && filePath.startsWith(wantedFilePath)) {
|
||||
try {
|
||||
const buffer = await fileArg.getContentAsBuffer();
|
||||
const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
|
||||
filePath,
|
||||
buffer
|
||||
);
|
||||
allMatchingFiles.push(smartFile);
|
||||
} catch (error) {
|
||||
console.error('Error processing file:', error);
|
||||
}
|
||||
const allFiles = await plugins.smartarchive.SmartArchive.create().url(tarballUrl).toSmartFiles();
|
||||
const allMatchingFiles = allFiles.filter((fileArg) => {
|
||||
if (returnOnFirstArg) {
|
||||
return fileArg.path === wantedFilePath;
|
||||
}
|
||||
}
|
||||
|
||||
done.resolve(allMatchingFiles);
|
||||
return done.promise;
|
||||
return fileArg.path.startsWith(wantedFilePath);
|
||||
});
|
||||
return returnOnFirstArg ? allMatchingFiles.slice(0, 1) : allMatchingFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,9 +152,9 @@ export class NpmPackage {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.SmartFile> {
|
||||
): Promise<plugins.smartfile.SmartFile | null> {
|
||||
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
|
||||
return result[0] || null;
|
||||
return result?.[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,7 +163,7 @@ export class NpmPackage {
|
||||
update() {}
|
||||
|
||||
/** */
|
||||
public getBestMatchingVersion(versionArg: string): string {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user