import { dirname , resolve , relative } from 'path' ; import os from 'os' ; import loadJsonFile from 'load-json-file' ; import { getVersionInfo , VersionInfo } from './version_info' ; import { PlatformName , PlatformArchitecture , ALL_PLATFORMS } from './platform' ; interface Options { isRelease : boolean ; targetAllPlatforms : boolean ; versionQualifier ? : string ; dockerTagQualifier : string | null ; dockerPush : boolean ; } interface Package { version : string ; engines : { node : string ; } ; workspaces : { packages : string [ ] ; } ; [ key ] : unknown ; } export class Config { static async create ( { isRelease , targetAllPlatforms , versionQualifier , dockerTagQualifier , dockerPush , } ) { const pkgPath = resolve ( __dirname , '../../../../package.json' ) ; const pkg = loadJsonFile . sync ( pkgPath ) ; return new Config ( targetAllPlatforms , pkg , pkg . engines . node , dirname ( pkgPath ) , await getVersionInfo ( { isRelease , versionQualifier , pkg , } ) , dockerTagQualifier , dockerPush , isRelease ) ; } constructor ( private readonly targetAllPlatforms , private readonly pkg , private readonly nodeVersion , private readonly repoRoot , private readonly versionInfo , private readonly dockerTagQualifier , private readonly dockerPush , public readonly isRelease ) { } /**
     * Get Kibana's parsed package.json file
     */ getKibanaPkg ( ) { return this . pkg ; } /**
     * Get the node version required by Kibana
     */ getNodeVersion ( ) { return this . nodeVersion ; } /**
     * Get the docker tag qualifier
     */ getDockerTagQualfiier ( ) { return this . dockerTagQualifier ; } /**
     * Get docker push
     */ getDockerPush ( ) { return this . dockerPush ; } /**
     * Convert an absolute path to a relative path, based from the repo
     */ getRepoRelativePath ( absolutePath ) { return relative ( this . repoRoot , absolutePath ) ; } /**
     * Resolve a set of relative paths based from the directory of the Kibana repo
     */ resolveFromRepo ( ... subPaths ) { return resolve ( this . repoRoot , ... subPaths ) ; } /**
     * Return the list of Platforms we are targeting, if --this-platform flag is
     * specified only the platform for this OS will be returned
     */ getTargetPlatforms ( ) { if ( this . targetAllPlatforms ) { return ALL_PLATFORMS ; } return [ this . getPlatformForThisOs ( ) ] ; } /**
     * Return the list of Platforms we need/have node downloads for. We always
     * include the linux platform even if we aren't targeting linux so we can
     * reliably get the LICENSE file, which isn't included in the windows version
     */ getNodePlatforms ( ) { if ( this . targetAllPlatforms ) { return ALL_PLATFORMS ; } if ( process . platform === 'linux' ) { return [ this . getPlatform ( 'linux' , 'x64' ) ] ; } return [ this . getPlatformForThisOs ( ) , this . getPlatform ( 'linux' , 'x64' ) ] ; } getPlatform ( name , arch ) { const selected = ALL_PLATFORMS . find ( ( p ) => { return name === p . getName ( ) && arch === p . getArchitecture ( ) ; } ) ; if ( ! selected ) { throw new Error ( `Unable to find platform (${ name }) with architecture (${ arch })` ) ; } return selected ; } /**
     * Get the platform object representing the OS on this machine
     */ getPlatformForThisOs ( ) { return this . getPlatform ( os . platform ( ) as PlatformName , os . arch ( ) as PlatformArchitecture ) ; } /**
     * Get the version to use for this build
     */ getBuildVersion ( ) { return this . versionInfo . buildVersion ; } /**
     * Get the build number of this build
     */ getBuildNumber ( ) { return this . versionInfo . buildNumber ; } /**
     * Get the git sha for this build
     */ getBuildSha ( ) { return this . versionInfo . buildSha ; } /**
     * Resolve a set of paths based from the target directory for this build.
     */ resolveFromTarget ( ... subPaths ) { return resolve ( this . repoRoot , 'target' , ... subPaths ) ; } }