All files / src/fs/operations file-operations.js

100% Statements 34/34
100% Branches 18/18
100% Functions 5/5
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 1131x 1x 1x 1x 1x                 1x 178x 143x   143x   35x                 1x 30x 2x         28x 8x         20x                       1x 8x                   1x 17x 4x         13x   13x 1x         12x   5x   5x     12x                     1x 7x 2x         5x 2x         3x    
import * as PathUtil from 'fs/util/path-util';
import * as BaseOp from 'fs/operations/base-operations';
import { isFile } from 'fs/util/file-util';
import { hasDirectory } from 'fs/operations/directory-operations';
import { makeError, fsErrorType } from 'fs/fs-error';
 
/**
 * Checks whether a file exists
 * @param  {Map}     fs       file system
 * @param  {string}  dirPath  directory of the file to check for existence
 * @param  {string}  fileName file name to check for existence
 * @return {Boolean}          true, if the file exists
 */
export const hasFile = (fs, filePath) => {
  if (fs.has(filePath)) {
    const possibleFile = fs.get(filePath);
 
    return isFile(possibleFile);
  }
  return false;
};
 
/**
 * Get a file from the file system
 * @param  {Map}    fs       file system
 * @param  {string} filePath path to file to read
 * @return {object}          file system or an error
 */
export const readFile = (fs, filePath) => {
  if (hasDirectory(fs, filePath)) {
    return {
      err: makeError(fsErrorType.IS_A_DIRECTORY)
    };
  }
 
  if (!hasFile(fs, filePath)) {
    return {
      err: makeError(fsErrorType.NO_SUCH_FILE)
    };
  }
 
  return {
    file: fs.get(filePath)
  };
};
 
/**
 * Write a new file to the file system
 * @param  {Map}     fs                            file system
 * @param  {string}  filePath                      path to new file
 * @param  {Map}     file                          the new file
 * @return {object}                                file system or an error
 */
export const writeFile = (fs, filePath, file) => {
  return BaseOp.add(fs, filePath, file);
};
 
/**
 * Copies a file from a source directory to a destination directory
 * @param  {Map}    fs           file system
 * @param  {string} sourcePath   path to source file (to copy from)
 * @param  {string} destPath     path to destination file (to copy to)
 * @return {object}              file system or an error
 */
export const copyFile = (fs, sourcePath, destPath) => {
  if (!hasFile(fs, sourcePath)) {
    return {
      err: makeError(fsErrorType.NO_SUCH_FILE, 'Source file does not exist')
    };
  }
 
  const pathParent = PathUtil.getPathParent(destPath);
 
  if (!hasDirectory(fs, pathParent)) {
    return {
      err: makeError(fsErrorType.NO_SUCH_DIRECTORY, 'Destination directory does not exist')
    };
  }
 
  if (hasDirectory(fs, destPath)) {
    // Copying file to directory without specifying the filename explicitly
    const sourceFileName = PathUtil.getLastPathPart(sourcePath);
 
    destPath = destPath === '/' ? `/${sourceFileName}` : `${destPath}/${sourceFileName}`;
  }
 
  return {
    fs: fs.set(destPath, fs.get(sourcePath))
  };
};
 
/**
 * Removes a file from the file system
 * @param  {Map}    fs       file system
 * @param  {string} filePath path to the file to delete
 * @return {object}          file system or an error
 */
export const deleteFile = (fs, filePath) => {
  if (hasDirectory(fs, filePath)) {
    return {
      err: makeError(fsErrorType.IS_A_DIRECTORY)
    };
  }
 
  if (!hasFile(fs, filePath)) {
    return {
      err: makeError(fsErrorType.NO_SUCH_FILE)
    };
  }
 
  return BaseOp.remove(fs, filePath);
};