import { FatalError , FixableError } from "./errors" ; import resolveFrom from "resolve-from" ; import chalk from "chalk" ; import { errors } from "./messages" ; import { Package } from "./package" ; let keys = Object . keys ; export async function fixPackage ( pkg ) { if ( pkg . entrypoints . length === 0 ) { throw new FatalError ( errors . noEntrypoints , pkg . name ) ; } let fields = { main : true , module : pkg . entrypoints . some ( ( x ) => x . json . module !== undefined ) , "umd:main" : pkg . entrypoints . some ( ( x ) => x . json [ "umd:main" ] !== undefined ) , browser : pkg . entrypoints . some ( ( x ) => x . json . browser !== undefined ) , } ; keys ( fields ) . filter ( ( x ) => fields [ x ] ) . forEach ( ( field ) => { pkg . setFieldOnEntrypoints ( field ) ; } ) ; return ( await Promise . all ( pkg . entrypoints . map ( ( x ) => x . save ( ) ) ) ) . some ( ( x ) => x ) ; } let unsafeRequire = require ; export function validatePackage ( pkg ) { if ( pkg . entrypoints . length === 0 ) { throw new FatalError ( errors . noEntrypoints , pkg . name ) ; } let fields = { module : pkg . entrypoints [ 0 ] . json . module !== undefined , "umd:main" : pkg . entrypoints [ 0 ] . json [ "umd:main" ] !== undefined , browser : pkg . entrypoints [ 0 ] . json . browser !== undefined , } ; pkg . entrypoints . forEach ( ( entrypoint ) => { keys ( fields ) . forEach ( ( field ) => { if ( entrypoint . json [ field ] && ! fields [ field ] ) { throw new FixableError ( `${ entrypoint . name } has a ${ field } build but ${ pkg . entrypoints [ 0 ] . name } does not have a ${ field } build. Entrypoints in a package must either all have a particular build type or all not have a particular build type.` , pkg . name ) ; } if ( ! entrypoint . json [ field ] && fields [ field ] ) { throw new FixableError ( `${ pkg . entrypoints [ 0 ] . name } has a ${ field } build but ${ entrypoint . name } does not have a ${ field } build. Entrypoints in a package must either all have a particular build type or all not have a particular build type.` , pkg . name ) ; } } ) ; } ) ; if ( fields [ "umd:main" ] ) { let sortaAllDeps = new Set ( [ ... ( pkg . json . peerDependencies ? Object . keys ( pkg . json . peerDependencies ) : [ ] ) , ... ( pkg . json . dependencies ? Object . keys ( pkg . json . dependencies ) : [ ] ) , ] ) ; for ( let depName in pkg . json . dependencies ) { let depPkgJson ; try { depPkgJson = unsafeRequire ( resolveFrom ( pkg . directory , depName + "/package.json" ) ) ; } catch ( err ) { if ( err . code === "ERR_PACKAGE_PATH_NOT_EXPORTED" ) { continue ; } throw err ; } if ( depPkgJson . peerDependencies ) { for ( let pkgName in depPkgJson . peerDependencies ) { if ( ! sortaAllDeps . has ( pkgName ) ) { throw new FatalError ( `the package ${ chalk . blue ( pkg . name ) } depends on ${ chalk . blue ( depName ) } which has a peerDependency on ${ chalk . blue ( pkgName ) } but ${ chalk . blue ( pkgName ) } is not specified in the dependencies or peerDependencies of ${ chalk . blue ( pkg . name ) }. please add ${ chalk . blue ( pkgName ) } to the dependencies or peerDependencies of ${ chalk . blue ( pkg . name ) }` , pkg . name ) ; } } } } } }