<commit_before>import * as path from "path";
import * as fs from "fs";

import { watch } from "chokidar";
import {sync as mkdirp} from "mkdirp";
import most from "most";
import _ from "lodash";

import compile from "./compile";
import { entries } from "./util";
import loadAst from "./compile/modules/load-ast";
import compileModules from "./compile/modules/compile";

/**
 * The entry point for the Interlock application
 *
 * @param   {Object}    options
 * @param   {Object[]}  options.emit          An Array of Objects containing compilation options
 *                                            for entry points
 * @param   {String}    options.emit[].entry  The path of a JavaScript entry point (relative to
 *                                            options.srcRoot)
 * @param   {String}    options.emit[].dest   The path to output the compiled JavaScript for the
 *                                            specified entry point (relative to
 *                                            options.destRoot)
 * @param   {String}    options.srcRoot       The root for interlock and root for file fetching
 *                                            paths on the client
 * @param   {String}    options.context       Interlock's working directory
 * @param   {String}    options.destRoot      The root used to generate an entry point's absolute
 *                                            output path
 * @param   {Array}     options.extensions    A list of filetypes for Interlock to read
 * @param   {String}    options.ns            A custom prefix for generated bundles
 */
export default function Interlock (options) {
  const cwd = process.cwd();

  if (!_.isArray(options.emit) || options.emit.length < 1) {
    throw new Error("Must define at least one bundle.");
  }
  if (!_.isString(options.srcRoot)) {
    throw new Error("Must define a project srcRoot");
  }

  let ns;
  try {
    ns = require(path.join(options.srcRoot, "./package.json")).name;
  } catch (e) {
    throw new Error("Invalid project root - cannot find package.json");
  }

  // TODO: validate options, or only persist specific values
  this.options = _.defaults(options || {}, {
    context: cwd,
    destRoot: path.join(cwd, "dist"),
    extensions: [".js", ".jsx", ".es6"],
    ns: ns
  });
}

/**
 * @return  {Promise}  Resolves to the compilation output.
 */
Interlock.prototype.build = function () {
  return compile(this.options)
    .then(this._saveBundles)
    .catch(function (err) {
      console.log(err); // eslint-disable-line no-console
    });
};

Interlock.prototype._saveBundles = function (compilation) {
  for (let [bundleDest, bundle] of entries(compilation.bundles)) {
    const bundleOutput = bundle.raw;
    const destRoot = path.join(compilation.opts.destRoot, bundleDest);
    mkdirp(path.dirname(destRoot));
    fs.writeFileSync(destRoot, bundleOutput);
  }
  return compilation;<commit_msg>Add default value for `implicitBundleDest` option.<commit_after>import * as path from "path";
import * as fs from "fs";

import { watch } from "chokidar";
import {sync as mkdirp} from "mkdirp";
import most from "most";
import _ from "lodash";

import compile from "./compile";
import { entries } from "./util";
import loadAst from "./compile/modules/load-ast";
import compileModules from "./compile/modules/compile";

/**
 * The entry point for the Interlock application
 *
 * @param   {Object}    options
 * @param   {Object[]}  options.emit          An Array of Objects containing compilation options
 *                                            for entry points
 * @param   {String}    options.emit[].entry  The path of a JavaScript entry point (relative to
 *                                            options.srcRoot)
 * @param   {String}    options.emit[].dest   The path to output the compiled JavaScript for the
 *                                            specified entry point (relative to
 *                                            options.destRoot)
 * @param   {String}    options.srcRoot       The root for interlock and root for file fetching
 *                                            paths on the client
 * @param   {String}    options.context       Interlock's working directory
 * @param   {String}    options.destRoot      The root used to generate an entry point's absolute
 *                                            output path
 * @param   {Array}     options.extensions    A list of filetypes for Interlock to read
 * @param   {String}    options.ns            A custom prefix for generated bundles
 */
export default function Interlock (options) {
  const cwd = process.cwd();

  if (!_.isArray(options.emit) || options.emit.length < 1) {
    throw new Error("Must define at least one bundle.");
  }
  if (!_.isString(options.srcRoot)) {
    throw new Error("Must define a project srcRoot");
  }

  let ns;
  try {
    ns = require(path.join(options.srcRoot, "./package.json")).name;
  } catch (e) {
    throw new Error("Invalid project root - cannot find package.json");
  }

  // TODO: validate options, or only persist specific values
  this.options = _.defaults(options || {}, {
    context: cwd,
    destRoot: path.join(cwd, "dist"),
    extensions: [".js", ".jsx", ".es6"],
    ns: ns,
    implicitBundleDest: "[setHash].js"
  });
}

/**
 * @return  {Promise}  Resolves to the compilation output.
 */
Interlock.prototype.build = function () {
  return compile(this.options)
    .then(this._saveBundles)
    .catch(function (err) {
      console.log(err); // eslint-disable-line no-console
    });
};

Interlock.prototype._saveBundles = function (compilation) {
  for (let [bundleDest, bundle] of entries(compilation.bundles)) {
    const bundleOutput = bundle.raw;
    const destRoot = path.join(compilation.opts.destRoot, bundleDest);
    mkdirp(path.dirname(destRoot));
    fs.writeFileSync(destRoot, bundleOutput);
  }
  return compilation;
{'fraction_non_alphanumeric': 0.11135630622984213, 'fraction_numerical': 0.0006790018672551349, 'mean_word_length': 2.3707093821510297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '288296', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1559, 'n_words': 522}