recoder-code
Version:
🚀 AI-powered development platform - Chat with 32+ models, build projects, automate workflows. Free models included!
311 lines (256 loc) • 8.03 kB
text/typescript
/**
* PackageVersion Entity
* Represents a specific version of a plugin package
*/
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, ManyToOne, Index } from 'typeorm';
import { Package } from './Package';
import { User } from './User';
export enum VersionStatus {
ACTIVE = 'active',
DEPRECATED = 'deprecated',
YANKED = 'yanked'
}
export class PackageVersion {
id!: string;
version!: string;
description?: string;
dependencies?: Record<string, string>;
dev_dependencies?: Record<string, string>;
peer_dependencies?: Record<string, string>;
optional_dependencies?: Record<string, string>;
bundled_dependencies?: string[];
engine_node?: string;
engines?: Record<string, string>;
os?: string[];
cpu?: string[];
scripts?: Record<string, string>;
bin?: Record<string, string>;
files?: string[];
main?: string;
module?: string;
types?: string;
exports?: any;
changelog?: string;
dist: {
tarball: string;
shasum: string;
integrity?: string;
size: number;
unpacked_size?: number;
};
status!: VersionStatus;
deprecation_message?: string;
yank_reason?: string;
download_count!: number;
security_scan: {
scan_date: string;
vulnerabilities: any[];
status: 'clean' | 'warning' | 'critical';
scanner: string;
version: string;
};
quality_analysis: {
size_score: number;
dependencies_score: number;
maintenance_score: number;
popularity_score: number;
quality_score: number;
details: any;
};
build_info: {
platform: string;
node_version: string;
npm_version: string;
build_time: string;
git_commit?: string;
};
npm_metadata: {
_id: string;
_nodeVersion: string;
_npmVersion: string;
_npmUser: any;
_hasShrinkwrap: boolean;
_from?: string;
_resolved?: string;
};
created_at!: Date;
published_at?: Date;
deprecated_at?: Date;
yanked_at?: Date;
// Relationships
package!: Package;
package_id!: string;
published_by?: User;
published_by_id?: string;
// Methods
addDownload() {
this.download_count += 1;
}
deprecate(message?: string) {
this.status = VersionStatus.DEPRECATED;
this.deprecated_at = new Date();
this.deprecation_message = message;
}
undeprecate() {
this.status = VersionStatus.ACTIVE;
this.deprecated_at = undefined;
this.deprecation_message = undefined;
}
yank(reason?: string) {
this.status = VersionStatus.YANKED;
this.yanked_at = new Date();
this.yank_reason = reason;
}
unyank() {
this.status = VersionStatus.ACTIVE;
this.yanked_at = undefined;
this.yank_reason = undefined;
}
updateSecurityScan(scan: PackageVersion['security_scan']) {
this.security_scan = {
...scan,
scan_date: new Date().toISOString()
};
}
updateQualityAnalysis(analysis: PackageVersion['quality_analysis']) {
this.quality_analysis = analysis;
}
getDependencyCount(): number {
const deps = this.dependencies || {};
const devDeps = this.dev_dependencies || {};
const peerDeps = this.peer_dependencies || {};
const optionalDeps = this.optional_dependencies || {};
return Object.keys(deps).length +
Object.keys(devDeps).length +
Object.keys(peerDeps).length +
Object.keys(optionalDeps).length;
}
getAllDependencies(): Record<string, string> {
return {
...this.dependencies,
...this.dev_dependencies,
...this.peer_dependencies,
...this.optional_dependencies
};
}
isPrerelease(): boolean {
return /\d+\.\d+\.\d+-.+/.test(this.version);
}
satisfiesNodeVersion(nodeVersion: string): boolean {
if (!this.engine_node) return true;
try {
// Simple semver check - in production, use a proper semver library
const required = this.engine_node.replace(/[^\d.]/g, '');
const current = nodeVersion.replace(/[^\d.]/g, '');
return current >= required;
} catch {
return true;
}
}
toNpmFormat(): any {
return {
name: this.package?.name,
version: this.version,
description: this.description,
main: this.main,
module: this.module,
types: this.types,
exports: this.exports,
scripts: this.scripts || {},
dependencies: this.dependencies || {},
devDependencies: this.dev_dependencies || {},
peerDependencies: this.peer_dependencies || {},
optionalDependencies: this.optional_dependencies || {},
bundledDependencies: this.bundled_dependencies || [],
engines: this.engines || {},
os: this.os,
cpu: this.cpu,
files: this.files,
bin: this.bin,
dist: this.dist,
_id: `${this.package?.name}@${this.version}`,
_nodeVersion: this.npm_metadata?._nodeVersion,
_npmVersion: this.npm_metadata?._npmVersion,
_npmUser: this.npm_metadata?._npmUser,
_hasShrinkwrap: this.npm_metadata?._hasShrinkwrap || false
};
}
toApiFormat(): any {
return {
id: this.id,
version: this.version,
description: this.description,
status: this.status,
dependencies: this.dependencies || {},
dev_dependencies: this.dev_dependencies || {},
peer_dependencies: this.peer_dependencies || {},
optional_dependencies: this.optional_dependencies || {},
engines: this.engines || {},
main: this.main,
module: this.module,
types: this.types,
exports: this.exports,
scripts: this.scripts || {},
files: this.files || [],
bin: this.bin || {},
dist: this.dist,
download_count: this.download_count,
security_scan: this.security_scan,
quality_analysis: this.quality_analysis,
build_info: this.build_info,
created_at: this.created_at,
published_at: this.published_at,
deprecated_at: this.deprecated_at,
yanked_at: this.yanked_at,
deprecation_message: this.deprecation_message,
yank_reason: this.yank_reason,
is_prerelease: this.isPrerelease(),
dependency_count: this.getDependencyCount()
};
}
}