import fs from 'fs';
import path from 'path';
import promisify from 'es6-promisify';
import {
  InvalidFile,
  InvalidJsonString
} from './errors';

export function convertOneFile(fileName, destFileName) {
  const fileReadPromise = promisify(fs.readFile);
  const fileWritePromise = promisify(fs.writeFile);
  return fileReadPromise(fileName)
    .then(fileContent => 
      fileWritePromise(destFileName, minifyJson(fileContent))
    )
    .catch(reason => {
      if (reason instanceof InvalidJsonString) throw reason;
      throw new InvalidFile(reason.path);
    }
    );
}

function minifyJson(content) {
  try {
    return JSON.stringify(JSON.parse(content));
  } catch (e) {
    throw new InvalidJsonString();
  }
}

import fs from 'fs';
import path from 'path';
import promisify from 'es6-promisify';
import {
  InvalidFile,
  InvalidJsonString
} from './errors';

export function convertOneFile(fileName, destFileName) {
  const fileReadPromise = promisify(fs.readFile);
  const fileWritePromise = promisify(fs.writeFile);
  
  const readFile = 
    fileReadPromise(fileName)
      .catch(reason => {throw new InvalidFile(reason.path)});
  
  const minify = fileContent =>
    minifyJson(fileContent)
      .catch(() => {throw new InvalidJsonString()});
  
  const writeFile = minifiedJson => 
    fileWritePromise(destFileName, minifiedJson)
      .catch(reason => {throw new InvalidFile(reason.path)});
  
  return readFile
    .then(minify)
    .then(writeFile);
}

const minifyJson = content => 
  new Promise((resolve, reject) => {
    try {
      resolve(JSON.stringify(JSON.parse(content)));
    } catch (e) {
      reject(e);
    }
  });

Refactor the one file converter.
Refactor the one file converter.
