import fs from "fs" ; import os from "os" ; import path from "path" ; import { pathExists } from "./util" ; export const DEFAULT_SITE = "quip.com" ; export const SKIP_SSL_FOR_SITES = new Set ( [ "quip.codes" ] ) ; export const NPM_BINARY_NAME = /^win/ . test ( process . platform ) ? "npm.cmd" : "npm" ; interface QLAConfigSite { accessToken : string ; } export interface QLAConfig { _exists : boolean ; sites : { [ hostname ] : QLAConfigSite ; } ; } export const defaultConfigPath = ( ) => path . join ( os . homedir ( ) , ".quiprc" ) ; export const writeSiteConfig = async ( configPath , site , config ) => { const qlaConfig = await readConfig ( configPath ) ; qlaConfig . sites [ site ] = config ; await writeConfig ( configPath , qlaConfig ) ; } ; const writeConfig = ( configPath , config ) => { let newConfig = { ... config , } ; delete newConfig . _exists ; return fs . promises . writeFile ( configPath , JSON . stringify ( newConfig , null , 2 ) , "utf-8" ) ; } ; export const readConfig = async ( configPath ) : Promise < QLAConfig > => { if ( ! ( await pathExists ( configPath ) ) ) { return { _exists : false , sites : { } } ; } const configStr = ( await fs . promises . readFile ( configPath , "utf-8" ) ) as string ; try { const config = Object . assign ( { _exists : true } , JSON . parse ( configStr ) ) ; config . sites = config . sites || { } ; return config ; } catch ( e ) { throw new Error ( `Corrupted config: ${ e . message }` ) ; } } ;