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

100% Statements 28/28
100% Branches 26/26
100% Functions 2/2
100% Lines 24/24

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 751x 1x 1x 1x 1x                     69x 27x 4x         23x   45x 45x 3x           42x 6x             14x   14x                       19x 19x 1x         18x 18x   18x 4x         14x        
import * as GlobUtil from 'fs/util/glob-util';
import * as DirOp from 'fs/operations/directory-operations';
import * as FileOp from 'fs/operations/file-operations';
import * as PathUtil from 'fs/util/path-util';
import { makeError, fsErrorType } from 'fs/fs-error';
 
/**
 * Adds a file or directory to a path
 * @param {Map}     fs                     file system
 * @param {string}  pathToAdd              path to add the file or directory to
 * @param {string}  fsElementToAdd         file or directory map
 * @param {Boolean} [addParentPaths=false] true, if path parent directories should
 *                                         be made (if they don't exist)
 * @return {object}                        file system or error
 */
export const add = (fs, pathToAdd, fsElementToAdd, addParentPaths = false) => {
  if (fs.has(pathToAdd)) {
    return {
      err: makeError(fsErrorType.FILE_OR_DIRECTORY_EXISTS)
    };
  }
 
  const parentPaths = PathUtil.getPathBreadCrumbs(pathToAdd).slice(0, -1);
 
  for (const parentPath of parentPaths) {
    if (FileOp.hasFile(fs, parentPath)) {
      return {
        err: makeError(fsErrorType.NOT_A_DIRECTORY,
          `Cannot add path to a file: ${parentPath}`)
      };
    }
 
    if (!fs.has(parentPath) && !addParentPaths) {
      return {
        err: makeError(fsErrorType.NO_SUCH_DIRECTORY,
          `Parent directory does not exist: ${parentPath}`)
      };
    }
  }
 
  const addedDirectoryFs = fs.set(pathToAdd, fsElementToAdd);
 
  return {
    fs: addParentPaths ? DirOp.fillGaps(addedDirectoryFs) : addedDirectoryFs
  };
};
 
/**
 * Removes a file or directory from a path
 * @param  {Map}     fs                                  file system
 * @param  {string}  pathToRemove                        removes the path
 * @param  {Boolean} [isNonEmptyDirectoryRemovable=true] true if non-empty paths can be removed
 * @return {object}                                      file system or error
 */
export const remove = (fs, pathToRemove, isNonEmptyDirectoryRemovable = true) => {
  if (!fs.has(pathToRemove)) {
    return {
      err: makeError(fsErrorType.NO_SUCH_FILE_OR_DIRECTORY)
    };
  }
 
  const childPathPattern = pathToRemove === '/' ? '/**' : `${pathToRemove}/**`;
  const childPaths = GlobUtil.globPaths(fs, childPathPattern);
 
  if (!isNonEmptyDirectoryRemovable && !childPaths.isEmpty()) {
    return {
      err: makeError(fsErrorType.DIRECTORY_NOT_EMPTY)
    };
  }
 
  return {
    fs: fs.removeAll(childPaths.concat(pathToRemove))
  };
};