directory-tree 3.2.1 → 3.3.1

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/README.md CHANGED
@@ -178,7 +178,7 @@ const callback: DirectoryTreeCallback = (
178
178
  item: DirectoryTree,
179
179
  path: string
180
180
  ) => {
181
- item.custom.id = createHash('sha1').update(path).digest('base64');
181
+ item.custom = {id: createHash('sha1').update(path).digest('base64')};
182
182
  };
183
183
 
184
184
  const dirTree: DirectoryTree & { id?: string } = directoryTree(
package/index.d.ts CHANGED
@@ -1,34 +1,48 @@
1
- import { Stats } from 'fs';
1
+ import { Stats } from "fs";
2
2
 
3
- declare function directoryTree(
3
+ interface IObj {
4
+ [key: string]: any;
5
+ }
6
+
7
+ declare function directoryTree<
8
+ TCustomFile extends IObj = IObj,
9
+ TCustomDir extends IObj = IObj,
10
+ TCustomResult = TCustomFile & TCustomDir
11
+ >(
4
12
  path: string,
5
- options ? : directoryTree.DirectoryTreeOptions,
6
- onEachFile ? : directoryTree.DirectoryTreeCallback,
7
- onEachDirectory ? : directoryTree.DirectoryTreeCallback,
8
- ): directoryTree.DirectoryTree;
13
+ options?: directoryTree.DirectoryTreeOptions,
14
+ onEachFile?: directoryTree.DirectoryTreeCallback<TCustomFile>,
15
+ onEachDirectory?: directoryTree.DirectoryTreeCallback<TCustomDir>
16
+ ): directoryTree.DirectoryTree<TCustomResult>;
9
17
 
10
18
  export as namespace directoryTree;
11
19
 
12
20
  declare namespace directoryTree {
13
- export interface DirectoryTree {
21
+ export interface DirectoryTree<C extends IObj = IObj> {
14
22
  path: string;
15
23
  name: string;
16
24
  size: number;
17
25
  type: "directory" | "file";
18
- children ? : DirectoryTree[];
26
+ children?: DirectoryTree<C>[];
19
27
  extension?: string;
20
28
  isSymbolicLink?: boolean;
21
- custom: { [key: string]: any };
29
+ custom: C;
22
30
  }
31
+
23
32
  export interface DirectoryTreeOptions {
24
- normalizePath ? : boolean;
25
- exclude ? : RegExp | RegExp[];
26
- attributes ? : (keyof Stats | "type" | "extension")[];
27
- extensions ? : RegExp;
28
- followSymlink ? : boolean;
29
- depth ? : number;
33
+ normalizePath?: boolean;
34
+ exclude?: RegExp | RegExp[];
35
+ attributes?: (keyof Stats | "type" | "extension")[];
36
+ extensions?: RegExp;
37
+ followSymlinks?: boolean;
38
+ depth?: number;
30
39
  }
31
- export type DirectoryTreeCallback = (item: DirectoryTree, path: string, stats: Stats) => void;
40
+
41
+ export type DirectoryTreeCallback<TCustom extends IObj = IObj> = (
42
+ item: DirectoryTree<TCustom>,
43
+ path: string,
44
+ stats: Stats
45
+ ) => void;
32
46
  }
33
47
 
34
48
  export = directoryTree;
@@ -51,7 +51,7 @@ function isRegExp(regExp) {
51
51
  function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth = 0) {
52
52
  options = options || {};
53
53
 
54
- if (options.depth !== undefined && options.attributes.indexOf('size') !== -1) {
54
+ if (options.depth !== undefined && options.attributes && options.attributes.indexOf('size') !== -1) {
55
55
  throw new Error('usage of size attribute with depth option is prohibited');
56
56
  }
57
57
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directory-tree",
3
- "version": "3.2.1",
3
+ "version": "3.3.1",
4
4
  "description": "Convert a directory tree to a JS object.",
5
5
  "repository": {
6
6
  "type": "git",