import { mkdirp , pathExists , readJson , writeJson } from 'fs-extra' ; import { homedir } from 'os' ; import { join } from 'path' ; import { DiezRegistryOptions } from './api' ; const diezRootPath = join ( homedir ( ) , '.diez' ) ; const registryPath = join ( diezRootPath , 'registry.json' ) ; const ensureDiezRoot = ( ) => mkdirp ( diezRootPath ) ; const getRegistry = async ( ) : Promise < Partial < DiezRegistryOptions > > => { await ensureDiezRoot ( ) ; if ( ! await pathExists ( registryPath ) ) { await writeJson ( registryPath , { } ) ; } return await readJson ( registryPath , { throws : false } ) || { } ; } ; const setRegistry = async ( configuration ) => { await ensureDiezRoot ( ) ; return writeJson ( registryPath , configuration , { spaces : 2 } ) ; } ; /**
 * A registry implementation with private details and async static accessors for reading/writing configuration values.
 */ export class Registry { private configuration : Partial < DiezRegistryOptions > = { } ; private static instance ? : Registry ; private async flush ( ) { setRegistry ( this . configuration ) ; } private get < T extends keyof DiezRegistryOptions > ( key ) { return this . configuration [ key ] ; } private async set ( values ) { Object . assign ( this . configuration , values ) ; await this . flush ( ) ; } private async delete ( key ) { delete this . configuration [ key ] ; await this . flush ( ) ; } private static async initialize ( ) { if ( ! Registry . instance ) { Registry . instance = new Registry ( ) ; Registry . instance . configuration = await getRegistry ( ) ; } } param key static async get < T extends keyof DiezRegistryOptions > ( key ) { await this . initialize ( ) ; return this . instance ! . get ( key ) ; } param key param value static async set ( values ) { await this . initialize ( ) ; await this . instance ! . set ( values ) ; } param key static async delete ( key ) { await this . initialize ( ) ; await this . instance ! . delete ( key ) ; } /**
     * Resets the in-memory registry.
     */ static reset ( ) { this . instance = undefined ; } }