@hypernym/utils 1.1.0 → 1.2.0

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.
@@ -2,13 +2,13 @@
2
2
 
3
3
  var promises = require('node:fs/promises');
4
4
  var node_path = require('node:path');
5
- var formatBytes = require('../mix/format-bytes');
6
5
 
7
6
  async function exists(path) {
8
7
  return await promises.access(path, promises.constants.F_OK).then(() => true).catch(() => false);
9
8
  }
10
9
 
11
- async function getDirStats(dirPath, options) {
10
+ async function getDirStats(path, options) {
11
+ const dirPath = node_path.resolve(path);
12
12
  const dirFiles = await promises.readdir(dirPath);
13
13
  const dirBase = node_path.basename(dirPath);
14
14
  let dirStats = [];
@@ -19,7 +19,7 @@ async function getDirStats(dirPath, options) {
19
19
  let fileIndex = -1;
20
20
  let subdirIndex = -1;
21
21
  for (const file of dirFiles) {
22
- const filePath = node_path.resolve(dirPath, file);
22
+ const filePath = node_path.resolve(path, file);
23
23
  const fileStat = await promises.stat(filePath);
24
24
  if (fileStat.isDirectory()) {
25
25
  subdirIndex++;
@@ -29,20 +29,18 @@ async function getDirStats(dirPath, options) {
29
29
  base: file
30
30
  });
31
31
  if (options?.recursive) {
32
- dirBase === dirPath ? dirBase : node_path.basename(filePath);
33
32
  const stats = await getDirStats(filePath);
34
33
  const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
35
34
  dirStats = [...dirStats, updateDirStats];
36
35
  }
37
36
  } else {
38
37
  const { base, name, ext } = node_path.parse(file);
39
- const path = filePath;
40
- const size = formatBytes.formatBytes(fileStat.size);
38
+ const { size } = fileStat;
41
39
  fileIndex++;
42
- dirSize += fileStat.size;
40
+ dirSize += size;
43
41
  fileList.push({
44
42
  index: fileIndex,
45
- path,
43
+ path: filePath,
46
44
  base,
47
45
  name,
48
46
  ext,
@@ -54,12 +52,30 @@ async function getDirStats(dirPath, options) {
54
52
  index: dirIndex,
55
53
  path: dirPath,
56
54
  base: dirBase,
57
- size: formatBytes.formatBytes(dirSize),
55
+ size: dirSize,
58
56
  subdirs: subdirList,
59
57
  files: fileList
60
58
  });
61
59
  return dirStats;
62
60
  }
63
61
 
62
+ async function getDirFiles(path, options) {
63
+ const dirPath = node_path.resolve(path);
64
+ const dirFiles = await promises.readdir(dirPath, { withFileTypes: true });
65
+ let fileList = [];
66
+ for (const file of dirFiles) {
67
+ const filePath = node_path.resolve(path, file.name);
68
+ if (file.isDirectory()) {
69
+ if (options?.recursive) {
70
+ fileList = [...fileList, ...await getDirFiles(filePath)];
71
+ }
72
+ } else {
73
+ fileList.push(filePath);
74
+ }
75
+ }
76
+ return fileList;
77
+ }
78
+
64
79
  exports.exists = exists;
80
+ exports.getDirFiles = getDirFiles;
65
81
  exports.getDirStats = getDirStats;
@@ -8,7 +8,7 @@ declare function exists(path: string): Promise<boolean>;
8
8
  *
9
9
  * By default, recursive mode is disabled so only one level is scanned.
10
10
  */
11
- declare function getDirStats(dirPath: string, options?: DirStatsOptions): Promise<DirStats[]>;
11
+ declare function getDirStats(path: string, options?: DirStatsOptions): Promise<DirStats[]>;
12
12
 
13
13
  interface SubdirDetails {
14
14
  index: number;
@@ -21,13 +21,13 @@ interface FileDetails {
21
21
  base: string;
22
22
  name: string;
23
23
  ext: string;
24
- size: string;
24
+ size: number;
25
25
  }
26
26
  interface DirStats {
27
27
  index: number;
28
28
  path: string;
29
29
  base: string;
30
- size: string;
30
+ size: number;
31
31
  subdirs: SubdirDetails[];
32
32
  files: FileDetails[];
33
33
  }
@@ -35,4 +35,15 @@ interface DirStatsOptions {
35
35
  recursive?: boolean;
36
36
  }
37
37
 
38
- export { DirStats, DirStatsOptions, exists, getDirStats };
38
+ /**
39
+ * Scans the specified directory and returns a list of all files.
40
+ *
41
+ * By default, recursive mode is disabled so only one level is scanned.
42
+ */
43
+ declare function getDirFiles(path: string, options?: DirFilesOptions): Promise<string[]>;
44
+
45
+ interface DirFilesOptions {
46
+ recursive?: boolean;
47
+ }
48
+
49
+ export { DirFilesOptions, DirStats, DirStatsOptions, exists, getDirFiles, getDirStats };
@@ -1,12 +1,12 @@
1
1
  import { access, constants, readdir, stat } from 'node:fs/promises';
2
- import { basename, resolve, parse } from 'node:path';
3
- import { formatBytes } from '../mix/format-bytes';
2
+ import { resolve, basename, parse } from 'node:path';
4
3
 
5
4
  async function exists(path) {
6
5
  return await access(path, constants.F_OK).then(() => true).catch(() => false);
7
6
  }
8
7
 
9
- async function getDirStats(dirPath, options) {
8
+ async function getDirStats(path, options) {
9
+ const dirPath = resolve(path);
10
10
  const dirFiles = await readdir(dirPath);
11
11
  const dirBase = basename(dirPath);
12
12
  let dirStats = [];
@@ -17,7 +17,7 @@ async function getDirStats(dirPath, options) {
17
17
  let fileIndex = -1;
18
18
  let subdirIndex = -1;
19
19
  for (const file of dirFiles) {
20
- const filePath = resolve(dirPath, file);
20
+ const filePath = resolve(path, file);
21
21
  const fileStat = await stat(filePath);
22
22
  if (fileStat.isDirectory()) {
23
23
  subdirIndex++;
@@ -27,20 +27,18 @@ async function getDirStats(dirPath, options) {
27
27
  base: file
28
28
  });
29
29
  if (options?.recursive) {
30
- dirBase === dirPath ? dirBase : basename(filePath);
31
30
  const stats = await getDirStats(filePath);
32
31
  const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
33
32
  dirStats = [...dirStats, updateDirStats];
34
33
  }
35
34
  } else {
36
35
  const { base, name, ext } = parse(file);
37
- const path = filePath;
38
- const size = formatBytes(fileStat.size);
36
+ const { size } = fileStat;
39
37
  fileIndex++;
40
- dirSize += fileStat.size;
38
+ dirSize += size;
41
39
  fileList.push({
42
40
  index: fileIndex,
43
- path,
41
+ path: filePath,
44
42
  base,
45
43
  name,
46
44
  ext,
@@ -52,11 +50,28 @@ async function getDirStats(dirPath, options) {
52
50
  index: dirIndex,
53
51
  path: dirPath,
54
52
  base: dirBase,
55
- size: formatBytes(dirSize),
53
+ size: dirSize,
56
54
  subdirs: subdirList,
57
55
  files: fileList
58
56
  });
59
57
  return dirStats;
60
58
  }
61
59
 
62
- export { exists, getDirStats };
60
+ async function getDirFiles(path, options) {
61
+ const dirPath = resolve(path);
62
+ const dirFiles = await readdir(dirPath, { withFileTypes: true });
63
+ let fileList = [];
64
+ for (const file of dirFiles) {
65
+ const filePath = resolve(path, file.name);
66
+ if (file.isDirectory()) {
67
+ if (options?.recursive) {
68
+ fileList = [...fileList, ...await getDirFiles(filePath)];
69
+ }
70
+ } else {
71
+ fileList.push(filePath);
72
+ }
73
+ }
74
+ return fileList;
75
+ }
76
+
77
+ export { exists, getDirFiles, getDirStats };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/utils",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "author": "Hypernym Studio",
5
5
  "maintainers": [
6
6
  "Ivo Dolenc (https://github.com/ivodolenc)"
@@ -11,19 +11,19 @@
11
11
  "repository": "hypernym-studio/utils",
12
12
  "homepage": "https://github.com/hypernym-studio/utils",
13
13
  "type": "module",
14
- "main": "./dist/utils.cjs",
15
- "module": "./dist/utils.mjs",
16
- "types": "./dist/utils.d.ts",
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts",
17
17
  "exports": {
18
18
  ".": {
19
- "types": "./dist/utils.d.ts",
20
- "import": "./dist/utils.mjs",
21
- "require": "./dist/utils.cjs"
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.mjs",
21
+ "require": "./dist/index.cjs"
22
22
  },
23
23
  "./node": {
24
- "types": "./dist/node.d.ts",
25
- "import": "./dist/node.mjs",
26
- "require": "./dist/node.cjs"
24
+ "types": "./dist/node/index.d.ts",
25
+ "import": "./dist/node/index.mjs",
26
+ "require": "./dist/node/index.cjs"
27
27
  }
28
28
  },
29
29
  "files": [
@@ -59,7 +59,7 @@
59
59
  "rollup-plugin-dts": "^5.3.0",
60
60
  "rollup-plugin-esbuild": "^5.0.0",
61
61
  "typescript": "^5.0.4",
62
- "vite": "^4.4.2",
62
+ "vite": "^4.4.3",
63
63
  "vite-node": "^0.33.0",
64
64
  "vitest": "^0.33.0"
65
65
  },
File without changes
File without changes
File without changes