'use babel'

import FS from 'fs'
import Path from 'path'
import {DEFAULT_CONFIG, CONFIG_FILE_NAME} from '../defaults'

export function getConfig(root) {
  const configPath = Path.join(root, CONFIG_FILE_NAME)

  let config = Object.assign({}, DEFAULT_CONFIG)
  try {
    FS.accessSync(configPath, FS.R_OK)
    const contents = FS.readFileSync(configPath)
    try {
      Object.assign(config, JSON.parse(contents))
    } catch (_) {
      throw new Error(`Malformed configuration file located at ${configPath}`)
    }
  } catch (_) {}

  return config
}

'use babel'

/* @flow */

import Path from 'path'
import type {Ucompiler$Config} from '../types'
import {read} from './common'
import {CONFIG_FILE_NAME} from '../defaults'

export async function getConfig(directory: string): Promise<Ucompiler$Config> {
  const configPath = Path.join(directory, CONFIG_FILE_NAME)
  try {
    return JSON.parse(await read(configPath))
  } catch (_) {
    throw new Error(`Malformed JSON configuration found at ${configPath}`)
  }
}

Add getConfig helper