<commit_before>const defaultNodeFs = require('fs');
const {join} = require('path');
const EventEmitter = require('events');
const async = require('async');

const mathUtils = require('./mathUtils');

const DEFAULT_STORAGE_DIR_NAME = '.slug-storage';
const DEFAULT_LOOK_FOR_INTERVAL = 5 * 1000;

class DevicesManager extends EventEmitter {
  constructor({
    devicesPath,
    lookupInterval = DEFAULT_LOOK_FOR_INTERVAL,
    storageDirName = DEFAULT_STORAGE_DIR_NAME,
    fs = defaultNodeFs
  } = {}) {
    super();

    if (!devicesPath) {
      throw new Error('No "devicesPath" parameter specified');
    }

    //TODO logger
    //console.log(`MergedFs devices path: ${devsPath}`);

    this.devicesPath = devicesPath;
    this.storageDirName = storageDirName;
    this.fs = fs;

    this.devices = [];
    this.isInitLookup = true;

    this._lookupDevices();
    this._lookupDevicesInterval = setInterval(() => this._lookupDevices(), lookupInterval);
    this._asyncFilterDirs = this._asyncFilterDirs.bind(this);
    this._asyncFilterNonExisting = this._asyncFilterNonExisting.bind(this);
  }

  _asyncFilterDirs(paths, done) {
    return this.fs.stat(
      paths,
      (err, stat) => done(null, !err && stat.isDirectory())
    );
  }

  _asyncFilterNonExisting(paths, done) {
    return this.fs.stat(
      paths,
      err => done(null, err && err.code === 'ENOENT')
    );
  }

  _lookupDevices() {
    //console.log('Look up devices start...');
    async.waterfall([
      done => this.fs.readdir(this.devicesPath, (err, files) => {
        if (err) {
          return done(new Error(`Cannot read devices directory "${this.devicesPath}": ${err}`));
        }
        return done(null, files.map(fileName => join(this.devicesPath, fileName)));
      }),
      (filePaths, done) => async.filter(filePaths, this._asyncFilterDirs, (err, dirs) => {
        if (!dirs.length) {<commit_msg>chore: use common codes<commit_after>const defaultNodeFs = require('fs');
const {join} = require('path');
const EventEmitter = require('events');
const async = require('async');

const mathUtils = require('./mathUtils');
const {CODES} = require('./errorHelpers');

const DEFAULT_STORAGE_DIR_NAME = '.slug-storage';
const DEFAULT_LOOK_FOR_INTERVAL = 5 * 1000;

class DevicesManager extends EventEmitter {
  constructor({
    devicesPath,
    lookupInterval = DEFAULT_LOOK_FOR_INTERVAL,
    storageDirName = DEFAULT_STORAGE_DIR_NAME,
    fs = defaultNodeFs
  } = {}) {
    super();

    if (!devicesPath) {
      throw new Error('No "devicesPath" parameter specified');
    }

    //TODO logger
    //console.log(`MergedFs devices path: ${devsPath}`);

    this.devicesPath = devicesPath;
    this.storageDirName = storageDirName;
    this.fs = fs;

    this.devices = [];
    this.isInitLookup = true;

    this._lookupDevices();
    this._lookupDevicesInterval = setInterval(() => this._lookupDevices(), lookupInterval);
    this._asyncFilterDirs = this._asyncFilterDirs.bind(this);
    this._asyncFilterNonExisting = this._asyncFilterNonExisting.bind(this);
  }

  _asyncFilterDirs(paths, done) {
    return this.fs.stat(
      paths,
      (err, stat) => done(null, !err && stat.isDirectory())
    );
  }

  _asyncFilterNonExisting(paths, done) {
    return this.fs.stat(
      paths,
      err => done(null, err && err.code === CODES.ENOENT)
    );
  }

  _lookupDevices() {
    //console.log('Look up devices start...');
    async.waterfall([
      done => this.fs.readdir(this.devicesPath, (err, files) => {
        if (err) {
          return done(new Error(`Cannot read devices directory "${this.devicesPath}": ${err}`));
        }
        return done(null, files.map(fileName => join(this.devicesPath, fileName)));
      }),
      (filePaths, done) => async.filter(filePaths, this._asyncFilterDirs, (err, dirs) => {
        if (!dirs.length) {
{'fraction_non_alphanumeric': 0.14616384915474642, 'fraction_numerical': 0.002600780234070221, 'mean_word_length': 3.425776754890679, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 19, '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': '2603850', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1200, 'n_words': 246}