import plist from '@expo/plist' ; import fs from 'fs-extra' ; import path from 'path' ; import { ExponentTools , LoggerDetach as logger } from '../internal' ; const { spawnAsyncThrowError } = ExponentTools ; function _getNormalizedPlistFilename ( plistName ) { let plistFilename ; if ( plistName . indexOf ( '.' ) !== - 1 ) { plistFilename = plistName ; } else { plistFilename = `${ plistName }.plist` ; } return plistFilename ; } param plistName async function modifyAsync ( plistPath , plistName , transform ) { const plistFilename = _getNormalizedPlistFilename ( plistName ) ; const configPlistName = path . join ( plistPath , plistFilename ) ; const configFilename = path . join ( plistPath , `${ plistName }.json` ) ; let config ; if ( process . platform === 'darwin' ) { await spawnAsyncThrowError ( 'plutil' , [ '-convert' , 'json' , configPlistName , '-o' , configFilename , ] ) ; const configContents = await fs . readFile ( configFilename , 'utf8' ) ; try { config = JSON . parse ( configContents ) ; } catch ( e ) { logger . info ( `Error parsing ${ configFilename }` , e ) ; logger . info ( 'The erroneous file contents was:' , configContents ) ; config = { } ; } } else { config = plist . parse ( fs . readFileSync ( configPlistName , 'utf8' ) ) ; } config = await transform ( config ) ; fs . copyFileSync ( configPlistName , `${ configPlistName }.bak` ) ; await fs . writeFile ( configFilename , JSON . stringify ( config ) ) ; if ( process . platform === 'darwin' ) { await spawnAsyncThrowError ( 'plutil' , [ '-convert' , 'xml1' , configFilename , '-o' , configPlistName , ] ) ; } else { await fs . writeFile ( configPlistName , plist . build ( config ) ) ; } return config ; } async function createBlankAsync ( plistPath , plistName ) { const emptyConfig = { } ; const tmpConfigFile = path . join ( plistPath , `${ plistName }.json` ) ; await fs . writeFile ( tmpConfigFile , JSON . stringify ( emptyConfig ) ) ; const plistFilename = _getNormalizedPlistFilename ( plistName ) ; const configPlistName = path . join ( plistPath , plistFilename ) ; if ( process . platform === 'darwin' ) { await spawnAsyncThrowError ( 'plutil' , [ '-convert' , 'xml1' , tmpConfigFile , '-o' , configPlistName , ] ) ; } else { await fs . writeFile ( configPlistName , JSON . stringify ( plist . build ( emptyConfig ) ) ) ; } fs . removeSync ( tmpConfigFile ) ; } export { modifyAsync , createBlankAsync } ;