# all files / src/util/ fs.js

100% Statements 19/19
100% Branches 6/6
100% Functions 3/3
100% Lines 14/14
1 branch Ignored

    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
    50
    51
    52
    53
    54
    551×
    1×

    1×

    1×
    50×
    50×
    24×

    26×
    25×

    1×

    1×
    1×
    1×

     import fs from 'fs';
    import path from 'path';
    // import fse from 'fs-extra';
    // import temp from 'temp';
    // import denodeify from 'denodeify';

    const rootPath = process.cwd();
    // const mkdir    = denodeify(fs.mkdir);
    // const mkTmpDir = denodeify(temp.mkdir);

    /*
     Node deprecated existsSync so this is a simple
     helper function wrapping try/catch around the new
     recommended approach of accessSync
     https://nodejs.org/api/fs.html#fs_fs_existssync_path
     */
    export const fileExists = (filename) => {
      try {
        fs.accessSync(filename);
        return true;
      } catch (e) {
        if (e.code === 'ENOENT') {
          return false;
        } else {
          throw e;
        }
      }
    };

    export const readFile = (filename) => {
      const filePath = path.join(rootPath, filename);
      return fs.readFileSync(filePath, 'utf8');
    };

    // Promise based fs helpers
    // currently not being used after blueprint refactor.  Keeping in case
    // I want to use later...

    // export const dirExists = (dirPath) => {
      // return new Promise(resolve => {
        // fse.exists(dirPath, resolve);
      // });
    // };

    // export const mkTmpDirIn = (dirPath) => {
      // return dirExists(dirPath).then(doesExist => {
        // if (!doesExist) {
          // return mkdir(dirPath);
        // }
      // }).then(() => {
        // return mkTmpDir({ dir: dirPath});
      // });
    // };