@hypernym/utils 1.0.0 → 1.1.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.
- package/dist/node.cjs +65 -0
- package/dist/node.d.ts +38 -0
- package/dist/node.mjs +62 -0
- package/dist/utils.cjs +12 -0
- package/dist/utils.d.ts +19 -11
- package/dist/utils.mjs +12 -1
- package/node.d.ts +1 -0
- package/package.json +13 -7
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var promises = require('node:fs/promises');
|
|
4
|
+
var node_path = require('node:path');
|
|
5
|
+
var formatBytes = require('../mix/format-bytes');
|
|
6
|
+
|
|
7
|
+
async function exists(path) {
|
|
8
|
+
return await promises.access(path, promises.constants.F_OK).then(() => true).catch(() => false);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function getDirStats(dirPath, options) {
|
|
12
|
+
const dirFiles = await promises.readdir(dirPath);
|
|
13
|
+
const dirBase = node_path.basename(dirPath);
|
|
14
|
+
let dirStats = [];
|
|
15
|
+
const subdirList = [];
|
|
16
|
+
const fileList = [];
|
|
17
|
+
let dirSize = 0;
|
|
18
|
+
let dirIndex = 0;
|
|
19
|
+
let fileIndex = -1;
|
|
20
|
+
let subdirIndex = -1;
|
|
21
|
+
for (const file of dirFiles) {
|
|
22
|
+
const filePath = node_path.resolve(dirPath, file);
|
|
23
|
+
const fileStat = await promises.stat(filePath);
|
|
24
|
+
if (fileStat.isDirectory()) {
|
|
25
|
+
subdirIndex++;
|
|
26
|
+
subdirList.push({
|
|
27
|
+
index: subdirIndex,
|
|
28
|
+
path: filePath,
|
|
29
|
+
base: file
|
|
30
|
+
});
|
|
31
|
+
if (options?.recursive) {
|
|
32
|
+
dirBase === dirPath ? dirBase : node_path.basename(filePath);
|
|
33
|
+
const stats = await getDirStats(filePath);
|
|
34
|
+
const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
|
|
35
|
+
dirStats = [...dirStats, updateDirStats];
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
const { base, name, ext } = node_path.parse(file);
|
|
39
|
+
const path = filePath;
|
|
40
|
+
const size = formatBytes.formatBytes(fileStat.size);
|
|
41
|
+
fileIndex++;
|
|
42
|
+
dirSize += fileStat.size;
|
|
43
|
+
fileList.push({
|
|
44
|
+
index: fileIndex,
|
|
45
|
+
path,
|
|
46
|
+
base,
|
|
47
|
+
name,
|
|
48
|
+
ext,
|
|
49
|
+
size
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
dirStats.push({
|
|
54
|
+
index: dirIndex,
|
|
55
|
+
path: dirPath,
|
|
56
|
+
base: dirBase,
|
|
57
|
+
size: formatBytes.formatBytes(dirSize),
|
|
58
|
+
subdirs: subdirList,
|
|
59
|
+
files: fileList
|
|
60
|
+
});
|
|
61
|
+
return dirStats;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exports.exists = exists;
|
|
65
|
+
exports.getDirStats = getDirStats;
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the file exists.
|
|
3
|
+
*/
|
|
4
|
+
declare function exists(path: string): Promise<boolean>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Scans the specified directory and gets details for each subdirectory and file.
|
|
8
|
+
*
|
|
9
|
+
* By default, recursive mode is disabled so only one level is scanned.
|
|
10
|
+
*/
|
|
11
|
+
declare function getDirStats(dirPath: string, options?: DirStatsOptions): Promise<DirStats[]>;
|
|
12
|
+
|
|
13
|
+
interface SubdirDetails {
|
|
14
|
+
index: number;
|
|
15
|
+
path: string;
|
|
16
|
+
base: string;
|
|
17
|
+
}
|
|
18
|
+
interface FileDetails {
|
|
19
|
+
index: number;
|
|
20
|
+
path: string;
|
|
21
|
+
base: string;
|
|
22
|
+
name: string;
|
|
23
|
+
ext: string;
|
|
24
|
+
size: string;
|
|
25
|
+
}
|
|
26
|
+
interface DirStats {
|
|
27
|
+
index: number;
|
|
28
|
+
path: string;
|
|
29
|
+
base: string;
|
|
30
|
+
size: string;
|
|
31
|
+
subdirs: SubdirDetails[];
|
|
32
|
+
files: FileDetails[];
|
|
33
|
+
}
|
|
34
|
+
interface DirStatsOptions {
|
|
35
|
+
recursive?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { DirStats, DirStatsOptions, exists, getDirStats };
|
package/dist/node.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
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';
|
|
4
|
+
|
|
5
|
+
async function exists(path) {
|
|
6
|
+
return await access(path, constants.F_OK).then(() => true).catch(() => false);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async function getDirStats(dirPath, options) {
|
|
10
|
+
const dirFiles = await readdir(dirPath);
|
|
11
|
+
const dirBase = basename(dirPath);
|
|
12
|
+
let dirStats = [];
|
|
13
|
+
const subdirList = [];
|
|
14
|
+
const fileList = [];
|
|
15
|
+
let dirSize = 0;
|
|
16
|
+
let dirIndex = 0;
|
|
17
|
+
let fileIndex = -1;
|
|
18
|
+
let subdirIndex = -1;
|
|
19
|
+
for (const file of dirFiles) {
|
|
20
|
+
const filePath = resolve(dirPath, file);
|
|
21
|
+
const fileStat = await stat(filePath);
|
|
22
|
+
if (fileStat.isDirectory()) {
|
|
23
|
+
subdirIndex++;
|
|
24
|
+
subdirList.push({
|
|
25
|
+
index: subdirIndex,
|
|
26
|
+
path: filePath,
|
|
27
|
+
base: file
|
|
28
|
+
});
|
|
29
|
+
if (options?.recursive) {
|
|
30
|
+
dirBase === dirPath ? dirBase : basename(filePath);
|
|
31
|
+
const stats = await getDirStats(filePath);
|
|
32
|
+
const updateDirStats = { ...stats[0], ...{ index: dirIndex++ } };
|
|
33
|
+
dirStats = [...dirStats, updateDirStats];
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
const { base, name, ext } = parse(file);
|
|
37
|
+
const path = filePath;
|
|
38
|
+
const size = formatBytes(fileStat.size);
|
|
39
|
+
fileIndex++;
|
|
40
|
+
dirSize += fileStat.size;
|
|
41
|
+
fileList.push({
|
|
42
|
+
index: fileIndex,
|
|
43
|
+
path,
|
|
44
|
+
base,
|
|
45
|
+
name,
|
|
46
|
+
ext,
|
|
47
|
+
size
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
dirStats.push({
|
|
52
|
+
index: dirIndex,
|
|
53
|
+
path: dirPath,
|
|
54
|
+
base: dirBase,
|
|
55
|
+
size: formatBytes(dirSize),
|
|
56
|
+
subdirs: subdirList,
|
|
57
|
+
files: fileList
|
|
58
|
+
});
|
|
59
|
+
return dirStats;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { exists, getDirStats };
|
package/dist/utils.cjs
CHANGED
|
@@ -33,6 +33,18 @@ const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
|
|
|
33
33
|
const isHtmlCollection = (v) => v instanceof HTMLCollection;
|
|
34
34
|
const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
|
|
35
35
|
|
|
36
|
+
function formatBytes(bytes, options) {
|
|
37
|
+
const decimals = options?.decimals || 2;
|
|
38
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
39
|
+
if (bytes === 0)
|
|
40
|
+
return `0 B`;
|
|
41
|
+
const k = 1024;
|
|
42
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
43
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
44
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.formatBytes = formatBytes;
|
|
36
48
|
exports.isArray = isArray;
|
|
37
49
|
exports.isArrayEmpty = isArrayEmpty;
|
|
38
50
|
exports.isBigint = isBigint;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An empty arrow function that performs no operation.
|
|
3
|
+
*/
|
|
4
|
+
declare const noop: () => void;
|
|
5
|
+
/**
|
|
6
|
+
* Returns a string representing the object.
|
|
7
|
+
*/
|
|
8
|
+
declare const toString: (v: any) => string;
|
|
9
|
+
|
|
1
10
|
/**
|
|
2
11
|
* Matches any primitive value.
|
|
3
12
|
*/
|
|
4
13
|
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
5
|
-
|
|
6
14
|
/**
|
|
7
15
|
* Matches any `Primitive`, `Date` or `RegExp` value.
|
|
8
16
|
*/
|
|
@@ -134,6 +142,15 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
|
134
142
|
*/
|
|
135
143
|
type IsNever<T> = [T] extends [never] ? true : false;
|
|
136
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Converts bytes to a _human-readable_ size and appends a units suffix.
|
|
147
|
+
*/
|
|
148
|
+
declare function formatBytes(bytes: number, options?: FormatBytesOptions): string;
|
|
149
|
+
|
|
150
|
+
interface FormatBytesOptions {
|
|
151
|
+
decimals?: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
137
154
|
type OptionsDeep = {
|
|
138
155
|
/**
|
|
139
156
|
* Enables recursive mode for arrays and tuples.
|
|
@@ -171,13 +188,4 @@ type RequiredObjectDeep<T extends object, Options extends OptionsDeep = {
|
|
|
171
188
|
[K in keyof T]-?: RequiredDeep<T[K], Options>;
|
|
172
189
|
};
|
|
173
190
|
|
|
174
|
-
|
|
175
|
-
* An empty arrow function that performs no operation.
|
|
176
|
-
*/
|
|
177
|
-
declare const noop: () => void;
|
|
178
|
-
/**
|
|
179
|
-
* Returns a string representing the object.
|
|
180
|
-
*/
|
|
181
|
-
declare const toString: (v: any) => string;
|
|
182
|
-
|
|
183
|
-
export { BuiltIn, IsAny, IsNever, IsNull, PartialDeep, Primitive, RequiredDeep, isArray, isArrayEmpty, isBigint, isBoolean, isBrowser, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, toString };
|
|
191
|
+
export { BuiltIn, FormatBytesOptions, IsAny, IsNever, IsNull, OptionsDeep, PartialDeep, Primitive, RequiredDeep, formatBytes, isArray, isArrayEmpty, isBigint, isBoolean, isBrowser, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, toString };
|
package/dist/utils.mjs
CHANGED
|
@@ -31,4 +31,15 @@ const isNodeListEmpty = (v) => isNodeList(v) && v.length === 0;
|
|
|
31
31
|
const isHtmlCollection = (v) => v instanceof HTMLCollection;
|
|
32
32
|
const isHtmlCollectionEmpty = (v) => isHtmlCollection(v) && v.length === 0;
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
function formatBytes(bytes, options) {
|
|
35
|
+
const decimals = options?.decimals || 2;
|
|
36
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
37
|
+
if (bytes === 0)
|
|
38
|
+
return `0 B`;
|
|
39
|
+
const k = 1024;
|
|
40
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
41
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
42
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { formatBytes, isArray, isArrayEmpty, isBigint, isBoolean, isBrowser, isDate, isElement, isError, isFunction, isHtmlCollection, isHtmlCollectionEmpty, isInfinity, isMap, isNaNValue, isNodeList, isNodeListEmpty, isNull, isNumber, isObject, isObjectEmpty, isPrimitive, isRegExp, isSet, isString, isStringEmpty, isSymbol, isURL, isUndefined, noop, toString };
|
package/node.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/node'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hypernym/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"author": "Hypernym Studio",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Ivo Dolenc (https://github.com/ivodolenc)"
|
|
@@ -19,10 +19,16 @@
|
|
|
19
19
|
"types": "./dist/utils.d.ts",
|
|
20
20
|
"import": "./dist/utils.mjs",
|
|
21
21
|
"require": "./dist/utils.cjs"
|
|
22
|
+
},
|
|
23
|
+
"./node": {
|
|
24
|
+
"types": "./dist/node.d.ts",
|
|
25
|
+
"import": "./dist/node.mjs",
|
|
26
|
+
"require": "./dist/node.cjs"
|
|
22
27
|
}
|
|
23
28
|
},
|
|
24
29
|
"files": [
|
|
25
|
-
"dist"
|
|
30
|
+
"dist",
|
|
31
|
+
"*.d.ts"
|
|
26
32
|
],
|
|
27
33
|
"keywords": [
|
|
28
34
|
"typescript",
|
|
@@ -46,16 +52,16 @@
|
|
|
46
52
|
"devDependencies": {
|
|
47
53
|
"@hypernym/eslint-config": "^1.0.1",
|
|
48
54
|
"@hypernym/prettier-config": "^1.0.1",
|
|
49
|
-
"@types/node": "^20.
|
|
55
|
+
"@types/node": "^20.4.1",
|
|
50
56
|
"eslint": "^8.44.0",
|
|
51
57
|
"prettier": "^2.8.8",
|
|
52
|
-
"rollup": "^3.26.
|
|
58
|
+
"rollup": "^3.26.2",
|
|
53
59
|
"rollup-plugin-dts": "^5.3.0",
|
|
54
60
|
"rollup-plugin-esbuild": "^5.0.0",
|
|
55
61
|
"typescript": "^5.0.4",
|
|
56
|
-
"vite": "^4.
|
|
57
|
-
"vite-node": "^0.
|
|
58
|
-
"vitest": "^0.
|
|
62
|
+
"vite": "^4.4.2",
|
|
63
|
+
"vite-node": "^0.33.0",
|
|
64
|
+
"vitest": "^0.33.0"
|
|
59
65
|
},
|
|
60
66
|
"publishConfig": {
|
|
61
67
|
"access": "public"
|