import * as rust from './rust' ; import Dict from 'ts-dict' ; import * as JSON from 'ts-typed-json' ; function isStringDict ( x ) { for ( let key of Object . keys ( x ) ) { if ( x [ key ] !== null && typeof x [ key ] !== 'string' ) { return false ; } } return true ; } export default class BuildSettings { private rustc : string ; private env : Dict < string | null > ; constructor ( rustc , env ) { this . rustc = rustc ; this . env = env ; } match ( other ) { return Object . keys ( this . env ) . every ( key => { return ( ! this . env [ key ] && ! other . env [ key ] ) || ( this . env [ key ] === other . env [ key ] ) ; } ) ; } static current ( toolchain ) { let rustc = rust . spawnSync ( "rustc" , [ "--version" ] , toolchain ) . stdout . toString ( 'utf8' ) . trim ( ) ; return new BuildSettings ( rustc , { npm_config_target : process . env . npm_config_target || null , npm_config_arch : process . env . npm_config_arch || null , npm_config_target_arch : process . env . npm_config_target_arch || null , npm_config_disturl : process . env . npm_config_disturl || null , npm_config_runtime : process . env . npm_config_runtime || null , npm_config_build_from_source : process . env . npm_config_build_from_source || null , npm_config_devdir : process . env . npm_config_devdir || null } ) ; } static fromJSON ( value ) { if ( ! JSON . isObject ( value ) ) { throw new TypeError ( "value is not an object" ) ; } let rustc = value . rustc ; let env = value . env ; if ( typeof rustc !== 'string' ) { throw new TypeError ( "value.rustc is not a string" ) ; } if ( ! JSON . isObject ( env ) ) { throw new TypeError ( "value.env is not an object" ) ; } if ( ! isStringDict ( env ) ) { throw new TypeError ( "value.env is not a string dict" ) ; } return new BuildSettings ( rustc , env ) ; } toJSON ( ) { return { "rustc" : this . rustc , "env" : this . env } ; } }