import path from 'path' ; import { getPackageDeps } from '@rushstack/package-deps-hash' ; import fs from 'fs-extra' ; import objectHash from 'object-hash' ; import { getPackageInfo } from './getPackageInfo' ; export interface PackageInfo { location : string ; name : string ; hash : string ; hasBuildStep : boolean ; } export type PackageInfoMap = { [ key ] : PackageInfo ; } ; export const getPackageHashes = async ( { globalHash , root , } ) : Promise < PackageInfoMap > => { const packageInfos = await getPackageInfo ( { root } ) ; const packageHashes = { } ; for ( const packageInfo of packageInfos ) { const cwd = path . relative ( root , packageInfo . location ) ; const hashObject = getPackageDeps ( cwd ) ; const hashOfFiles = objectHash ( hashObject ) ; const pkgJson = await fs . readJson ( path . join ( cwd , 'package.json' ) ) ; const depsHashes = Object . keys ( { ... pkgJson . dependencies , ... pkgJson . devDependencies , } ) . filter ( ( name ) => packageHashes [ name ] ) . map ( ( name ) => packageHashes [ name ] . hash ) ; const hash = objectHash ( [ hashOfFiles , depsHashes , globalHash ] ) ; const slug = packageInfo . name . replace ( '@' , '' ) . replace ( '/' , '__' ) ; const fileName = `${ slug }-${ hash }.tgz` ; packageHashes [ packageInfo . name ] = { name : packageInfo . name , location : cwd , hash : fileName , hasBuildStep : pkgJson . scripts && pkgJson . scripts . build , } ; } return packageHashes ; } ;