@jiakun-zhao/utils 0.0.12
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 +5 -0
- package/dist/index.cjs +73 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.mjs +56 -0
- package/package.json +45 -0
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const promises = require('node:fs/promises');
|
|
4
|
+
const node_path = require('node:path');
|
|
5
|
+
const process = require('node:process');
|
|
6
|
+
const crypto = require('node:crypto');
|
|
7
|
+
|
|
8
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
9
|
+
|
|
10
|
+
const process__default = /*#__PURE__*/_interopDefaultCompat(process);
|
|
11
|
+
const crypto__default = /*#__PURE__*/_interopDefaultCompat(crypto);
|
|
12
|
+
|
|
13
|
+
async function dist(cwd = process__default.cwd()) {
|
|
14
|
+
const path = node_path.join(cwd, "dist");
|
|
15
|
+
await promises.rm(path, { recursive: true, force: true });
|
|
16
|
+
await promises.mkdir(path, { recursive: true });
|
|
17
|
+
return path;
|
|
18
|
+
}
|
|
19
|
+
async function isFileExist(path) {
|
|
20
|
+
try {
|
|
21
|
+
await promises.access(path);
|
|
22
|
+
return true;
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function readText(path) {
|
|
28
|
+
return await isFileExist(path) ? await promises.readFile(path, "utf-8") : null;
|
|
29
|
+
}
|
|
30
|
+
async function readJson(path) {
|
|
31
|
+
const raw = await readText(path);
|
|
32
|
+
return raw ? JSON.parse(raw) : null;
|
|
33
|
+
}
|
|
34
|
+
async function writeText(path, data) {
|
|
35
|
+
const { dir } = node_path.parse(path);
|
|
36
|
+
await promises.mkdir(dir, { recursive: true });
|
|
37
|
+
await promises.writeFile(path, data, "utf-8");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const md5 = crypto__default.createHash("md5");
|
|
41
|
+
function toMD5(str) {
|
|
42
|
+
return md5.update(str).digest("hex");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function toArray() {
|
|
46
|
+
return Array.isArray(this) ? this : [this];
|
|
47
|
+
}
|
|
48
|
+
function filterNotNull() {
|
|
49
|
+
return this.filter((it) => it != null);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isString(value) {
|
|
53
|
+
return typeof value === "string";
|
|
54
|
+
}
|
|
55
|
+
function it(block) {
|
|
56
|
+
block(this);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
function to(block) {
|
|
60
|
+
return block(this);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
exports.dist = dist;
|
|
64
|
+
exports.filterNotNull = filterNotNull;
|
|
65
|
+
exports.isFileExist = isFileExist;
|
|
66
|
+
exports.isString = isString;
|
|
67
|
+
exports.it = it;
|
|
68
|
+
exports.readJson = readJson;
|
|
69
|
+
exports.readText = readText;
|
|
70
|
+
exports.to = to;
|
|
71
|
+
exports.toArray = toArray;
|
|
72
|
+
exports.toMD5 = toMD5;
|
|
73
|
+
exports.writeText = writeText;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare function dist(cwd?: string): Promise<string>;
|
|
2
|
+
declare function isFileExist(path: string): Promise<boolean>;
|
|
3
|
+
declare function readText(path: string): Promise<string | null>;
|
|
4
|
+
declare function readJson<T>(path: string): Promise<T | null>;
|
|
5
|
+
declare function writeText(path: string, data: string): Promise<void>;
|
|
6
|
+
|
|
7
|
+
declare function toMD5(str: string): string;
|
|
8
|
+
|
|
9
|
+
declare function toArray<T>(this: T | T[]): T[];
|
|
10
|
+
declare function filterNotNull<T>(this: T[]): NonNullable<T>[];
|
|
11
|
+
|
|
12
|
+
declare function isString(value: any): value is string;
|
|
13
|
+
declare function it<T>(this: T, block: (it: T) => void): T;
|
|
14
|
+
declare function to<T, R>(this: T, block: (it: T) => R): R;
|
|
15
|
+
|
|
16
|
+
export { dist, filterNotNull, isFileExist, isString, it, readJson, readText, to, toArray, toMD5, writeText };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { rm, mkdir, access, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { join, parse } from 'node:path';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
|
|
6
|
+
async function dist(cwd = process.cwd()) {
|
|
7
|
+
const path = join(cwd, "dist");
|
|
8
|
+
await rm(path, { recursive: true, force: true });
|
|
9
|
+
await mkdir(path, { recursive: true });
|
|
10
|
+
return path;
|
|
11
|
+
}
|
|
12
|
+
async function isFileExist(path) {
|
|
13
|
+
try {
|
|
14
|
+
await access(path);
|
|
15
|
+
return true;
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function readText(path) {
|
|
21
|
+
return await isFileExist(path) ? await readFile(path, "utf-8") : null;
|
|
22
|
+
}
|
|
23
|
+
async function readJson(path) {
|
|
24
|
+
const raw = await readText(path);
|
|
25
|
+
return raw ? JSON.parse(raw) : null;
|
|
26
|
+
}
|
|
27
|
+
async function writeText(path, data) {
|
|
28
|
+
const { dir } = parse(path);
|
|
29
|
+
await mkdir(dir, { recursive: true });
|
|
30
|
+
await writeFile(path, data, "utf-8");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const md5 = crypto.createHash("md5");
|
|
34
|
+
function toMD5(str) {
|
|
35
|
+
return md5.update(str).digest("hex");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function toArray() {
|
|
39
|
+
return Array.isArray(this) ? this : [this];
|
|
40
|
+
}
|
|
41
|
+
function filterNotNull() {
|
|
42
|
+
return this.filter((it) => it != null);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isString(value) {
|
|
46
|
+
return typeof value === "string";
|
|
47
|
+
}
|
|
48
|
+
function it(block) {
|
|
49
|
+
block(this);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
function to(block) {
|
|
53
|
+
return block(this);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { dist, filterNotNull, isFileExist, isString, it, readJson, readText, to, toArray, toMD5, writeText };
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jiakun-zhao/utils",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.12",
|
|
5
|
+
"description": "Utils.",
|
|
6
|
+
"author": "Jiakun Zhao <jiakun.zhao@outlook.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/jiakun-zhao/utils#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/jiakun-zhao/utils.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/jiakun-zhao/utils/issues"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"require": "./dist/index.cjs",
|
|
20
|
+
"import": "./dist/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@jiakun-zhao/eslint-config": "^1.2.0",
|
|
31
|
+
"@types/node": "^20.4.5",
|
|
32
|
+
"bumpp": "^9.1.1",
|
|
33
|
+
"eslint": "^8.45.0",
|
|
34
|
+
"esno": "^0.17.0",
|
|
35
|
+
"typescript": "^5.1.6",
|
|
36
|
+
"unbuild": "^1.2.1"
|
|
37
|
+
},
|
|
38
|
+
"eslintConfig": {
|
|
39
|
+
"extends": "@jiakun-zhao"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "unbuild",
|
|
43
|
+
"release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils"
|
|
44
|
+
}
|
|
45
|
+
}
|