@jiakun-zhao/utils 1.0.3 → 1.0.4
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 +22 -19
- package/dist/index.d.cts +7 -7
- package/dist/index.d.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.mjs +23 -20
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const utils = require('@antfu/utils');
|
|
4
4
|
const shared = require('./shared.cjs');
|
|
5
|
+
const node_crypto = require('node:crypto');
|
|
6
|
+
const node_fs = require('node:fs');
|
|
5
7
|
const promises = require('node:fs/promises');
|
|
6
8
|
const node_path = require('node:path');
|
|
7
9
|
const fastGlob = require('fast-glob');
|
|
@@ -10,29 +12,30 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
12
|
|
|
11
13
|
const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
|
|
12
14
|
|
|
13
|
-
async function
|
|
14
|
-
|
|
15
|
-
await promises.access(node_path.join(...paths));
|
|
16
|
-
return true;
|
|
17
|
-
} catch {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
15
|
+
async function exists(...path) {
|
|
16
|
+
return await promises.access(node_path.join(...path), promises.constants.F_OK).then(() => true, () => false);
|
|
20
17
|
}
|
|
21
|
-
async function
|
|
22
|
-
return await promises.readFile(node_path.join(...
|
|
18
|
+
async function readText(...path) {
|
|
19
|
+
return await promises.readFile(node_path.join(...path), "utf-8");
|
|
23
20
|
}
|
|
24
|
-
async function
|
|
25
|
-
return await
|
|
21
|
+
async function readJson(...path) {
|
|
22
|
+
return JSON.parse(await readText(...path));
|
|
26
23
|
}
|
|
27
|
-
async function
|
|
28
|
-
|
|
24
|
+
async function writeText(content, ...path) {
|
|
25
|
+
await promises.writeFile(node_path.join(...path), content, "utf-8");
|
|
29
26
|
}
|
|
30
|
-
async function
|
|
31
|
-
await promises.writeFile(node_path.join(...paths), content, "utf-8");
|
|
32
|
-
}
|
|
33
|
-
async function writeJson(content, ...paths) {
|
|
27
|
+
async function writeJson(content, ...path) {
|
|
34
28
|
await writeText(`${JSON.stringify(content, null, 2)}
|
|
35
|
-
`, ...
|
|
29
|
+
`, ...path);
|
|
30
|
+
}
|
|
31
|
+
async function toMD5(...path) {
|
|
32
|
+
const hash = node_crypto.createHash("md5");
|
|
33
|
+
const stream = node_fs.createReadStream(node_path.join(...path));
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
stream.on("data", (chunk) => hash.update(chunk));
|
|
36
|
+
stream.on("end", () => resolve(hash.digest("base64url")));
|
|
37
|
+
stream.on("error", reject);
|
|
38
|
+
});
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
const alias = {
|
|
@@ -47,9 +50,9 @@ exports.takeIf = shared.takeIf;
|
|
|
47
50
|
exports.glob = fastGlob__default;
|
|
48
51
|
exports.alias = alias;
|
|
49
52
|
exports.exists = exists;
|
|
50
|
-
exports.isExists = isExists;
|
|
51
53
|
exports.readJson = readJson;
|
|
52
54
|
exports.readText = readText;
|
|
55
|
+
exports.toMD5 = toMD5;
|
|
53
56
|
exports.writeJson = writeJson;
|
|
54
57
|
exports.writeText = writeText;
|
|
55
58
|
Object.keys(utils).forEach(function (k) {
|
package/dist/index.d.cts
CHANGED
|
@@ -2,13 +2,13 @@ export * from '@antfu/utils';
|
|
|
2
2
|
export { random, safe, singleOrNull, take, takeIf } from './shared.cjs';
|
|
3
3
|
export { default as glob } from 'fast-glob';
|
|
4
4
|
|
|
5
|
-
declare function
|
|
6
|
-
declare function
|
|
7
|
-
declare function
|
|
8
|
-
declare function
|
|
9
|
-
declare function
|
|
10
|
-
declare function
|
|
5
|
+
declare function exists(...path: string[]): Promise<boolean>;
|
|
6
|
+
declare function readText(...path: string[]): Promise<string>;
|
|
7
|
+
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
8
|
+
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
9
|
+
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
10
|
+
declare function toMD5(...path: string[]): Promise<string>;
|
|
11
11
|
|
|
12
12
|
declare const alias: Record<string, string>;
|
|
13
13
|
|
|
14
|
-
export { alias, exists,
|
|
14
|
+
export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/index.d.mts
CHANGED
|
@@ -2,13 +2,13 @@ export * from '@antfu/utils';
|
|
|
2
2
|
export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
|
|
3
3
|
export { default as glob } from 'fast-glob';
|
|
4
4
|
|
|
5
|
-
declare function
|
|
6
|
-
declare function
|
|
7
|
-
declare function
|
|
8
|
-
declare function
|
|
9
|
-
declare function
|
|
10
|
-
declare function
|
|
5
|
+
declare function exists(...path: string[]): Promise<boolean>;
|
|
6
|
+
declare function readText(...path: string[]): Promise<string>;
|
|
7
|
+
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
8
|
+
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
9
|
+
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
10
|
+
declare function toMD5(...path: string[]): Promise<string>;
|
|
11
11
|
|
|
12
12
|
declare const alias: Record<string, string>;
|
|
13
13
|
|
|
14
|
-
export { alias, exists,
|
|
14
|
+
export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,13 +2,13 @@ export * from '@antfu/utils';
|
|
|
2
2
|
export { random, safe, singleOrNull, take, takeIf } from './shared.js';
|
|
3
3
|
export { default as glob } from 'fast-glob';
|
|
4
4
|
|
|
5
|
-
declare function
|
|
6
|
-
declare function
|
|
7
|
-
declare function
|
|
8
|
-
declare function
|
|
9
|
-
declare function
|
|
10
|
-
declare function
|
|
5
|
+
declare function exists(...path: string[]): Promise<boolean>;
|
|
6
|
+
declare function readText(...path: string[]): Promise<string>;
|
|
7
|
+
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
8
|
+
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
9
|
+
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
10
|
+
declare function toMD5(...path: string[]): Promise<string>;
|
|
11
11
|
|
|
12
12
|
declare const alias: Record<string, string>;
|
|
13
13
|
|
|
14
|
-
export { alias, exists,
|
|
14
|
+
export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/index.mjs
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
1
|
export * from '@antfu/utils';
|
|
2
2
|
export { random, safe, singleOrNull, take, takeIf } from './shared.mjs';
|
|
3
|
-
import {
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { createReadStream } from 'node:fs';
|
|
5
|
+
import { access, constants, readFile, writeFile } from 'node:fs/promises';
|
|
4
6
|
import { join } from 'node:path';
|
|
5
7
|
export { default as glob } from 'fast-glob';
|
|
6
8
|
|
|
7
|
-
async function
|
|
8
|
-
|
|
9
|
-
await access(join(...paths));
|
|
10
|
-
return true;
|
|
11
|
-
} catch {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
9
|
+
async function exists(...path) {
|
|
10
|
+
return await access(join(...path), constants.F_OK).then(() => true, () => false);
|
|
14
11
|
}
|
|
15
|
-
async function
|
|
16
|
-
return await readFile(join(...
|
|
12
|
+
async function readText(...path) {
|
|
13
|
+
return await readFile(join(...path), "utf-8");
|
|
17
14
|
}
|
|
18
|
-
async function
|
|
19
|
-
return await
|
|
15
|
+
async function readJson(...path) {
|
|
16
|
+
return JSON.parse(await readText(...path));
|
|
20
17
|
}
|
|
21
|
-
async function
|
|
22
|
-
|
|
18
|
+
async function writeText(content, ...path) {
|
|
19
|
+
await writeFile(join(...path), content, "utf-8");
|
|
23
20
|
}
|
|
24
|
-
async function
|
|
25
|
-
await writeFile(join(...paths), content, "utf-8");
|
|
26
|
-
}
|
|
27
|
-
async function writeJson(content, ...paths) {
|
|
21
|
+
async function writeJson(content, ...path) {
|
|
28
22
|
await writeText(`${JSON.stringify(content, null, 2)}
|
|
29
|
-
`, ...
|
|
23
|
+
`, ...path);
|
|
24
|
+
}
|
|
25
|
+
async function toMD5(...path) {
|
|
26
|
+
const hash = createHash("md5");
|
|
27
|
+
const stream = createReadStream(join(...path));
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
stream.on("data", (chunk) => hash.update(chunk));
|
|
30
|
+
stream.on("end", () => resolve(hash.digest("base64url")));
|
|
31
|
+
stream.on("error", reject);
|
|
32
|
+
});
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
const alias = {
|
|
33
36
|
"@jiakun-zhao/utils": "@jiakun-zhao/utils/shared"
|
|
34
37
|
};
|
|
35
38
|
|
|
36
|
-
export { alias, exists,
|
|
39
|
+
export { alias, exists, readJson, readText, toMD5, writeJson, writeText };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.4",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <hi@zhaojiakun.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@jiakun-zhao/eslint-config": "^1.3.1",
|
|
42
|
-
"@types/node": "^20.
|
|
42
|
+
"@types/node": "^20.10.0",
|
|
43
43
|
"bumpp": "^9.2.0",
|
|
44
44
|
"eslint": "^8.54.0",
|
|
45
45
|
"esno": "^4.0.0",
|