import axios from 'axios' ; import * as path from 'path' ; import * as mkdirpCallback from 'mkdirp' ; import * as Mocha from 'mocha' ; import { readFile as readFileCallback , writeFile as writeFileCallback , exists as existsCallback , } from 'fs' ; import { promisify } from 'util' ; const exists = promisify ( existsCallback ) ; const mkdirp = promisify ( mkdirpCallback ) ; const writeFile = promisify ( writeFileCallback ) ; const readFile = promisify ( readFileCallback ) ; hidden const CACHE_DIRECTORY = '.lamington' ; hidden const ENCODING = 'utf8' ; hidden const CONFIGURATION_FILE_NAME = '.lamingtonrc' ; hidden export interface LamingtonConfig { cdt : string ; eos : string ; contracts : string ; keepAlive ? : boolean ; outDir ? : string ; include ? : Array < string > ; exclude ? : Array < string > ; debugTransactions ? : boolean ; debug : LamingtonDebugLevel ; reporter ? : string ; reporterOptions ? : any ; bailOnFailure : boolean ; } /**
 * Level of debug output
 */ export enum LamingtonDebugLevel { NONE = 0 , MINIMAL , VERBOSE } export namespace LamingtonDebugLevel { export function isNone ( debugLevel ) { return debugLevel == LamingtonDebugLevel . NONE ; } export function isMin ( debugLevel ) { return debugLevel == LamingtonDebugLevel . MINIMAL ; } export function isVerbose ( debugLevel ) { return debugLevel == LamingtonDebugLevel . VERBOSE ; } } /**
 * Default configuration values which are merged in
 * as the base layer config. Users can override these
 * values by specifying them in their `.lamingtonrc`
 */ const DEFAULT_CONFIG = { eos : '' , cdt : '' , contracts : 'v1.8.0-rc1' , debug : LamingtonDebugLevel . NONE , debugTransactions : false , keepAlive : false , outDir : CACHE_DIRECTORY , include : [ '.*' ] , exclude : [ ] , bailOnFailure : false , } ; /**
 * Manages Lamington configuration setup and caching
 */ export class ConfigManager { hidden private static config : LamingtonConfig ; author Kevin Brown <github.com/thekevinbrown>
     *  author Mitch Pierias <github.com/MitchPierias>
      public static async initWithDefaults ( ) { DEFAULT_CONFIG . cdt = await ConfigManager . getAssetURL ( 'EOSIO' , 'eosio.cdt' , 'amd64.deb' ) ; DEFAULT_CONFIG . eos = await ConfigManager . getAssetURL ( 'EOSIO' , 'eos' , 'ubuntu-18.04' ) ; if ( ! ( await ConfigManager . configExists ( ) ) ) { console . log ( 'Project has not yet been initialized.' ) ; console . log ( 'Please run lamington init before running this command.' ) ; process . exit ( 1 ) ; } await ConfigManager . loadConfigFromDisk ( ) ; } author Kevin Brown <github.com/thekevinbrown>
     *  param organization param repository param filter hidden private static async getAssetURL ( organization , repository , filter ) { const result = await axios . get ( `https://api.github.com/repos/${ organization }/${ repository }/releases/latest` ) ; if ( ! result . data || ! result . data . assets || ! Array . isArray ( result . data . assets ) ) { console . error ( result ) ; throw new Error ( 'Unexpected response from GitHub API. Please try again later.' ) ; } const asset = result . data . assets . find ( ( asset ) => asset . browser_download_url . includes ( filter ) ) ; if ( ! asset ) throw new Error ( `Could not locate asset with ${ filter } in the download URL in the ${ organization }/${ repository } repository` ) ; return asset . browser_download_url as string ; } public static async isValidConfig ( config ) { return true ; } author Mitch Pierias <github.com/MitchPierias>
     *  author Kevin Brown <github.com/thekevinbrown>
     *  param atPath public static async createConfigIfMissing ( atPath = CONFIGURATION_FILE_NAME ) { if ( ( await ConfigManager . configExists ( atPath ) ) && ( await ConfigManager . isValidConfig ( ConfigManager . config ) ) ) return ; await mkdirp ( CACHE_DIRECTORY , { } ) ; const defaultConfig = { ... DEFAULT_CONFIG , cdt : await ConfigManager . getAssetURL ( 'EOSIO' , 'eosio.cdt' , 'amd64.deb' ) , eos : await ConfigManager . getAssetURL ( 'EOSIO' , 'eos' , 'ubuntu-18.04' ) , } ; await writeFile ( atPath , JSON . stringify ( defaultConfig , null , 4 ) , ENCODING ) ; } author Mitch Pierias <github.com/MitchPierias>
     *  param atPath returns public static configExists ( atPath = CONFIGURATION_FILE_NAME ) { return exists ( atPath ) ; } author Kevin Brown <github.com/thekevinbrown>
     *  param atPath public static async loadConfigFromDisk ( atPath = CONFIGURATION_FILE_NAME ) { ConfigManager . config = { ... DEFAULT_CONFIG , ... JSON . parse ( await readFile ( atPath , ENCODING ) ) , } ; } author Kevin Brown <github.com/thekevinbrown>
      static get eos ( ) { return ( ConfigManager . config && ConfigManager . config . eos ) || '' ; } author Kevin Brown <github.com/thekevinbrown>
      static get cdt ( ) { return ( ConfigManager . config && ConfigManager . config . cdt ) || '' ; } author Johan Nordberg <github.com/jnordberg>
      static get contracts ( ) { return ( ConfigManager . config && ConfigManager . config . contracts ) || 'master' ; } author Mitch Pierias <github.com/MitchPierias>
      static get keepAlive ( ) { return ( ConfigManager . config && ConfigManager . config . keepAlive ) || DEFAULT_CONFIG . keepAlive ; } author Kevin Brown <github.com/thekevinbrown>
      static get debugTransactions ( ) { return ( ( ConfigManager . config && ConfigManager . config . debugTransactions ) || DEFAULT_CONFIG . debugTransactions ) ; } author Dallas Johnson <github.com/dallasjohnson>
      static get debugLevel ( ) { return ( ConfigManager . config && ConfigManager . config . debug ) || DEFAULT_CONFIG . debug ; } author Dallas Johnson <github.com/dallasjohnson>
      static get debugLevelNone ( ) { return LamingtonDebugLevel . isNone ( this . debugLevel ) ; } static get debugLevelMin ( ) { return LamingtonDebugLevel . isMin ( this . debugLevel ) ; } static get debugLevelVerbose ( ) { return LamingtonDebugLevel . isVerbose ( this . debugLevel ) ; } author Mitch Pierias <github.com/MitchPierias>
      static get outDir ( ) { return ( ConfigManager . config && ConfigManager . config . outDir ) || DEFAULT_CONFIG . outDir ; } author Dallas Johnson <github.com/dallasjohnson>
      static get include ( ) { return ( ConfigManager . config && ConfigManager . config . include ) || DEFAULT_CONFIG . include ; } author Mitch Pierias <github.com/MitchPierias>
      static get exclude ( ) { return ( ConfigManager . config && ConfigManager . config . exclude ) || DEFAULT_CONFIG . exclude ; } author Dallas Johnson <github.com/dallasjohnson>
      static get testReporter ( ) { return ( ConfigManager . config && ConfigManager . config . reporter ) || Mocha . reporters . Min ; } author Dallas Johnson <github.com/dallasjohnson>
      static get bailOnFailure ( ) { return ( ( ConfigManager . config && ConfigManager . config . bailOnFailure ) || DEFAULT_CONFIG . bailOnFailure ) ; } }