|
@@ -1,5 +1,5 @@
|
|
|
import fsp from 'fs/promises';
|
|
import fsp from 'fs/promises';
|
|
|
-import { sep } from 'path';
|
|
|
|
|
|
|
+import { sep, relative } from 'path';
|
|
|
|
|
|
|
|
interface TreeFileType {
|
|
interface TreeFileType {
|
|
|
type: 'file',
|
|
type: 'file',
|
|
@@ -19,19 +19,20 @@ export type TreeTypeArray = TreeType[];
|
|
|
|
|
|
|
|
type VoidOrVoidArray = void | VoidOrVoidArray[];
|
|
type VoidOrVoidArray = void | VoidOrVoidArray[];
|
|
|
|
|
|
|
|
-export const treeDir = async (path: string): Promise<TreeTypeArray> => {
|
|
|
|
|
|
|
+export const treeDir = async (rootPath: string): Promise<TreeTypeArray> => {
|
|
|
const tree: TreeTypeArray = [];
|
|
const tree: TreeTypeArray = [];
|
|
|
|
|
|
|
|
const walk = async (dir: string, node: TreeTypeArray): Promise<VoidOrVoidArray> => {
|
|
const walk = async (dir: string, node: TreeTypeArray): Promise<VoidOrVoidArray> => {
|
|
|
const promises: Array<Promise<VoidOrVoidArray>> = [];
|
|
const promises: Array<Promise<VoidOrVoidArray>> = [];
|
|
|
for await (const child of await fsp.opendir(dir)) {
|
|
for await (const child of await fsp.opendir(dir)) {
|
|
|
const childFullPath = child.parentPath + sep + child.name;
|
|
const childFullPath = child.parentPath + sep + child.name;
|
|
|
|
|
+ const childRelativeToRoot = relative(rootPath, childFullPath);
|
|
|
|
|
|
|
|
if (child.isDirectory()) {
|
|
if (child.isDirectory()) {
|
|
|
const newNode: TreeDirectoryType = {
|
|
const newNode: TreeDirectoryType = {
|
|
|
type: 'directory',
|
|
type: 'directory',
|
|
|
name: child.name,
|
|
name: child.name,
|
|
|
- path: childFullPath,
|
|
|
|
|
|
|
+ path: childRelativeToRoot,
|
|
|
children: []
|
|
children: []
|
|
|
};
|
|
};
|
|
|
node.push(newNode);
|
|
node.push(newNode);
|
|
@@ -42,7 +43,7 @@ export const treeDir = async (path: string): Promise<TreeTypeArray> => {
|
|
|
const newNode: TreeFileType = {
|
|
const newNode: TreeFileType = {
|
|
|
type: 'file',
|
|
type: 'file',
|
|
|
name: child.name,
|
|
name: child.name,
|
|
|
- path: childFullPath
|
|
|
|
|
|
|
+ path: childRelativeToRoot
|
|
|
};
|
|
};
|
|
|
node.push(newNode);
|
|
node.push(newNode);
|
|
|
continue;
|
|
continue;
|
|
@@ -51,7 +52,7 @@ export const treeDir = async (path: string): Promise<TreeTypeArray> => {
|
|
|
return Promise.all(promises);
|
|
return Promise.all(promises);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- await walk(path, tree);
|
|
|
|
|
|
|
+ await walk(rootPath, tree);
|
|
|
|
|
|
|
|
return tree;
|
|
return tree;
|
|
|
};
|
|
};
|