<commit_before>// Concats js/css resources per-bundle
//


'use strict';


/*global underscore, N*/


// stdlib
var fs    = require('fs');
var path  = require('path');


// 3rd-party
var _       = underscore;
var async   = require('async');
var fstools = require('fs-tools');


// internal
var stopwatch = require('./utils/stopwatch');


////////////////////////////////////////////////////////////////////////////////


// Returns map of { <locale> : <source>, ... }
//
function concatPackageJavascripts(pkgName, tmpdir, callback) {
  // collect package "base" javascripts
  async.map([ 'views', 'client' ], function (part, next) {
    var filename = path.join(tmpdir, part, pkgName + '.js');

    fs.exists(filename, function (exists) {
      if (!exists) {
        next(null, "");
        return;
      }

      fs.readFile(filename, 'utf8', next);
    });
  }, function (err, results) {
    var base, data;

    if (err) {
      callback(err);
      return;
    }

    data = {};
    base = results.join("\n\n");

    async.forEach(N.config.locales['enabled'], function (locale, next) {
      var filename = path.join(tmpdir, 'i18n', pkgName, locale + '.js');

      data[locale] = base;

      fs.exists(filename, function (exists) {
        if (!exists) {
          next();
          return;
        }

        fs.readFile(filename, 'utf8', function (err, str) {
          if (err) {
            next(err);
            return;
          }

          data[locale] += str;
          next();
        });
      });
    }, function (err) {
      if (err) {
        callback(err);
        return;
      }

      callback(null, data);
    });
  });
}


// Returns package styesheet string
function readPackageStyles(pkgName, tmpdir, callback) {
  var filename = path.join(tmpdir, "styles", pkgName + ".css");

  fs.exists(filename, function (exists) {
    if (!exists) {
      callback(null, "");
      return;
    }

    fs.readFile(filename, "utf8", callback);
  });
}


// Returns hash with package -> assets:
//
//    {
//      forum: {
//        stylesheet: <String>,
//        javascripts: {
//          "en-US": <String>,
//          ...
//        }
//      }
//    }
//
function collectPackageAssets(tmpdir, sandbox, callback) {
  var data = {};

  async.forEach(_.keys(sandbox.config.packages), function (pkgName, next) {
    async.series([
      async.apply(readPackageStyles, pkgName, tmpdir),
      async.apply(concatPackageJavascripts, pkgName, tmpdir)
    ], function (err, results) {
      if (err) {
        next(err);
        return;
      }

      data[pkgName] = _.object(["stylesheet", "javascripts"], results);
      next();
    });
  }, function (err) {
    callback(err, data);
  });
}



// Concat stylesheets from different bundles and writes bundles stylesheet if
// it's non-empty
//
function writeBundleStylesheet(name, tmpdir, sandbox, assets, callback) {
  var
  stylesheet  = "",
  filename    = path.join(tmpdir, "bundle", name + ".css");

  _.each(assets, function (data) {
    stylesheet += data.stylesheet;
  });

  if (!stylesheet) {
    callback();
    return;
  }

  sandbox.assets.files.push(filename);

  fs.writeFile(filename, stylesheet, "utf8", callback);
}


// Concat all javascripts per-bundle / locale
function writeBundleJavascripts(name, tmpdir, sandbox, assets, callback) {
  async.forEach(N.config.locales["enabled"], function (locale, next) {
    var
    javascript  = "",
    filename    = path.join(tmpdir, "bundle", name + "." + locale + ".js");

    _.each(assets, function (data) {
      javascript += data.javascripts[locale];
    });

    sandbox.assets.files.push(filename);

    fs.writeFile(filename, javascript, "utf8", callback);
  }, callback);
}


// Write bundle files
//<commit_msg>Fix assets localization<commit_after>// Concats js/css resources per-bundle
//


'use strict';


/*global underscore, N*/


// stdlib
var fs    = require('fs');
var path  = require('path');


// 3rd-party
var _       = underscore;
var async   = require('async');
var fstools = require('fs-tools');


// internal
var stopwatch = require('./utils/stopwatch');


////////////////////////////////////////////////////////////////////////////////


// Returns map of { <locale> : <source>, ... }
//
function concatPackageJavascripts(pkgName, tmpdir, withLocales, callback) {
  // collect package "base" javascripts
  async.map([ 'views', 'client' ], function (part, next) {
    var filename = path.join(tmpdir, part, pkgName + '.js');

    fs.exists(filename, function (exists) {
      if (!exists) {
        next(null, "");
        return;
      }

      fs.readFile(filename, 'utf8', next);
    });
  }, function (err, results) {
    var base, data;

    if (err) {
      callback(err);
      return;
    }

    data = {};
    base = results.join("\n\n");

    if (!withLocales) {
      data['*'] = base;
      callback();
      return;
    }

    async.forEach(N.config.locales['enabled'], function (locale, next) {
      var filename = path.join(tmpdir, 'i18n', pkgName, locale + '.js');

      data[locale] = base;

      fs.exists(filename, function (exists) {
        if (!exists) {
          next();
          return;
        }

        fs.readFile(filename, 'utf8', function (err, str) {
          if (err) {
            next(err);
            return;
          }

          data[locale] += str;
          next();
        });
      });
    }, function (err) {
      if (err) {
        callback(err);
        return;
      }

      callback(null, data);
    });
  });
}


// Returns package styesheet string
function readPackageStyles(pkgName, tmpdir, callback) {
  var filename = path.join(tmpdir, "styles", pkgName + ".css");

  fs.exists(filename, function (exists) {
    if (!exists) {
      callback(null, "");
      return;
    }

    fs.readFile(filename, "utf8", callback);
  });
}


// Returns hash with package -> assets:
//
//    {
//      forum: {
//        stylesheet: <String>,
//        javascripts: {
//          "en-US": <String>,
//          ...
//        }
//      }
//    }
//
function collectPackageAssets(tmpdir, sandbox, callback) {
  var data = {};

  async.forEach(_.keys(sandbox.config.packages), function (pkgName, next) {
    var withLocales = !!sandbox.config.packages[pkgName].i18n_client;

    async.series([
      async.apply(readPackageStyles, pkgName, tmpdir),
      async.apply(concatPackageJavascripts, pkgName, !!withLocales, tmpdir)
    ], function (err, results) {
      if (err) {
        next(err);
        return;
      }

      data[pkgName] = _.object(["stylesheet", "javascripts"], results);
      next();
    });
  }, function (err) {
    callback(err, data);
  });
}



// Concat stylesheets from different bundles and writes bundles stylesheet if
// it's non-empty
//
function writeBundleStylesheet(name, tmpdir, sandbox, assets, callback) {
  var
  stylesheet  = "",
  filename    = path.join(tmpdir, "bundle", name + ".css");

  _.each(assets, function (data) {
    stylesheet += data.stylesheet;
  });

  if (!stylesheet) {
    callback();
    return;
  }

  sandbox.assets.files.push(filename);

  fs.writeFile(filename, stylesheet, "utf8", callback);
}


// Concat all javascripts per-bundle / locale
function writeBundleJavascripts(name, tmpdir, sandbox, assets, callback) {
  var withLocales = _.any(assets, function (data) {
    return !!data.javascripts['*'];
  });

  function writeFile(locale) {
    var
    javascript  = "",
    suffix      = (locale ? ('.' + locale) : '') + '.js',
    filename    = path.join(tmpdir, "bundle", name + suffix);

    _.each(assets, function (data) {
      javascript += data.javascripts[locale] || data.javascripts['*'] || '';
    });

    sandbox.assets.files.push(filename);
    fs.writeFileSync(filename, javascript, "utf8");
  }

  if (!withLocales) {
    writeFile();
    callback();
    return;
  }

  N.config.locales["enabled"].forEach(writeFile);
  callback();
}


// Write bundle files
//
{'fraction_non_alphanumeric': 0.18295139763283808, 'fraction_numerical': 0.002266431629312516, 'mean_word_length': 2.251330331559558, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 13, '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': '6318428', 'n_tokens_mistral': 2551, 'n_tokens_neox': 2422, 'n_words': 551}