<commit_before>const path = require('path');
const _ = require('./util/lodash-extended');
const fs = require('fs-extra');
const sass = require('node-sass');
const pathExists = require('path-exists');
const whicheverExists = require('./util/whichever-exists');
const csso = require('csso');
const uglifyjs = require('uglify-js');

module.exports = function(build) {
  const mappingsFileName = whicheverExists('src/wrapper.json', 'src/page.json');
  const mappings = fs.readJsonSync(mappingsFileName).files;
  const destDir = 'dest';
  const normalizedMappings = _(mappings).mapValues(normalizeMapping).mapValues(finalizeDest).value();
  const consumerMappings = _.mapValues(normalizedMappings, mapToRelativeDest);

  _.forEach(normalizedMappings, _.partial(processGroup, build));

  return consumerMappings;

  function normalizeMapping(mapping) {
    if (_.isString(mapping)) {
      return {dest: '', src: mapping};
    } else if (_.isArray(mapping)) {
      return _.map(mapping, normalizeMapping);
    } else if (_.isObject(mapping) && _.isArray(mapping.src)) {
      const dest = mapping.dest || '';
      return _.map(mapping.src, src => ({dest, src}));
    } else if (_.isObject(mapping)) {
      return {dest: mapping.dest || '', src: mapping.src};
    }
  }

  function finalizeDest(fileMapping) {
    const extMap = {
      scss: 'css',
      sass: 'css'
    };

    return _.isArray(fileMapping) ? _.map(fileMapping, finalize) : finalize(fileMapping);

    function finalize(mapping) {
      const extnameSrc = path.extname(mapping.src).substr(1);

      if (extnameSrc && !path.extname(mapping.dest)) {
        const extnameDest = extMap[extnameSrc] || extnameSrc;
        const basenameDest = path.basename(mapping.src, extnameSrc) + extnameDest;
        mapping.dest = path.join(mapping.dest, basenameDest);
      }

      mapping.relativeDest = mapping.dest;
      mapping.dest = path.join(destDir, mapping.dest);
      return mapping;
    }
  }

  function processGroup(build, group) {
    _.isArray(group) ? _.forEach(group, mapping => process(mapping)) : process(mapping);

    function process(mapping) {
      const {dest, src, minified} = mapping;
      const fileType = path.extname(src).substr(1);

      if (_.includes(['sass', 'scss', 'js'], fileType)) {
        const result = build && !minified ? minify({src: compile(src), dest}) : compile(src);
        return fs.outputFileSync(dest, result);
      }

      return fs.copySync(src, dest, {dereference: true});
    }

    function compile(src) {
      const fileType = path.extname(src).substr(1);
      if (fileType === 'scss' || fileType === 'sass') {
        return sass.renderSync({file: src}).css.toString();
      }
      return fs.readFileSync(src).toString();
    }

    function minify(mapping) {
      const {dest, src} = mapping;
      const fileType = path.extname(dest).substr(1);
      if (fileType === 'css') {
        return csso.minify(src).css;
      }
      if (fileType === 'js') {
        return uglifyjs.minify(src, {fromString: true}).code;
      }
      return src;
    }
  }

  function mapToRelativeDest(group) {
    return _.isArray(group) ? _.map(group, mapping => mapping.relativeDest) : group.relativeDest;
  }
}<commit_msg>flag file maps for existing minification<commit_after>const path = require('path');
const _ = require('./util/lodash-extended');
const fs = require('fs-extra');
const sass = require('node-sass');
const pathExists = require('path-exists');
const whicheverExists = require('./util/whichever-exists');
const csso = require('csso');
const uglifyjs = require('uglify-js');

module.exports = function(optimize) {
  const mappingsFileName = whicheverExists('src/wrapper.json', 'src/page.json');
  const mappings = fs.readJsonSync(mappingsFileName).files;
  const destDir = 'dest';
  const normalizedMappings = _(mappings)
    .mapValues(normalizeMapping)
    .thru(mappings => optimize ? _.mapValues(mappings, checkMinified) : mappings)
    .mapValues(finalizeDest)
    .value();
  const consumerMappings = _.mapValues(normalizedMappings, mapToRelativeDest);

  _.forEach(normalizedMappings, _.partial(processGroup, optimize));

  return consumerMappings;

  function normalizeMapping(mapping) {
    if (_.isString(mapping)) {
      return {dest: '', src: mapping};
    } else if (_.isArray(mapping)) {
      return _.map(mapping, normalizeMapping);
    } else if (_.isObject(mapping) && _.isArray(mapping.src)) {
      const dest = mapping.dest || '';
      return _.map(mapping.src, src => ({dest, src}));
    } else if (_.isObject(mapping)) {
      return {dest: mapping.dest || '', src: mapping.src};
    }
  }

  function checkMinified(group) {
    return _.isArray(group) ? _.map(group, mapping => check(mapping)) : check(group);

    function check(mapping) {
      const {src} = mapping;
      const extname = path.extname(src);
      const basenameStripped = path.basename(src, extname);
      const dirname = path.dirname(src);
      mapping.isMinified = path.extname(basenameStripped) === '.min';
      mapping.withMinified = pathExists.sync(path.join(dirname, basenameStripped + '.min' + extname));
      return mapping;
    }
  }

  function finalizeDest(fileMapping) {
    const extMap = {
      scss: 'css',
      sass: 'css'
    };

    return _.isArray(fileMapping) ? _.map(fileMapping, finalize) : finalize(fileMapping);

    function finalize(mapping) {
      const extnameSrc = path.extname(mapping.src).substr(1);

      if (extnameSrc && !path.extname(mapping.dest)) {
        const extnameDest = extMap[extnameSrc] || extnameSrc;
        const basenameDest = path.basename(mapping.src, extnameSrc) + extnameDest;
        mapping.dest = path.join(mapping.dest, basenameDest);
      }

      mapping.relativeDest = mapping.dest;
      mapping.dest = path.join(destDir, mapping.dest);
      return mapping;
    }
  }

  function processGroup(optimize, group) {
    _.isArray(group) ? _.forEach(group, mapping => process(mapping)) : process(mapping);

    function process(mapping) {
      const {dest, src, minified} = mapping;
      const fileType = path.extname(src).substr(1);

      if (_.includes(['sass', 'scss', 'js'], fileType)) {
        const result = optimize && !minified ? minify({src: compile(src), dest}) : compile(src);
        return fs.outputFileSync(dest, result);
      }

      return fs.copySync(src, dest, {dereference: true});
    }

    function compile(src) {
      const fileType = path.extname(src).substr(1);
      if (fileType === 'scss' || fileType === 'sass') {
        return sass.renderSync({file: src}).css.toString();
      }
      return fs.readFileSync(src).toString();
    }

    function minify(mapping) {
      const {dest, src} = mapping;
      const fileType = path.extname(dest).substr(1);
      if (fileType === 'css') {
        return csso.minify(src).css;
      }
      if (fileType === 'js') {
        return uglifyjs.minify(src, {fromString: true}).code;
      }
      return src;
    }
  }

  function mapToRelativeDest(group) {
    return _.isArray(group) ? _.map(group, mapping => mapping.relativeDest) : group.relativeDest;
  }
}
{'fraction_non_alphanumeric': 0.14973337075498175, 'fraction_numerical': 0.001122649452708392, 'mean_word_length': 3.3993827160493826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3265481', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2172, 'n_words': 458}