import * as fs from 'fs' ; import * as path from 'path' ; import * as util from 'util' ; const readdir = util . promisify ( fs . readdir ) ; const stat = util . promisify ( fs . stat ) ; /**
 * Return the package JSON for the current package
 */ export function currentPackageJson ( ) { return require ( path . join ( process . cwd ( ) , 'package.json' ) ) ; } /**
 * Return the CDK build options
 */ export function cdkBuildOptions ( ) { return currentPackageJson ( ) [ 'cdk-build' ] || { } ; } /**
 * Return the cdk-package options
 */ export function cdkPackageOptions ( ) { return currentPackageJson ( ) [ 'cdk-package' ] || { } ; } /**
 * Whether this is a jsii package
 */ export function isJsii ( ) { return currentPackageJson ( ) . jsii !== undefined ; } /**
 * Whether this is a private package
 */ export function isPrivate ( ) { return currentPackageJson ( ) . private ; } export interface File { filename : string ; path : string ; } export async function listFiles ( dirName , predicate ) { try { const files = ( await readdir ( dirName ) ) . map ( filename => ( { filename , path : path . join ( dirName , filename ) } ) ) ; const ret = [ ] ; for ( const file of files ) { const s = await stat ( file . path ) ; if ( s . isDirectory ( ) ) { ret . push ( ... await listFiles ( file . path , predicate ) ) ; } else { if ( predicate ( file ) ) { ret . push ( file ) ; } } } return ret ; } catch ( e ) { if ( e . code === 'ENOENT' ) { return [ ] ; } throw e ; } } /**
 * Return the unit test files for this package
 */ export async function unitTestFiles ( ) { return listFiles ( 'test' , f => f . filename . endsWith ( '.test.js' ) ) ; } export async function hasIntegTests ( ) { const files = await listFiles ( 'test' , f => f . filename . startsWith ( 'integ.' ) && f . filename . endsWith ( '.js' ) ) ; return files . length > 0 ; } export interface CompilerOverrides { eslint ? : string ; jsii ? : string ; tsc ? : string ; } /**
 * Return the compiler for this package (either tsc or jsii)
 */ export function packageCompiler ( compilers , options ? ) { if ( isJsii ( ) ) { const args = [ '--silence-warnings=reserved-word' , '--add-deprecation-warnings' ] ; if ( options ?. stripDeprecated ) { args . push ( `--strip-deprecated ${ path . join ( __dirname , '..' , '..' , '..' , '..' , 'deprecated_apis.txt' ) }` ) ; } return [ compilers . jsii || require . resolve ( 'jsii/bin/jsii' ) , ... args ] ; } else { return [ compilers . tsc || require . resolve ( 'typescript/bin/tsc' ) , '--build' ] ; } } /**
 * Return the command defined in scripts.gen if exists
 */ export function genScript ( ) { return currentPackageJson ( ) . scripts ?. gen ; } export interface CDKBuildOptions { /**
     * What CloudFormation scope to generate resources for, if any
     */ cloudformation ? : string | string [ ] ; /**
     * Options passed to `eslint` invocations.
     */ eslint ? : { default disable ? : boolean ; } ; pkglint ? : { disable ? : boolean ; } ; /**
     * An optional command (formatted as a list of strings) to run before building
     *
     * (Typically a code generator)
     */ pre ? : string [ ] ; /**
     * An optional command (formatted as a list of strings) to run after building
     *
     * (Schema generator for example)
     */ post ? : string [ ] ; /**
     * An optional command (formatted as a list of strings) to run before testing.
     */ test ? : string [ ] ; /**
     * Whether the package uses Jest for tests.
     * The default is NodeUnit,
     * but we want to eventually move all of them to Jest.
     */ jest ? : boolean ; /**
     * Environment variables to be passed to 'cdk-build' and all of its child processes.
     */ env ? : NodeJS . ProcessEnv ; see https stripDeprecated ? : boolean ; } export interface CDKPackageOptions { /**
     *  Should this package be shrinkwrap
     */ shrinkWrap ? : boolean ; post ? : string [ ] ; } /**
 * Return a full path to the config file in this package
 *
 * The addressed file is cdk-build-tools/config/FILE.
 */ export function configFilePath ( fileName ) { return path . resolve ( __dirname , '..' , 'config' , fileName ) ; }