aboutsummaryrefslogblamecommitdiffhomepage
path: root/src/PursLoader/FS.js
blob: 1a7f5b08a8f38ce96e34064688fd746b0b2c223e (plain) (tree)



































                                                                             
'use strict';

// module PursLoader.FS

var fs = require('fs');

var async = require('async');

function writeFileUtf8Fn(filepath, contents, errback, callback) {
  return function(){
    fs.writeFile(filepath, contents, function(error){
      if (error) errback(error)();
      else callback()();
    });
  };
}

function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) {
  return function(){
    function findFile(filepath, callback) {
      fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){
        if (error) callback(false);
        else callback(regex.test(result));
      });
    }

    async.detect(filepaths, findFile, function(result){
      if (!result) callback(nothing)();
      else callback(just(result))();
    });
  };
}

exports.writeFileUtf8Fn = writeFileUtf8Fn;

exports.findFileUtf8Fn = findFileUtf8Fn;