2020-01-01 13:55:28 +05:30
|
|
|
import * as pathUtils from 'path';
|
2021-01-29 00:20:46 +05:30
|
|
|
import { commitActionTypes } from '~/ide/constants';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { decorateData } from '~/ide/stores/utils';
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
export const file = (name = 'name', id = name, type = '', parent = null) =>
|
|
|
|
decorateData({
|
|
|
|
id,
|
|
|
|
type,
|
|
|
|
icon: 'icon',
|
|
|
|
name,
|
|
|
|
path: parent ? `${parent.path}/${name}` : name,
|
|
|
|
parentPath: parent ? parent.path : '',
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createEntriesFromPaths = (paths) =>
|
2020-01-01 13:55:28 +05:30
|
|
|
paths
|
2021-03-08 18:12:59 +05:30
|
|
|
.map((path) => ({
|
2020-01-01 13:55:28 +05:30
|
|
|
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,
|
|
|
|
};
|
|
|
|
}, {});
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createTriggerChangeAction = (payload) => ({
|
2021-01-29 00:20:46 +05:30
|
|
|
type: 'triggerFilesChange',
|
|
|
|
...(payload ? { payload } : {}),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const createTriggerRenamePayload = (path, newPath) => ({
|
|
|
|
type: commitActionTypes.move,
|
|
|
|
path,
|
|
|
|
newPath,
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
export const createTriggerUpdatePayload = (path) => ({
|
|
|
|
type: commitActionTypes.update,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
export const createTriggerRenameAction = (path, newPath) =>
|
|
|
|
createTriggerChangeAction(createTriggerRenamePayload(path, newPath));
|