summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/helpers.js
blob: 3ce9c9fcda112ba30df4df70ef75a2559d5bc6e0 (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
import * as pathUtils from 'path';
import { decorateData } from '~/ide/stores/utils';
import state from '~/ide/stores/state';
import commitState from '~/ide/stores/modules/commit/state';
import mergeRequestsState from '~/ide/stores/modules/merge_requests/state';
import pipelinesState from '~/ide/stores/modules/pipelines/state';
import branchesState from '~/ide/stores/modules/branches/state';
import fileTemplatesState from '~/ide/stores/modules/file_templates/state';

export const resetStore = store => {
  const newState = {
    ...state(),
    commit: commitState(),
    mergeRequests: mergeRequestsState(),
    pipelines: pipelinesState(),
    branches: branchesState(),
    fileTemplates: fileTemplatesState(),
  };
  store.replaceState(newState);
};

export const file = (name = 'name', id = name, type = '', parent = null) =>
  decorateData({
    id,
    type,
    icon: 'icon',
    url: 'url',
    name,
    path: parent ? `${parent.path}/${name}` : name,
    parentPath: parent ? parent.path : '',
    lastCommit: {},
  });

export const createEntriesFromPaths = paths =>
  paths
    .map(path => ({
      name: pathUtils.basename(path),
      dir: pathUtils.dirname(path),
      ext: pathUtils.extname(path),
    }))
    .reduce((entries, path, idx) => {
      const { name } = path;
      const parent = path.dir ? entries[path.dir] : null;
      const type = path.ext ? 'blob' : 'tree';

      const entry = file(name, (idx + 1).toString(), type, parent);

      return {
        [entry.path]: entry,
        ...entries,
      };
    }, {});