feat: treeToResourcePaths
This commit is contained in:
parent
c5a928b7e7
commit
0ee7b771da
|
@ -4,7 +4,7 @@ import {promisify} from 'util';
|
||||||
import {Model, Sequelize, Types} from '@latus/db/server';
|
import {Model, Sequelize, Types} from '@latus/db/server';
|
||||||
import glob from 'glob';
|
import glob from 'glob';
|
||||||
|
|
||||||
import {pathsToTree} from '../tree-utils';
|
import {pathsToTree} from '../tree';
|
||||||
|
|
||||||
export default () => class Project extends Model {
|
export default () => class Project extends Model {
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {promisify} from 'util';
|
||||||
|
|
||||||
import {decorateWithLatus, gatherWithLatus} from '@latus/core';
|
import {decorateWithLatus, gatherWithLatus} from '@latus/core';
|
||||||
|
|
||||||
import {treeToPaths} from '../tree-utils';
|
import {treeToPaths} from '../tree';
|
||||||
|
|
||||||
const readFile = promisify(fs.readFile).bind(fs);
|
const readFile = promisify(fs.readFile).bind(fs);
|
||||||
const stat = promisify(fs.stat).bind(fs);
|
const stat = promisify(fs.stat).bind(fs);
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
import {
|
import {
|
||||||
addPathToTree,
|
addPathToTree,
|
||||||
removePathFromTree,
|
removePathFromTree,
|
||||||
} from '../tree-utils';
|
} from '../tree';
|
||||||
|
|
||||||
export const projectsSelector = (state) => state.projects;
|
export const projectsSelector = (state) => state.projects;
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import natsort from 'natsort';
|
||||||
import {
|
import {
|
||||||
getFlatDataFromTree,
|
getFlatDataFromTree,
|
||||||
getTreeFromFlatData,
|
getTreeFromFlatData,
|
||||||
// removeNodeAtPath,
|
walk,
|
||||||
} from 'react-sortable-tree';
|
} from 'react-sortable-tree';
|
||||||
|
|
||||||
const getKey = ({path}) => path;
|
const getKey = ({path}) => path;
|
||||||
|
@ -16,16 +16,6 @@ const getParentKey = ({path}) => {
|
||||||
|
|
||||||
export const pathToNode = (path) => ({path, label: basename(path)});
|
export const pathToNode = (path) => ({path, label: basename(path)});
|
||||||
|
|
||||||
export const treeToPaths = (tree) => (
|
|
||||||
getFlatDataFromTree({
|
|
||||||
getNodeKey: ({node: {path}}) => path,
|
|
||||||
ignoreCollapsed: false,
|
|
||||||
treeData: tree,
|
|
||||||
})
|
|
||||||
.map(({path}) => path.pop())
|
|
||||||
.sort(natsort({insensitive: true}))
|
|
||||||
);
|
|
||||||
|
|
||||||
export const pathsToTree = (paths) => (
|
export const pathsToTree = (paths) => (
|
||||||
getTreeFromFlatData({
|
getTreeFromFlatData({
|
||||||
flatData: paths
|
flatData: paths
|
||||||
|
@ -37,6 +27,32 @@ export const pathsToTree = (paths) => (
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const treeToResourcePaths = (tree) => {
|
||||||
|
const files = [];
|
||||||
|
walk({
|
||||||
|
callback: ({node: {children, path}}) => {
|
||||||
|
if (!children) {
|
||||||
|
files.push(path);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getNodeKey: ({node: {path}}) => path,
|
||||||
|
ignoreCollapsed: false,
|
||||||
|
treeData: tree,
|
||||||
|
});
|
||||||
|
return files
|
||||||
|
.sort(natsort({insensitive: true}));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const treeToPaths = (tree) => (
|
||||||
|
getFlatDataFromTree({
|
||||||
|
getNodeKey: ({node: {path}}) => path,
|
||||||
|
ignoreCollapsed: false,
|
||||||
|
treeData: tree,
|
||||||
|
})
|
||||||
|
.map(({path}) => path.pop())
|
||||||
|
.sort(natsort({insensitive: true}))
|
||||||
|
);
|
||||||
|
|
||||||
export const addPathToTree = (tree, path) => {
|
export const addPathToTree = (tree, path) => {
|
||||||
const paths = treeToPaths(tree);
|
const paths = treeToPaths(tree);
|
||||||
const parts = path.split('/');
|
const parts = path.split('/');
|
|
@ -4,8 +4,9 @@ import {
|
||||||
addPathToTree,
|
addPathToTree,
|
||||||
pathsToTree,
|
pathsToTree,
|
||||||
removePathFromTree,
|
removePathFromTree,
|
||||||
|
treeToResourcePaths,
|
||||||
treeToPaths,
|
treeToPaths,
|
||||||
} from '../src/tree-utils';
|
} from '../src/tree';
|
||||||
|
|
||||||
it('can get a tree from paths', () => {
|
it('can get a tree from paths', () => {
|
||||||
expect(pathsToTree(['/foo', '/bar', '/bar/baz.txt', '/boo']))
|
expect(pathsToTree(['/foo', '/bar', '/bar/baz.txt', '/boo']))
|
||||||
|
@ -18,6 +19,17 @@ it('can get a tree from paths', () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can get resource paths from a tree', () => {
|
||||||
|
expect(treeToResourcePaths([
|
||||||
|
{path: '/foo', label: 'foo'},
|
||||||
|
{path: '/bar', label: 'bar', children: [
|
||||||
|
{path: '/bar/baz.txt', label: 'baz.txt'},
|
||||||
|
]},
|
||||||
|
{path: '/boo', label: 'boo'},
|
||||||
|
]))
|
||||||
|
.to.deep.equal(['/bar/baz.txt', '/boo', '/foo']);
|
||||||
|
});
|
||||||
|
|
||||||
it('can get paths from a tree', () => {
|
it('can get paths from a tree', () => {
|
||||||
expect(treeToPaths([
|
expect(treeToPaths([
|
||||||
{path: '/foo', label: 'foo'},
|
{path: '/foo', label: 'foo'},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user