<commit_before>// Filename: scrounge_adapt.js
// Timestamp: 2018.04.07-19:01:44 (last modified)
// Author(s): bumblehead <<EMAIL>>

const umd = require('umd'),
      path = require('path'),
      less = require('less'),
      babel = require('babel-core'),
      babelpresetenv = require('babel-preset-env'),
      umdname = require('umdname'),
      Cleancss = require('clean-css'),
      moduletype = require('moduletype'),
      typescript = require('typescript'),
      replacerequires = require('replace-requires'),
      replaceimports = require('replace-imports'),

      scrounge_uid = require('./scrounge_uid');

module.exports = (o => {
  o = (opts, node, fn) => {
    let filepath = node.get('filepath'),
        extname = path.extname(filepath).slice(1),
        str = node.get('content');

    opts.embedarr.map(embed => {
      if (~filepath.indexOf(embed.filepath)) {
        str = embed.content + str;
      }
    });

    opts.globalarr.map(global => {
      // module namespace defined on user-given name
      if (~filepath.indexOf(global.filepath)) {
        str += '\nif (typeof window === "object") window.:NAME = :UID;'
          .replace(/:NAME/g, global.name)
          .replace(/:UID/g, scrounge_uid.sanitised(node.get('uid')));
      }
    });

    if (extname && typeof o[extname] === 'function') {
      o[extname](opts, node, str, fn);
    } else {
      fn(null, str);
    }
  };

  o.try = (path, fn) => {
    try {
      return fn();
    } catch (e) {
      console.error(e);

      throw new Error(`[!!!] parse error ${path}`);
    }
  };

  o.js = (opts, node, str, fn) => {
    let filepath = node.get('filepath'),
        modname = scrounge_uid.sanitised(node.get('uid')),
        skip = opts.skippatharr
          .some(path => ~filepath.indexOf(path));

    if (skip)
      return fn(null, str);

    str = o.try(filepath, () => (
      babel.transform(str, {
        compact : opts.iscompress && !skip,
        presets : opts.ises2015 ? [
          babelpresetenv
        ] : [],
        plugins : opts.babelpluginarr || []
      })).code);

    if (moduletype.umd(str)) {
      str = umdname(str, modname);
    } else if (moduletype.cjs(str) ||
               moduletype.esm(str)) {
      if (moduletype.cjs(str) && !moduletype.esm(str)) {
        str = umd(modname, str, { commonJS : true });
      }

      // build import and require replacement mappings
      let replace = node.get('outarr').reduce((prev, cur) => {
        let refname = cur.get('refname'),
            depname = scrounge_uid.sanitised(cur.get('uid'));

        opts.aliasarr.map(([ matchname, newname ]) => (
          newname === refname &&
            (prev[matchname] = depname)
        ));

        prev.require[refname] = depname;
        prev.import[refname] = `./${depname}.js`;

        return prev;
      }, {
        require : {},
        import : {}
      });

      str = replacerequires(str, replace.require);
      str = replaceimports(str, replace.import);
    }

    fn(null, str);
  };

  // perform o.js on mjs files
  o.mjs = o.js;

  o.ts = (opts, node, str, fn) => {
    let tsconfig = opts.tsconfig || {},
        jsstr = typescript.transpileModule(str, tsconfig).outputText;

    o.js(opts, node, jsstr, fn);<commit_msg>do not replace require, import unless iscjs, isesm<commit_after>// Filename: scrounge_adapt.js
// Timestamp: 2018.04.08-03:46:11 (last modified)
// Author(s): bumblehead <<EMAIL>>

const umd = require('umd'),
      path = require('path'),
      less = require('less'),
      babel = require('babel-core'),
      babelpresetenv = require('babel-preset-env'),
      umdname = require('umdname'),
      Cleancss = require('clean-css'),
      moduletype = require('moduletype'),
      typescript = require('typescript'),
      replacerequires = require('replace-requires'),
      replaceimports = require('replace-imports'),

      scrounge_uid = require('./scrounge_uid');

module.exports = (o => {
  o = (opts, node, fn) => {
    let filepath = node.get('filepath'),
        extname = path.extname(filepath).slice(1),
        str = node.get('content');

    opts.embedarr.map(embed => {
      if (~filepath.indexOf(embed.filepath)) {
        str = embed.content + str;
      }
    });

    opts.globalarr.map(global => {
      // module namespace defined on user-given name
      if (~filepath.indexOf(global.filepath)) {
        str += '\nif (typeof window === "object") window.:NAME = :UID;'
          .replace(/:NAME/g, global.name)
          .replace(/:UID/g, scrounge_uid.sanitised(node.get('uid')));
      }
    });

    if (extname && typeof o[extname] === 'function') {
      o[extname](opts, node, str, fn);
    } else {
      fn(null, str);
    }
  };

  o.try = (path, fn) => {
    try {
      return fn();
    } catch (e) {
      console.error(e);

      throw new Error(`[!!!] parse error ${path}`);
    }
  };

  o.js = (opts, node, str, fn) => {
    let filepath = node.get('filepath'),
        modname = scrounge_uid.sanitised(node.get('uid')),
        skip = opts.skippatharr.some(path => ~filepath.indexOf(path)),
        iscjs,
        isesm;

    if (skip)
      return fn(null, str);

    str = o.try(filepath, () => (
      babel.transform(str, {
        compact : opts.iscompress && !skip,
        presets : opts.ises2015 ? [
          babelpresetenv
        ] : [],
        plugins : opts.babelpluginarr || []
      })).code);

    iscjs = moduletype.cjs(str);
    isesm = moduletype.esm(str);

    if (moduletype.umd(str)) {
      str = umdname(str, modname);
    } else if (iscjs || isesm) {
      if (iscjs && !isesm)
        str = umd(modname, str, { commonJS : true });

      // build import and require replacement mappings
      let replace = node.get('outarr').reduce((prev, cur) => {
        let refname = cur.get('refname'),
            depname = scrounge_uid.sanitised(cur.get('uid'));

        opts.aliasarr.map(([ matchname, newname ]) => (
          newname === refname &&
            (prev[matchname] = depname)
        ));

        prev.require[refname] = depname;
        prev.import[refname] = `./${depname}.js`;

        return prev;
      }, {
        require : {},
        import : {}
      });

      if (iscjs)
        str = replacerequires(str, replace.require);

      if (isesm)
        str = replaceimports(str, replace.import);
    }

    fn(null, str);
  };

  // perform o.js on mjs files
  o.mjs = o.js;

  o.ts = (opts, node, str, fn) => {
    let tsconfig = opts.tsconfig || {},
        jsstr = typescript.transpileModule(str, tsconfig).outputText;

    o.js(opts, node, jsstr, fn);
{'fraction_non_alphanumeric': 0.17255496588324487, 'fraction_numerical': 0.005761940864291129, 'mean_word_length': 2.288135593220339, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '6500837', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2156, 'n_words': 433}