summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/stores/workers/files_decorator_worker.js
blob: a16732769000efe1894db8c35eff834469937ae2 (plain)
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
import { viewerInformationForPath } from '~/vue_shared/components/content_viewer/lib/viewer_utils';
import { decorateData, sortTree } from '../utils';

self.addEventListener('message', e => {
  const { data, projectId, branchId, tempFile = false, content = '', base64 = false } = e.data;

  const treeList = [];
  let file;
  const entries = data.reduce((acc, path) => {
    const pathSplit = path.split('/');
    const blobName = pathSplit.pop().trim();

    if (pathSplit.length > 0) {
      pathSplit.reduce((pathAcc, folderName) => {
        const parentFolder = acc[pathAcc[pathAcc.length - 1]];
        const folderPath = `${parentFolder ? `${parentFolder.path}/` : ''}${folderName}`;
        const foundEntry = acc[folderPath];

        if (!foundEntry) {
          const tree = decorateData({
            projectId,
            branchId,
            id: folderPath,
            name: folderName,
            path: folderPath,
            url: `/${projectId}/tree/${branchId}/${folderPath}/`,
            type: 'tree',
            parentTreeUrl: parentFolder ? parentFolder.url : `/${projectId}/tree/${branchId}/`,
            tempFile,
            changed: tempFile,
            opened: tempFile,
          });

          Object.assign(acc, {
            [folderPath]: tree,
          });

          if (parentFolder) {
            parentFolder.tree.push(tree);
          } else {
            treeList.push(tree);
          }

          pathAcc.push(tree.path);
        } else {
          pathAcc.push(foundEntry.path);
        }

        return pathAcc;
      }, []);
    }

    if (blobName !== '') {
      const fileFolder = acc[pathSplit.join('/')];
      file = decorateData({
        projectId,
        branchId,
        id: path,
        name: blobName,
        path,
        url: `/${projectId}/blob/${branchId}/${path}`,
        type: 'blob',
        parentTreeUrl: fileFolder ? fileFolder.url : `/${projectId}/blob/${branchId}`,
        tempFile,
        changed: tempFile,
        content,
        base64,
        previewMode: viewerInformationForPath(blobName),
      });

      Object.assign(acc, {
        [path]: file,
      });

      if (fileFolder) {
        fileFolder.tree.push(file);
      } else {
        treeList.push(file);
      }
    }

    return acc;
  }, {});

  self.postMessage({
    entries,
    treeList: sortTree(treeList),
    file,
  });
});