import chalk from "chalk" ; import glob from "glob" ; import mkdirp from "mkdirp" ; import * as fs from "mz/fs" ; import * as path from "path" ; import { RepositoryCommandArgs } from "./command" ; import { Logger } from "./logger" ; export const setupDir = path . join ( __dirname , "../setup" ) ; export const mkdirpSafe = async ( dir ) => { try { await mkdirp ( dir ) ; } catch { } } ; param repository param logger returns export const getDependencies = async ( repository , logger ) : Promise < { [ i ] : string ; } > => { const packagePath = path . join ( ... repository , "package.json" ) ; try { return JSON . parse ( ( await fs . readFile ( packagePath ) ) . toString ( ) ) . dependencies || { } ; } catch ( error ) { logger . log ( chalk . red ( "Could not parse" , packagePath ) ) ; throw error ; } } ; export const parseFileJson = async < TContents extends { } > ( file ) : Promise < TContents > => JSON . parse ( ( await fs . readFile ( file ) ) . toString ( ) ) as TContents ; export const globAsync = async ( source ) => new Promise < string [ ] > ( ( resolve , reject ) => { glob ( source , { dot : true } , ( error , matches ) => { if ( error !== null ) { reject ( error ) ; return ; } resolve ( matches ) ; } ) ; } ) ; export const getShenanigansPackageContents = async ( args ) => { const filePath = path . join ( args . directory , args . repository , "package.json" ) ; const packageContentsBase = await fs . readFile ( filePath ) ; const packageContents = JSON . parse ( packageContentsBase . toString ( ) ) ; return packageContents ; } ; export interface DependencyNamesAndExternals { /**
     * Unique names of dependencies of the package.
     */ dependencyNames : string [ ] ; /**
     * Required external files that must exist at runtime.
     */ externals : string [ ] ; } param basePackageLocation returns export const getDependencyNamesAndExternalsOfPackage = async ( basePackageLocation ) : Promise < DependencyNamesAndExternals > => { const { dependencies , shenanigans } = await parseFileJson < Partial < ShenanigansPackage > > ( basePackageLocation ) ; if ( dependencies === undefined || shenanigans === undefined ) { return { dependencyNames : [ ] , externals : [ ] , } ; } const externalsRaw = shenanigans . loading ?. externals ?? [ ] ; const externals = externalsRaw . map ( ( external ) : string => `"${ external . name }": "${ external . js . dev }"` ) ; const allDependencyNames = Object . keys ( dependencies ) ; for ( let i = 0 ; i < allDependencyNames . length ; i += 1 ) { const localDependency = allDependencyNames [ i ] ; const modulePackageLocation = path . normalize ( basePackageLocation . replace ( "package.json" , `node_modules/${ localDependency }/package.json` ) ) ; if ( await fs . exists ( modulePackageLocation ) ) { allDependencyNames . push ( ... ( await getDependencyNamesAndExternalsOfPackage ( modulePackageLocation ) ) . dependencyNames ) ; } } const dependencyNames = Array . from ( new Set ( allDependencyNames ) ) . filter ( ( dependencyName ) => dependencyName !== "requirejs" ) . filter ( ( dependencyName ) => ! externalsRaw . some ( ( externalRaw ) => externalRaw . name === dependencyName . toLowerCase ( ) ) ) ; return { dependencyNames , externals } ; } ;