import BuildSettings from './build-settings' ; import { writeFileSync } from 'fs' ; import Dict from 'ts-dict' ; import * as JSON from 'ts-typed-json' ; /**
 * The current state of build artifacts, for all targets.
 */ export default class Artifacts { /**
     * The relative path within the `target` directory to the current active build,
     * i.e., the build that was most recently copied as the main `.node` addon.
     */ private active : string | null ; /**
     * A table tracking the state of any build artifacts in the `target`
     * directory.
     *
     * On Windows, this table has the type:
     *
     * ```
     * {
     *   "i686-pc-windows-msvc\\debug"?: BuildSettings,
     *   "i686-pc-windows-msvc\\release"?: BuildSettings,
     *   "x86_64-pc-windows-msvc\\debug"?: BuildSettings,
     *   "x86_64-pc-windows-msvc\\release"?: BuildSettings
     * }
     * ```
     *
     * On Linux and macOS, this table has the type:
     *
     * ```
     * {
     *   "debug"?: BuildSettings,
     *   "release"?: BuildSettings
     * }
     * ```
     */ private targets : Dict < BuildSettings > ; constructor ( active = null , targets = { } ) { this . active = active ; this . targets = targets ; } static load ( file ) { try { return Artifacts . fromJSON ( JSON . loadSync ( file ) ) ; } catch ( e ) { return new Artifacts ( ) ; } } static fromJSON ( json ) { if ( ! JSON . isObject ( json ) ) { throw new TypeError ( "expected object, found " + ( json === null ? "null" : typeof json ) ) ; } let active = json . active ; if ( typeof active !== 'string' && active !== null ) { throw new TypeError ( "json.active is not a string or null" ) ; } let jsonTargets = json . targets ; if ( ! JSON . isObject ( jsonTargets ) ) { throw new TypeError ( "json.targets is not an object" ) ; } let targets = { } ; for ( let key of Object . keys ( jsonTargets ) ) { targets [ key ] = BuildSettings . fromJSON ( jsonTargets [ key ] ) ; } return new Artifacts ( active , targets ) ; } toJSON ( ) { let targets = { } ; for ( let target of Object . keys ( this . targets ) ) { targets [ target ] = this . targets [ target ] . toJSON ( ) ; } return { active : this . active , targets : targets } ; } save ( file ) { writeFileSync ( file , JSON . stringify ( this . toJSON ( ) ) ) ; } lookup ( path ) { return this . targets [ path ] ; } activate ( path , settings ) { this . targets [ path ] = settings ; this . active = path ; } haveActivated ( path ) { return this . active === path ; } delete ( path ) { delete this . targets [ path ] ; if ( this . active === path ) { this . active = null ; } } reset ( ) { this . active = null ; this . targets = { } ; } }