import path from "path" ; import chalk from "chalk" ; import * as fs from "fs-extra" ; import packlist from "npm-packlist" ; import { Package } from "./package" ; import { FatalError } from "./errors" ; export async function validateIncludedFiles ( pkg ) { try { const rootDistDirectoryTestFilepath = path . join ( pkg . directory , "dist" , "preconstruct-test-file" ) ; const hasNoEntrypointAtRootOfPackage = pkg . entrypoints . every ( ( entrypoint ) => entrypoint . directory !== pkg . directory ) ; await Promise . all ( pkg . entrypoints . map ( async ( entrypoint ) => { let filename = path . join ( entrypoint . directory , "dist" , "preconstruct-test-file" ) ; return fs . outputFile ( filename , "test content" ) ; } ) . concat ( hasNoEntrypointAtRootOfPackage ? fs . outputFile ( rootDistDirectoryTestFilepath , "test content" ) : [ ] ) ) ; let packedFilesArr = await packlist ( { path : pkg . directory } ) ; let result = new Set ( packedFilesArr . map ( ( p ) => path . normalize ( p ) ) ) ; let messages = [ ] ; pkg . entrypoints . forEach ( ( entrypoint ) => { let pkgJsonPath = path . relative ( pkg . directory , path . resolve ( entrypoint . directory , "package.json" ) ) ; let distFilePath = path . relative ( pkg . directory , path . resolve ( entrypoint . directory , "dist" , "preconstruct-test-file" ) ) ; let entrypointName = path . relative ( pkg . directory , entrypoint . directory ) ; if ( ! result . has ( pkgJsonPath ) ) { messages . push ( `the entrypoint ${ chalk . cyan ( entrypointName ) } isn't included in the published files for this package, please add it to the files field in the package's package.json` ) ; } else if ( ! result . has ( distFilePath ) ) { messages . push ( `the dist directory ${ entrypointName === "" ? "" : `for entrypoint ${ chalk . cyan ( entrypointName ) } ` }isn't included in the published files for this package, please add it to the files field in the package's package.json` ) ; } } ) ; if ( hasNoEntrypointAtRootOfPackage && ! result . has ( path . relative ( pkg . directory , rootDistDirectoryTestFilepath ) ) ) { messages . push ( "the dist directory in the root of the package isn't included in the published files for this package, please add it to the files field in the package's package.json.\nthough this package does not have an entrypoint at the root of the package, preconstruct will write common chunks to the root dist directory so it must be included." ) ; } if ( messages . length ) { throw new FatalError ( messages . join ( "\n" ) , pkg . name ) ; } } finally { await Promise . all ( pkg . entrypoints . map ( ( entrypoint ) => fs . remove ( path . join ( entrypoint . directory , "dist" , "preconstruct-test-file" ) ) ) ) ; } }