import path from 'path';

function parse(content) {
  try {
    return JSON.parse(content);
  } catch (error) {
    return {}; // ignore parse error silently
  }
}

function values(object) {
  return Object.keys(object || {}).map(key => object[key]);
}

function contain(array, dep, prefix) {
  if (!array) {
    return false;
  }

  // extract name if wrapping with options
  const names = array.map(item => typeof item === 'string' ? item : item[0]);
  if (names.indexOf(dep) !== -1) {
    return true;
  }

  if (prefix && dep.indexOf(prefix) === 0) {
    return contain(array, dep.substring(prefix.length), false);
  }

  return false;
}

function filter(deps, options) {
  const presets = deps.filter(dep =>
    contain(options.presets, dep, 'babel-preset-'));

  const plugins = deps.filter(dep =>
    contain(options.plugins, dep, 'babel-plugin-'));

  return presets.concat(plugins);
}

function checkOptions(deps, options = {}) {
  const optDeps = filter(deps, options);
  const envDeps = values(options.env).map(env => filter(deps, env))
    .reduce((array, item) => array.concat(item), []);

  return optDeps.concat(envDeps);
}

export default (content, filePath, deps) => {
  const filename = path.basename(filePath);

  if (filename === '.babelrc') {
    const options = parse(content);
    return checkOptions(deps, options);
  }

  if (filename === 'package.json') {
    const metadata = parse(content);
    return checkOptions(deps, metadata.babel);
  }

  return [];
};

@@ -243,24 +243,232 @@
 t%5Bkey%5D);%0A%7D%0A%0A
+function isPlugin(target, plugin) %7B%0A  return typeof target === 'string'%0A    ? target === plugin %7C%7C target === %60babel-plugin-$%7Bplugin%7D%60%0A    : target%5B0%5D === plugin %7C%7C target%5B0%5D === %60babel-plugin-$%7Bplugin%7D%60;%0A%7D%0A%0A
 function con
@@ -494,16 +494,16 @@
 efix) %7B%0A
-
   if (!a
@@ -840,24 +840,396 @@
 n false;%0A%7D%0A%0A
+function getReactTransforms(deps, plugins) %7B%0A  if (!plugins) %7B%0A    return %5B%5D;%0A  %7D%0A%0A  const transforms = plugins%0A    .filter(plugin =%3E isPlugin(plugin, 'react-transform'))%0A    .map(plugin =%3E plugin%5B1%5D.transforms.map(transform =%3E transform.transform))%5B0%5D;%0A%0A  if (!transforms) %7B%0A    return %5B%5D;%0A  %7D%0A%0A  return transforms.filter(transform =%3E deps.indexOf(transform) !== -1);%0A%7D%0A%0A
 function fil
@@ -1427,24 +1427,94 @@
 lugin-'));%0A%0A
+  const reactTransforms = getReactTransforms(deps, options.plugins);%0A%0A
   return pre
@@ -1533,16 +1533,40 @@
 plugins)
+.concat(reactTransforms)
 ;%0A%7D%0A%0Afun

Support react transforms.