All files / app/assets/javascripts/ide/lib files.js

96.77% Statements 30/31
96.15% Branches 25/26
100% Functions 4/4
96.77% Lines 30/31

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    48x 53x   53x                             48x             17x 17x           17x 24x     24x 6x     18x 18x 18x   18x                     18x       18x 5x   13x     18x     17x 28x   28x   28x 22x   22x                         22x       22x 13x   9x         17x              
import { decorateData, sortTree } from '../stores/utils';
 
export const splitParent = (path) => {
  const idx = path.lastIndexOf('/');
 
  return {
    parent: idx >= 0 ? path.substring(0, idx) : null,
    name: idx >= 0 ? path.substring(idx + 1) : path,
  };
};
 
/**
 * Create file objects from a list of file paths.
 *
 * @param {Array} options.data Array of blob paths to parse and create a file tree from.
 * @param {Boolean} options.tempFile Web IDE flag for whether this is a "new" file or not.
 * @param {String} options.content Content to initialize the new blob with.
 * @param {String} options.rawPath Raw path used for the new blob.
 * @param {Object} options.blobData Extra values to initialize each blob with.
 */
export const decorateFiles = ({
  data,
  tempFile = false,
  content = '',
  rawPath = '',
  blobData = {},
}) => {
  const treeList = [];
  const entries = {};
 
  // These mutable variable references end up being exported and used by `createTempEntry`
  let file;
  let parentPath;
 
  const insertParent = (path) => {
    Iif (!path) {
      return null;
    }
    if (entries[path]) {
      return entries[path];
    }
 
    const { parent, name } = splitParent(path);
    const parentFolder = parent && insertParent(parent);
    parentPath = parentFolder && parentFolder.path;
 
    const tree = decorateData({
      id: path,
      name,
      path,
      type: 'tree',
      tempFile,
      changed: tempFile,
      opened: tempFile,
      parentPath,
    });
 
    Object.assign(entries, {
      [path]: tree,
    });
 
    if (parentFolder) {
      parentFolder.tree.push(tree);
    } else {
      treeList.push(tree);
    }
 
    return tree;
  };
 
  data.forEach((path) => {
    const { parent, name } = splitParent(path);
 
    const fileFolder = parent && insertParent(parent);
 
    if (name) {
      parentPath = fileFolder && fileFolder.path;
 
      file = decorateData({
        id: path,
        name,
        path,
        type: 'blob',
        tempFile,
        changed: tempFile,
        content,
        rawPath,
        parentPath,
        ...blobData,
      });
 
      Object.assign(entries, {
        [path]: file,
      });
 
      if (fileFolder) {
        fileFolder.tree.push(file);
      } else {
        treeList.push(file);
      }
    }
  });
 
  return {
    entries,
    treeList: sortTree(treeList),
    file,
    parentPath,
  };
};