<commit_before>// compileWASM.js
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const colors = require('colors');

/**
 * This function pulls parameters from the wasm.config.js file,
 * sets the emcc environment and calls the emcc compile arguments
 * via shell commands
 * @param {Object} config 
 */
function compileWASM (config) {

  // check that emscripten path is correct
  if (!fs.existsSync(config.emscripten_path)) {
    return process.stdout.write(colors.red(`Error: Could not find emscripten directory at ${config.emscripten_path}\n`));
  }

  // format exported functions, if present, from config for shell script
  let expFuncStr = '';
  if (config.exported_functions && config.exported_functions.length > 0) {
    let expFuncs = config.exported_functions.reduce((acc, val) => acc.concat('\'', val, '\'\,'), '[');
    expFuncs = expFuncs.substring(0, expFuncs.length - 1).concat(']');
    expFuncStr = `-s EXPORTED_FUNCTIONS="${expFuncs}" `;
  }

  // format flags from config for shell script
  const flags = config.flags.reduce((acc, val) => acc.concat(' ', val), '');

  process.stdout.write(colors.cyan('Running emscripten...\n'));

  // execute shell script
  exec(`
  if [[ :$PATH: != *:"/emsdk":* ]]
  then
    # use path to emsdk folder, relative to project directory
    BASEDIR="${config.emscripten_path}"
    EMSDK_ENV=$(find "$BASEDIR" -type f -name "emsdk_env.sh")
    source "$EMSDK_ENV"
  fi

  emcc -o ${config.outputfile} ${config.inputfile} ${expFuncStr} ${flags}
  `, (error, stdout, stderr) => {
    // check for emcc compile errors
    if (stderr) {
      process.stderr.write(colors.red.bold('EMSCRIPTEN COMPILE ERROR\n'));
      process.stderr.write(colors.white(stderr));
    } else {
      process.stdout.write(stdout);
      insertEventListener(config);<commit_msg>MVP cmake support. TODO: pass through flags, output file, exported functions<commit_after>// compileWASM.js
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
const colors = require('colors');

/**
 * This function pulls parameters from the wasm.config.js file,
 * sets the emcc environment and calls the emcc compile arguments
 * via shell commands
 * @param {Object} config 
 */
function compileWASM (config) {

  // check that emscripten path is correct
  if (!fs.existsSync(config.emscripten_path)) {
    return process.stdout.write(colors.red(`Error: Could not find emscripten directory at ${config.emscripten_path}\n`));
  }

  // format exported functions, if present, from config for shell script
  let expFuncStr = '';
  if (config.exported_functions && config.exported_functions.length > 0) {
    let expFuncs = config.exported_functions.reduce((acc, val) => acc.concat('\'', val, '\'\,'), '[');
    expFuncs = expFuncs.substring(0, expFuncs.length - 1).concat(']');
    expFuncStr = `-s EXPORTED_FUNCTIONS="${expFuncs}" `;
  }

  // format flags from config for shell script
  const flags = config.flags.reduce((acc, val) => acc.concat(' ', val), '');
  // construct the default compile mode string.
  let commandStr = `emcc -o ${config.outputfile} ${config.inputfile} ${expFuncStr} ${flags}`;
  // generate a command string suitable for cmake
  if(config.mode == "build") {
    // assuming make
    config.generator = config.generator ? config.generator : 'make';
    switch(config.generator) {
      case 'ninja':
        config.gen_flag = 'Ninja';
      default:
        config.gen_flag = '"Unix Makefiles"';
        break;
    }
    
    commandStr = `cd wasm && emcmake cmake -G ${config.gen_flag} ..`+
      ` && emmake ${config.generator}`;
  }
  process.stdout.write(colors.cyan('Running emscripten...\n'));

  // execute shell script
  exec(`
  if [[ :$PATH: != *:"/emsdk":* ]]
  then
\    # use path to emsdk folder, relative to project directory
    BASEDIR="${config.emscripten_path}"
    EMSDK_ENV=$(find "$BASEDIR" -type f -name "emsdk_env.sh")
    source "$EMSDK_ENV"
  fi

  ${commandStr}
  `, { shell: '/bin/bash' }, (error, stdout, stderr) => {
    // check for emcc compile errors
    if (stderr) {
      process.stderr.write(colors.red.bold('EMSCRIPTEN COMPILE ERROR\n'));
      process.stderr.write(colors.white(stderr));
    } else {
      process.stdout.write(stdout);
      insertEventListener(config);
{'fraction_non_alphanumeric': 0.15311996315910661, 'fraction_numerical': 0.001381533502187428, 'mean_word_length': 3.650963597430407, 'pattern_counts': {'":': 2, '<': 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': '4074827', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1347, 'n_words': 358}