<commit_before>// @flow
'use strict';

const path = require('path');
const {spawn} = require('child_process');

const CHILD_SCRIPT = path.join(__dirname, 'child.js');

/*::
type Serializeable =
  | null
  | number
  | boolean
  | string
  | Array<Serializeable>
  | SerializeableObject;

type SerializeableObject = {
  [key: string]: Serializeable,
};
*/

class ProjectorError extends Error {
  /*::
  code: number;
  stderr: string;
  */
  constructor(message, code, stderr) {
    super(message);
    this.code = code;
    this.stderr = stderr;
  }
}

function projector(script /*: string */, exportName /*: string */, opts /*: SerializeableObject */ = {}) {
  return new Promise((resolve, reject) => {
    let child = spawn('node', [CHILD_SCRIPT, script, exportName]);

    let stdout = '';
    child.stdout.setEncoding('utf8');
    child.stdout.on('data', chunk => {
      stdout += chunk.toString();
    });

    let stderr = '';
    child.stderr.setEncoding('utf8');
    child.stderr.on('data', chunk => {
      stderr += chunk.toString();
    });

    child.on('close', code => {
      if (code === 0) {
        resolve(stdout ? JSON.parse(stdout) : {});
      } else {
        reject(new ProjectorError('Process errored with non-zero exit code.', code, stderr));
      }
    });

    child.stdin.setDefaultEncoding('utf8');
    child.stdin.write(JSON.stringify(opts));
    child.stdin.end();
  });
}

projector.ProjectorError = ProjectorError;

module.exports = projector;<commit_msg>Rename ProjectorError to ChildError<commit_after>// @flow
'use strict';

const path = require('path');
const {spawn} = require('child_process');

const CHILD_SCRIPT = path.join(__dirname, 'child.js');

/*::
type Serializeable =
  | null
  | number
  | boolean
  | string
  | Array<Serializeable>
  | SerializeableObject;

type SerializeableObject = {
  [key: string]: Serializeable,
};
*/

class ChildError extends Error {
  /*::
  code: number;
  stderr: string;
  */
  constructor(message, code, stderr) {
    super(message);
    this.code = code;
    this.stderr = stderr;
  }
}

function projector(script /*: string */, exportName /*: string */, opts /*: SerializeableObject */ = {}) {
  return new Promise((resolve, reject) => {
    let child = spawn('node', [CHILD_SCRIPT, script, exportName]);

    let stdout = '';
    child.stdout.setEncoding('utf8');
    child.stdout.on('data', chunk => {
      stdout += chunk.toString();
    });

    let stderr = '';
    child.stderr.setEncoding('utf8');
    child.stderr.on('data', chunk => {
      stderr += chunk.toString();
    });

    child.on('close', code => {
      if (code === 0) {
        resolve(stdout ? JSON.parse(stdout) : {});
      } else {
        reject(new ChildError('Process errored with non-zero exit code.', code, stderr));
      }
    });

    child.stdin.setDefaultEncoding('utf8');
    child.stdin.write(JSON.stringify(opts));
    child.stdin.end();
  });
}

projector.ChildError = ChildError;

module.exports = projector;
{'fraction_non_alphanumeric': 0.17001338688085676, 'fraction_numerical': 0.002677376171352075, 'mean_word_length': 3.1980337078651684, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1927859', 'n_tokens_mistral': 974, 'n_tokens_neox': 950, 'n_words': 198}