@jiakun-zhao/utils 0.1.4 → 0.1.5

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/dist/index.cjs CHANGED
@@ -1,6 +1,19 @@
1
1
  'use strict';
2
2
 
3
3
  const utils = require('@antfu/utils');
4
+ const promises = require('node:fs/promises');
5
+ const node_path = require('node:path');
6
+
7
+ async function tree(dir) {
8
+ const arr = [];
9
+ const items = await promises.readdir(dir, { withFileTypes: true });
10
+ for (const it of items) {
11
+ const path = node_path.join(dir, it.name);
12
+ const item = it.isFile() ? { type: "file", path } : { type: "directory", path, children: await tree(path) };
13
+ arr.push(item);
14
+ }
15
+ return arr;
16
+ }
4
17
 
5
18
  function to(value, block) {
6
19
  return block(value);
@@ -25,6 +38,7 @@ exports.isNotEmpty = isNotEmpty;
25
38
  exports.singleOrNull = singleOrNull;
26
39
  exports.takeIf = takeIf;
27
40
  exports.to = to;
41
+ exports.tree = tree;
28
42
  exports.use = use;
29
43
  Object.keys(utils).forEach(function (k) {
30
44
  if (k !== 'default' && !exports.hasOwnProperty(k)) exports[k] = utils[k];
package/dist/index.d.ts CHANGED
@@ -1,9 +1,20 @@
1
1
  export * from '@antfu/utils';
2
2
 
3
+ type Tree = {
4
+ path: string;
5
+ } & ({
6
+ type: 'file';
7
+ } | {
8
+ type: 'directory';
9
+ children: Tree[];
10
+ });
11
+
12
+ declare function tree(dir: string): Promise<Tree[]>;
13
+
3
14
  declare function to<T, R>(value: T, block: (it: T) => R): R;
4
15
  declare function use<T>(value: T, block: (it: T) => void): T;
5
16
  declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
6
17
  declare function isNotEmpty(o: any): boolean;
7
18
  declare function singleOrNull<T>(arr: T[]): T | null;
8
19
 
9
- export { isNotEmpty, singleOrNull, takeIf, to, use };
20
+ export { isNotEmpty, singleOrNull, takeIf, to, tree, use };
package/dist/index.mjs CHANGED
@@ -1,4 +1,17 @@
1
1
  export * from '@antfu/utils';
2
+ import { readdir } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+
5
+ async function tree(dir) {
6
+ const arr = [];
7
+ const items = await readdir(dir, { withFileTypes: true });
8
+ for (const it of items) {
9
+ const path = join(dir, it.name);
10
+ const item = it.isFile() ? { type: "file", path } : { type: "directory", path, children: await tree(path) };
11
+ arr.push(item);
12
+ }
13
+ return arr;
14
+ }
2
15
 
3
16
  function to(value, block) {
4
17
  return block(value);
@@ -19,4 +32,4 @@ function singleOrNull(arr) {
19
32
  return arr.length === 1 ? arr[0] : null;
20
33
  }
21
34
 
22
- export { isNotEmpty, singleOrNull, takeIf, to, use };
35
+ export { isNotEmpty, singleOrNull, takeIf, to, tree, use };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "0.1.4",
4
+ "version": "0.1.5",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <jiakun.zhao@outlook.com>",
7
7
  "license": "MIT",