Skip to content

Commit

Permalink
✨ feat(core): add core feats
Browse files Browse the repository at this point in the history
  • Loading branch information
angelespejo committed Jan 13, 2025
1 parent 7c172a8 commit 8f5d2bf
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 135 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
},
"devDependencies": {
"@changesets/changelog-github": "0.5.0",
"@dovenv/core": "1.2.0",
"@dovenv/theme-pigeonposse": "1.2.0",
"@dovenv/core": "1.1.5",
"@dovenv/theme-pigeonposse": "1.1.5",
"@playwright/test": "1.49.1",
"@types/node": "22.10.5",
"binarium": "2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ${bold( 'Usage:' )} ${cyan( name )} ${green( '<command>' )} ${yellow( '[...flags
${bold( 'Options:' )}
${yellow( '-i, --input' )} ${dim( 'Library input. Accepted:' )}
${yellow( '-i, --input' )} ${dim( 'Library input. Accepted:' )}
${dim( ' - libraryID (name@version)' )}
${dim( ' - path (dir to project / file to package.json)' )}
${dim( ' - URL (to https://www.npmjs.com/package/${name} or link to package.json)' )}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const run = async () => {
const size = new Sizium( FLAGS.INPUT )
const data = await size.get()

if ( FLAGS.RES === RES_TYPE.SIZE ) console.log( `${data.size} bytes | ${data.size / 1000000} megabytes` )
else if ( FLAGS.RES === RES_TYPE.INFO ) console.log( `Name: ${data.id}\nPackages: ${data.packageNum}\nSize: ${data.size} bytes | ${data.size / 1000000} megabytes` )
if ( FLAGS.RES === RES_TYPE.SIZE ) console.log( `${data.sizeKB}kb | ${data.sizeMB}mb` )
else if ( FLAGS.RES === RES_TYPE.INFO ) console.log( `Name: ${data.id}\nPackages: ${data.packageNum}\nSize: ${data.sizeKB}kb | ${data.sizeMB}mb` )
else if ( FLAGS.RES === RES_TYPE.JSON ) console.log( JSON.stringify( data ) )
else console.dir( data, { depth: Infinity } )

Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/_shared/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const normalizeRepositoryUrl = ( url?: string ): string | undefined => {

if ( !url ) return undefined

try {

const parsedUrl = new URL( url )
parsedUrl.protocol = 'https:' // force https

if ( parsedUrl.pathname.endsWith( '.git' ) )
parsedUrl.pathname = parsedUrl.pathname.slice( 0, -4 )

return parsedUrl.toString()

}
catch ( _error ) {

return undefined

}

}
9 changes: 9 additions & 0 deletions packages/core/src/search/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ export const ERROR_ID = {
GETTING_LOCAL_DATA : 'GETTING_LOCAL_DATA',
} as const

export const LIFE_CYCLE_SCRIPTS = [
'preinstall',
'install',
'postinstall',
'prepublish',
'preprepare',
'prepare',
'postprepare',
] as const
30 changes: 6 additions & 24 deletions packages/core/src/search/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,28 @@ import type {
*/
export class SiziumLocal extends PackageSuper {

constructor( public packagePath: string ) {

super()

}

async #getPackage(): Promise<PackageJSON> {

try {

if ( isUrl( this.packagePath ) ) {
if ( isUrl( this.input ) ) {

const response = await fetch( this.packagePath )
const response = await fetch( this.input )
if ( !response.ok )
throw new Error( `Failed to fetch package file from URL: ${response.statusText}` )

const content = await response.text()
return JSON.parse( content )

}
else if ( isJsonString( this.packagePath ) ) {
else if ( isJsonString( this.input ) ) {

return JSON.parse( this.packagePath )
return JSON.parse( this.input )

}
else {

let filePath = path.resolve( this.packagePath )
let filePath = path.resolve( this.input )
const stats = await stat( filePath )

if ( stats.isDirectory() ) filePath = path.join( filePath, 'package.json' )
Expand Down Expand Up @@ -93,19 +87,7 @@ export class SiziumLocal extends PackageSuper {
const mainPackage = await this.getPkgData( packageData, 0 )
const allPackages = await this.getPackagesData( mainPackage )

const totalSize = allPackages.reduce( ( sum, pkg ) => {

const size = pkg.unpackedSize ?? 0
return sum + size

}, 0 )

return {
id : packageData.name,
packageNum : allPackages.length,
size : totalSize,
packages : allPackages,
}
return this.getMainPkgData( allPackages )

}

Expand Down
23 changes: 2 additions & 21 deletions packages/core/src/search/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ import type { SiziumResponse } from './types'
*/
export class SiziumRegistry extends PackageSuper {

constructor(
public name: string,
) {

super()

}

#parseName( input: string ) {

try {
Expand Down Expand Up @@ -60,22 +52,11 @@ export class SiziumRegistry extends PackageSuper {

async get(): Promise<SiziumResponse> {

const data = this.#parseName( this.name )
const data = this.#parseName( this.input )
const mainPackage = await this.getRegistryData( data.name, data.version, 0 )
const allPackages = await this.getPackagesData( mainPackage )
const totalSize = allPackages.reduce( ( sum, pkg ) => {

const size = pkg.unpackedSize ?? 0
return sum + size

}, 0 )

return {
id : this.name,
packageNum : allPackages.length,
size : totalSize,
packages : allPackages,
}
return this.getMainPkgData( allPackages )

}

Expand Down
98 changes: 70 additions & 28 deletions packages/core/src/search/super.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable @stylistic/object-curly-newline */
import semver from 'semver'

import { ERROR_ID } from './const'
import { TypedError } from '../_shared/error'
import {
ERROR_ID,
LIFE_CYCLE_SCRIPTS,
} from './const'
import { TypedError } from '../_shared/error'
import { normalizeRepositoryUrl } from '../_shared/url'

import type {
PackageJSON,
Expand All @@ -20,32 +25,55 @@ export class PackageSuper {
ERROR_ID = ERROR_ID
Error = SiziumError

constructor(
public input: string,
public opts?: {
/** Skip error on package dependence and return undefined */
skipError : boolean
},
) {

}

private processedPackages : Set<string> = new Set()
protected LIFE_CYCLE_SCRIPTS = LIFE_CYCLE_SCRIPTS

protected getPkgData( data: PackageJSON, level = 0, unpackedSize?: number, installedBy?: string | string[] ): PackageInfo {
protected getMainPkgData( allPackages: PackageInfo[] ) {

const normalizeRepositoryUrl = ( url?: string ): string | undefined => {
const totalSize = allPackages.reduce( ( sum, pkg ) => {

if ( !url ) return undefined
const size = pkg.unpackedSize ?? 0
return sum + size

try {
}, 0 )

const parsedUrl = new URL( url )
parsedUrl.protocol = 'https:' // force https
return {
id : allPackages[0].name,
packageNum : allPackages.length,
size : totalSize,
sizeKB : totalSize / 1000,
sizeMB : totalSize / 1000000,
packages : allPackages,
}

if ( parsedUrl.pathname.endsWith( '.git' ) )
parsedUrl.pathname = parsedUrl.pathname.slice( 0, -4 )
}

return parsedUrl.toString()
protected getPkgData( data: PackageJSON, level = 0, unpackedSize?: number, installedBy?: string | string[] ): PackageInfo {

}
catch ( _error ) {
const lcScripts = {} as NonNullable<PackageInfo['lifeCycleScripts']>
if ( data.scripts ) {

return undefined
const scripts = Object.keys( data.scripts )
scripts.forEach( script => {

}
if ( this.LIFE_CYCLE_SCRIPTS.includes( script as keyof typeof lcScripts ) )
// @ts-ignore
lcScripts[script] = data.scripts[script]

} )

}
const size = unpackedSize ?? 0
return {
name : data.name,
version : data.version,
Expand Down Expand Up @@ -74,12 +102,16 @@ export class PackageSuper {
: typeof data.funding === 'string'
? data.funding
: data.funding?.url,
npm : `https://www.npmjs.com/package/${data.name}/v/${data.version}`,
unpkg : `https://unpkg.com/${data.name}@${data.version}/`,
},
unpackedSize : unpackedSize ?? 0,
dependencies : data.dependencies,
devDependencies : data.devDependencies,
installedBy : installedBy === undefined
unpackedSize : size,
unpackedSizeKB : size / 1000,
unpackedSizeMB : size / 1000000,
dependencies : data.dependencies,
devDependencies : data.devDependencies,
lifeCycleScripts : Object.keys( lcScripts ).length ? lcScripts : undefined,
installedBy : installedBy === undefined
? undefined
: Array.isArray( installedBy )
? installedBy
Expand Down Expand Up @@ -151,20 +183,30 @@ export class PackageSuper {

for ( const [ depName, depVersion ] of Object.entries( dependencies ) ) {

const packageKey = `${depName}@${depVersion}`
if ( !depVersion ) continue
if ( this.processedPackages.has( packageKey ) ) continue
try {

const packageKey = `${depName}@${depVersion}`
if ( !depVersion ) continue
if ( this.processedPackages.has( packageKey ) ) continue

this.processedPackages.add( packageKey )

this.processedPackages.add( packageKey )
const depData = await this.getRegistryData( depName, depVersion, level, parentName )

const depData = await this.getRegistryData( depName, depVersion, level, parentName )
packages.push( depData )

packages.push( depData )
if ( depData.dependencies ) {

if ( depData.dependencies ) {
const subDeps = await this.#processDependencies( depData.dependencies, level + 1, depName )
packages.push( ...subDeps )

}

}
catch ( e ) {

const subDeps = await this.#processDependencies( depData.dependencies, level + 1, depName )
packages.push( ...subDeps )
if ( this.opts?.skipError ) continue
throw e

}

Expand Down
19 changes: 13 additions & 6 deletions packages/core/src/search/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { LIFE_CYCLE_SCRIPTS } from './const'
import type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package'

export type PackageJSON = JSONSchemaForNPMPackageJsonFiles & {
name : string
version : string
}

type lifeCycleScripts = typeof LIFE_CYCLE_SCRIPTS[number]
export type PackageInfo = {
name : string
version : string
Expand All @@ -18,25 +19,31 @@ export type PackageInfo = {
url : string
}
url: {
npm : string
homepage? : string
repository? : string
funding? : string
unpkg? : string
}
/** Unpacked size in bytes */
unpackedSize : number
dependencies? : PackageJSON['dependencies']
devDependencies? : PackageJSON['devDependencies']
installedBy? : string[]
unpackedSize : number
unpackedSizeKB : number
unpackedSizeMB : number
dependencies? : PackageJSON['dependencies']
devDependencies? : PackageJSON['devDependencies']
lifeCycleScripts? : { [key in lifeCycleScripts]?: string }
installedBy? : string[]
/** Level of the dependence installation. Main packages is 0 */
level : number
level : number
}

export type SiziumResponse = {
id : string
packageNum : number
/** Size in bytes */
size : number
sizeKB : number
sizeMB : number
packages : PackageInfo[]
}

Loading

0 comments on commit 8f5d2bf

Please sign in to comment.