@xiping/node-utils 0.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/index.d.ts +51 -0
- package/lib/index.js +83 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present, xiping.wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a node in a directory tree structure
|
|
3
|
+
* @interface Tree
|
|
4
|
+
* @property {string} id - Unique identifier for the node (full path)
|
|
5
|
+
* @property {string} label - Display name of the file or directory
|
|
6
|
+
* @property {Tree[]} [children] - Optional array of child nodes for directories
|
|
7
|
+
*/
|
|
8
|
+
export interface Tree {
|
|
9
|
+
id: string;
|
|
10
|
+
label: string;
|
|
11
|
+
children?: Tree[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Recursively builds a tree structure from a directory
|
|
15
|
+
* @param {string} dirPath - The absolute path to the directory to process
|
|
16
|
+
* @param {string} [basePath] - The base path for generating relative paths (defaults to dirPath)
|
|
17
|
+
* @returns {Tree} A tree structure representing the directory hierarchy
|
|
18
|
+
* @throws Will throw an error if directory access fails
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Create a tree from a directory
|
|
22
|
+
* const tree = buildDirectoryTree('/path/to/directory');
|
|
23
|
+
*
|
|
24
|
+
* // Structure of returned tree:
|
|
25
|
+
* // {
|
|
26
|
+
* // id: '/path/to/directory',
|
|
27
|
+
* // label: 'directory',
|
|
28
|
+
* // children: [
|
|
29
|
+
* // { id: '/path/to/directory/file.txt', label: 'file.txt' },
|
|
30
|
+
* // { id: '/path/to/directory/subdir', label: 'subdir', children: [...] }
|
|
31
|
+
* // ]
|
|
32
|
+
* // }
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildDirectoryTree(dirPath: string, basePath?: string): Tree;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the parent directory path of a given directory path
|
|
37
|
+
* Cross-platform compatible (Windows, macOS, Linux)
|
|
38
|
+
* @param {string} dirPath - The path to get the parent directory from
|
|
39
|
+
* @returns {string} The parent directory path
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // On Unix-like systems (Mac/Linux)
|
|
43
|
+
* getParentDirectory('/Users/documents/folder') // returns '/Users/documents'
|
|
44
|
+
*
|
|
45
|
+
* // On Windows
|
|
46
|
+
* getParentDirectory('C:\\Users\\Documents\\folder') // returns 'C:\\Users\\Documents'
|
|
47
|
+
*
|
|
48
|
+
* // Works with both forward and backward slashes
|
|
49
|
+
* getParentDirectory('C:/Users/Documents/folder') // returns 'C:/Users/Documents'
|
|
50
|
+
*/
|
|
51
|
+
export declare function getParentDirectory(dirPath: string): string;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { join, dirname, normalize } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Recursively builds a tree structure from a directory
|
|
5
|
+
* @param {string} dirPath - The absolute path to the directory to process
|
|
6
|
+
* @param {string} [basePath] - The base path for generating relative paths (defaults to dirPath)
|
|
7
|
+
* @returns {Tree} A tree structure representing the directory hierarchy
|
|
8
|
+
* @throws Will throw an error if directory access fails
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Create a tree from a directory
|
|
12
|
+
* const tree = buildDirectoryTree('/path/to/directory');
|
|
13
|
+
*
|
|
14
|
+
* // Structure of returned tree:
|
|
15
|
+
* // {
|
|
16
|
+
* // id: '/path/to/directory',
|
|
17
|
+
* // label: 'directory',
|
|
18
|
+
* // children: [
|
|
19
|
+
* // { id: '/path/to/directory/file.txt', label: 'file.txt' },
|
|
20
|
+
* // { id: '/path/to/directory/subdir', label: 'subdir', children: [...] }
|
|
21
|
+
* // ]
|
|
22
|
+
* // }
|
|
23
|
+
*/
|
|
24
|
+
export function buildDirectoryTree(dirPath, basePath = dirPath) {
|
|
25
|
+
const stats = statSync(dirPath);
|
|
26
|
+
const relativePath = dirPath.replace(basePath, "").replace(/^\//, "") || "/";
|
|
27
|
+
const fileName = dirPath.split("/").pop() || "/";
|
|
28
|
+
const tree = {
|
|
29
|
+
id: dirPath,
|
|
30
|
+
label: fileName,
|
|
31
|
+
};
|
|
32
|
+
if (stats.isDirectory()) {
|
|
33
|
+
try {
|
|
34
|
+
const items = readdirSync(dirPath);
|
|
35
|
+
if (items.length > 0) {
|
|
36
|
+
tree.children = items
|
|
37
|
+
.map((item) => {
|
|
38
|
+
const itemPath = join(dirPath, item);
|
|
39
|
+
try {
|
|
40
|
+
return buildDirectoryTree(itemPath, basePath);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Error processing ${itemPath}:`, error);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
.filter((item) => item !== null);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error(`Error reading directory ${dirPath}:`, error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
console.log("tree=", tree);
|
|
55
|
+
return tree;
|
|
56
|
+
}
|
|
57
|
+
// buildDirectoryTree(
|
|
58
|
+
// "/Users/xipingwang/Documents/EF-code/e1-ai-assistant-it-web/src",
|
|
59
|
+
// );
|
|
60
|
+
/**
|
|
61
|
+
* Gets the parent directory path of a given directory path
|
|
62
|
+
* Cross-platform compatible (Windows, macOS, Linux)
|
|
63
|
+
* @param {string} dirPath - The path to get the parent directory from
|
|
64
|
+
* @returns {string} The parent directory path
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* // On Unix-like systems (Mac/Linux)
|
|
68
|
+
* getParentDirectory('/Users/documents/folder') // returns '/Users/documents'
|
|
69
|
+
*
|
|
70
|
+
* // On Windows
|
|
71
|
+
* getParentDirectory('C:\\Users\\Documents\\folder') // returns 'C:\\Users\\Documents'
|
|
72
|
+
*
|
|
73
|
+
* // Works with both forward and backward slashes
|
|
74
|
+
* getParentDirectory('C:/Users/Documents/folder') // returns 'C:/Users/Documents'
|
|
75
|
+
*/
|
|
76
|
+
export function getParentDirectory(dirPath) {
|
|
77
|
+
// Normalize the path to handle different path formats
|
|
78
|
+
const normalizedPath = normalize(dirPath);
|
|
79
|
+
// Get the parent directory using dirname
|
|
80
|
+
const parentPath = dirname(normalizedPath);
|
|
81
|
+
// If we're already at the root, return the normalized root
|
|
82
|
+
return normalizedPath === parentPath ? parentPath : parentPath;
|
|
83
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xiping/node-utils",
|
|
3
|
+
"version": "0.0.30",
|
|
4
|
+
"description": "node-utils",
|
|
5
|
+
"author": "The-End-Hero <527409987@qq.com>",
|
|
6
|
+
"homepage": "https://github.com/The-End-Hero/xiping#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"directories": {
|
|
10
|
+
"lib": "lib",
|
|
11
|
+
"test": "__tests__"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/The-End-Hero/wang-ping.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
22
|
+
"build": "tsc"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/The-End-Hero/wang-ping/issues"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "80bce04180eb3e840bba53584d44b988e3ef8fda",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "^22.10.1",
|
|
33
|
+
"tslib": "^2.8.1",
|
|
34
|
+
"typescript": "^5.7.2"
|
|
35
|
+
}
|
|
36
|
+
}
|