all files / src/ fs-utils.js

41.67% Statements 10/24
25% Branches 1/4
25% Functions 2/8
41.67% Lines 10/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                        32×   32× 32×                    
/* eslint no-use-before-define: 0 */
import Rx from 'rx';
import path from 'path';
import fsp from 'fs-promise';
 
function expanddir(dir) {
  let fullDirPath = path.resolve(dir);
  let paths = fsp.readdir(dir);
 
  return Rx.Observable.fromPromise(paths)
    .flatMap(x => x)
    .map(filename => {
      let filepath = path.join(fullDirPath, filename);
 
      return expand(filepath);
    })
    .flatMap(x => x);
}
 
function expand(filepath) {
  let fullPath = path.resolve(filepath);
  let p = fsp.stat(fullPath);
 
  return Rx.Observable.fromPromise(p)
    .flatMap(stat => {
      if (stat.isDirectory()) {
        return expanddir(fullPath);
      } else {
        return Rx.Observable.just(fullPath);
      }
    });
}
 
function ensureDir(dir) {
  return fsp.exists(dir)
    .then(exists => {
      if (Eexists) {
        return null;
      }
 
      return fsp.mkdir(dir);
    });
}
 
export default {
  expand,
  ensureDir
};