const { injectBabelPlugin, compose } = require('react-app-rewired');
const rewireCssModules = require('react-app-rewire-css-modules');
const rewireLess = require('react-app-rewire-less');
const WebpackVersionFilePlugin = require('webpack-version-file-plugin');
const execa = require('execa');
const path = require('path');
const gitHash = execa.sync('git', ['rev-parse', '--short', 'HEAD']).stdout;
const gitNumCommits = Number(execa.sync('git', ['rev-list', 'HEAD', '--count']).stdout);
const gitDirty = execa.sync('git', ['status', '-s', '-uall']).stdout.length > 0;

module.exports = compose(
  rewireCssModules,
)

module.exports = function override(config, env) {

  const rewires = compose(
    // rewireCssModules,
    rewireLess
  );

  config.plugins.push(new WebpackVersionFilePlugin({
    packageFile: path.join(__dirname, 'package.json'),
    template: path.join(__dirname, 'version.ejs'),
    outputFile: path.join(__dirname, '/src/version.json'),
    extras: {
      'githash': gitHash,
      'gitNumCommits': gitNumCommits,
      'timestamp': Date.now(),
      'dirty': gitDirty
    }
  }));

  config = injectBabelPlugin(['import', { libraryName: 'antd', libraryDirectory: 'es', style: 'css' }], config);
  return rewires(config, env);
};