@bitbeater/node-utils 2.0.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/README.md +19 -0
- package/dist/fs/dirs.d.ts +25 -0
- package/dist/fs/dirs.d.ts.map +1 -0
- package/dist/fs/dirs.js +101 -0
- package/dist/fs/dirs.js.map +1 -0
- package/dist/fs/files.d.ts +192 -0
- package/dist/fs/files.d.ts.map +1 -0
- package/dist/fs/files.js +330 -0
- package/dist/fs/files.js.map +1 -0
- package/dist/fs/fs.d.ts +4 -0
- package/dist/fs/fs.d.ts.map +1 -0
- package/dist/fs/fs.js +20 -0
- package/dist/fs/fs.js.map +1 -0
- package/dist/fs/paths.d.ts +29 -0
- package/dist/fs/paths.d.ts.map +1 -0
- package/dist/fs/paths.js +46 -0
- package/dist/fs/paths.js.map +1 -0
- package/dist/http.d.ts +292 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +460 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# node-utils
|
|
2
|
+
|
|
3
|
+
simple, lightweight, dependenciesless, nodejs generic helper library
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
npm
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @bitbeater/node-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
deno
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
deno install npm:@bitbeater/node-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
[Documentation](https://bitbeater.github.io/node-utils/index.html)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Directory for storing app data during development.
|
|
3
|
+
* This ensures that isn't required special permissions to write to the directory and it's isolated from system directories. */
|
|
4
|
+
export declare function getOsAppInstallDir(): string;
|
|
5
|
+
/**
|
|
6
|
+
* Architecture-independent (shared) data.
|
|
7
|
+
* e.g. C:\ProgramData\ on win32 or /usr/share on Linux.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getOsSharedDataDir(): string;
|
|
10
|
+
/**
|
|
11
|
+
* OS-specific system wide dir for app configurations.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getOsSysConfDir(): string;
|
|
14
|
+
/**
|
|
15
|
+
* USER-specific dir for app configurations.
|
|
16
|
+
*/
|
|
17
|
+
export declare function getOsUsrConfDir(): string;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the home directory of the current OS user.
|
|
20
|
+
* e.g. C:\Users\<user> on win32 or /home/<user> on Linux.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getOsUserHomeDir(): string;
|
|
23
|
+
export declare function getOsUserBinDir(): string;
|
|
24
|
+
export declare function getOsDeskotpDir(): string;
|
|
25
|
+
//# sourceMappingURL=dirs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dirs.d.ts","sourceRoot":"","sources":["../../src/fs/dirs.ts"],"names":[],"mappings":"AAEA;;+HAE+H;AAC/H,wBAAgB,kBAAkB,IAAI,MAAM,CAW3C;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,CAW3C;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAWxC;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAUxC;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAUzC;AAED,wBAAgB,eAAe,IAAI,MAAM,CAWxC;AAED,wBAAgB,eAAe,IAAI,MAAM,CAExC"}
|
package/dist/fs/dirs.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOsAppInstallDir = getOsAppInstallDir;
|
|
4
|
+
exports.getOsSharedDataDir = getOsSharedDataDir;
|
|
5
|
+
exports.getOsSysConfDir = getOsSysConfDir;
|
|
6
|
+
exports.getOsUsrConfDir = getOsUsrConfDir;
|
|
7
|
+
exports.getOsUserHomeDir = getOsUserHomeDir;
|
|
8
|
+
exports.getOsUserBinDir = getOsUserBinDir;
|
|
9
|
+
exports.getOsDeskotpDir = getOsDeskotpDir;
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
/**
|
|
12
|
+
* Directory for storing app data during development.
|
|
13
|
+
* This ensures that isn't required special permissions to write to the directory and it's isolated from system directories. */
|
|
14
|
+
function getOsAppInstallDir() {
|
|
15
|
+
switch (process.platform) {
|
|
16
|
+
case 'linux':
|
|
17
|
+
return '/opt';
|
|
18
|
+
case 'darwin':
|
|
19
|
+
return '/Applications';
|
|
20
|
+
case 'win32':
|
|
21
|
+
return 'C:\\Program Files';
|
|
22
|
+
default:
|
|
23
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Architecture-independent (shared) data.
|
|
28
|
+
* e.g. C:\ProgramData\ on win32 or /usr/share on Linux.
|
|
29
|
+
*/
|
|
30
|
+
function getOsSharedDataDir() {
|
|
31
|
+
switch (process.platform) {
|
|
32
|
+
case 'linux':
|
|
33
|
+
return (0, path_1.resolve)('/usr', 'share');
|
|
34
|
+
case 'darwin':
|
|
35
|
+
return '~/Library/Application Support';
|
|
36
|
+
case 'win32':
|
|
37
|
+
return process.env.APPDATA;
|
|
38
|
+
default:
|
|
39
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* OS-specific system wide dir for app configurations.
|
|
44
|
+
*/
|
|
45
|
+
function getOsSysConfDir() {
|
|
46
|
+
switch (process.platform) {
|
|
47
|
+
case 'linux':
|
|
48
|
+
return (0, path_1.resolve)('/etc');
|
|
49
|
+
case 'darwin':
|
|
50
|
+
return (0, path_1.resolve)('/Library/Application Support');
|
|
51
|
+
case 'win32':
|
|
52
|
+
return process.env.APPDATA;
|
|
53
|
+
default:
|
|
54
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* USER-specific dir for app configurations.
|
|
59
|
+
*/
|
|
60
|
+
function getOsUsrConfDir() {
|
|
61
|
+
switch (process.platform) {
|
|
62
|
+
case 'linux':
|
|
63
|
+
case 'darwin':
|
|
64
|
+
return (0, path_1.resolve)(getOsUserHomeDir(), '.config');
|
|
65
|
+
case 'win32':
|
|
66
|
+
return process.env.LOCALAPPDATA;
|
|
67
|
+
default:
|
|
68
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the home directory of the current OS user.
|
|
73
|
+
* e.g. C:\Users\<user> on win32 or /home/<user> on Linux.
|
|
74
|
+
*/
|
|
75
|
+
function getOsUserHomeDir() {
|
|
76
|
+
switch (process.platform) {
|
|
77
|
+
case 'linux':
|
|
78
|
+
case 'darwin':
|
|
79
|
+
return process.env.HOME;
|
|
80
|
+
case 'win32':
|
|
81
|
+
return process.env.USERPROFILE;
|
|
82
|
+
default:
|
|
83
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function getOsUserBinDir() {
|
|
87
|
+
switch (process.platform) {
|
|
88
|
+
case 'linux':
|
|
89
|
+
return (0, path_1.resolve)(getOsUserHomeDir(), '.local', 'bin');
|
|
90
|
+
case 'darwin':
|
|
91
|
+
return (0, path_1.resolve)(getOsUserHomeDir(), 'bin');
|
|
92
|
+
case 'win32':
|
|
93
|
+
return getOsAppInstallDir();
|
|
94
|
+
default:
|
|
95
|
+
throw new Error(`Unsupported OS: ${process.platform}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function getOsDeskotpDir() {
|
|
99
|
+
return (0, path_1.resolve)(getOsUserHomeDir(), 'Desktop');
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=dirs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dirs.js","sourceRoot":"","sources":["../../src/fs/dirs.ts"],"names":[],"mappings":";;AAKA,gDAWC;AAMD,gDAWC;AAKD,0CAWC;AAKD,0CAUC;AAMD,4CAUC;AAED,0CAWC;AAED,0CAEC;AAjGD,+BAA+B;AAE/B;;+HAE+H;AAC/H,SAAgB,kBAAkB;IAC9B,QAAQ,OAAO,CAAC,QAAQ,EAAE,CAAC;QACvB,KAAK,OAAO;YACR,OAAO,MAAM,CAAC;QAClB,KAAK,QAAQ;YACT,OAAO,eAAe,CAAC;QAC3B,KAAK,OAAO;YACR,OAAO,mBAAmB,CAAC;QAC/B;YACI,MAAM,IAAI,KAAK,CAAC,mBAAmB,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/D,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAgB,kBAAkB;IAC9B,QAAQ,OAAO,CAAC,QAAQ,EAAE,CAAC;QACvB,KAAK,OAAO;YACR,OAAO,IAAA,cAAO,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACpC,KAAK,QAAQ;YACT,OAAO,+BAA+B,CAAC;QAC3C,KAAK,OAAO;YACR,OAAO,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC;QAC/B;YACI,MAAM,IAAI,KAAK,CAAC,mBAAmB,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/D,CAAC;AACL,CAAC;AAED;;GAEG;AACH,SAAgB,eAAe;IAC3B,QAAQ,OAAO,CAAC,QAAQ,EAAE,CAAC;QACvB,KAAK,OAAO;YACR,OAAO,IAAA,cAAO,EAAC,MAAM,CAAC,CAAC;QAC3B,KAAK,QAAQ;YACT,OAAO,IAAA,cAAO,EAAC,8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,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/D,CAAC;AACL,CAAC;AAED,SAAgB,eAAe;IAC3B,OAAO,IAAA,cAAO,EAAC,gBAAgB,EAAE,EAAE,SAAS,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Abortable } from 'events';
|
|
2
|
+
import { Mode, ObjectEncodingOptions, OpenMode, PathLike, WriteFileOptions } from 'fs';
|
|
3
|
+
import { FileHandle, FlagAndOpenMode } from 'fs/promises';
|
|
4
|
+
import { Stream } from 'stream';
|
|
5
|
+
import { ZlibOptions } from 'zlib';
|
|
6
|
+
import { Reviver, Replacer } from '@bitbeater/ecma-utils/revivers';
|
|
7
|
+
/** *
|
|
8
|
+
* If `data` is a plain object, it must have an own (not inherited) `toString`function property.
|
|
9
|
+
*
|
|
10
|
+
* The `mode` option only affects the newly created file. See {@link open} for more details.
|
|
11
|
+
*
|
|
12
|
+
* For detailed information, see the documentation of the asynchronous version of
|
|
13
|
+
* this API: {@link writeFile}.
|
|
14
|
+
* @param file filename or file descriptor
|
|
15
|
+
*/
|
|
16
|
+
export declare function writeSync(file: PathLike, data?: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): void;
|
|
17
|
+
/**
|
|
18
|
+
* Writes the given object as JSON to the specified file path synchronously.
|
|
19
|
+
* @param path - The file path where the JSON should be written.
|
|
20
|
+
* @param object - The object to be written as JSON.
|
|
21
|
+
*/
|
|
22
|
+
export declare function writeJsonSync(path: string, object: any): void;
|
|
23
|
+
/**
|
|
24
|
+
* Reads and parses a JSON file synchronously.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The type of the parsed JSON object.
|
|
27
|
+
* @param {string} path - The path to the JSON file.
|
|
28
|
+
* @param {reviver.Reviver<any>} [reviver] - Optional reviver function for JSON.parse.
|
|
29
|
+
* @returns {T} - The parsed JSON object.
|
|
30
|
+
*/
|
|
31
|
+
export declare function readJsonSync<T>(path: string, reviver?: Reviver<any>): T;
|
|
32
|
+
/**
|
|
33
|
+
* Inserts the specified data between the given placeholders in the file synchronously.
|
|
34
|
+
* If the file does not exist, it creates the file and inserts the data.
|
|
35
|
+
*
|
|
36
|
+
* @param filePath - The path of the file.
|
|
37
|
+
* @param data - The data to be inserted between the placeholders.
|
|
38
|
+
* @param beginPlaceHolder - The beginning placeholder.
|
|
39
|
+
* @param endPlaceHolder - The ending placeholder.
|
|
40
|
+
*/
|
|
41
|
+
export declare function insertBetweenPlacweHoldersSync(filePath: string, data: string, beginPlaceHolder: string, endPlaceHolder: string): void;
|
|
42
|
+
/**
|
|
43
|
+
* Reads a file and returns an array of string lines.
|
|
44
|
+
*
|
|
45
|
+
* @param path - The path to the file.
|
|
46
|
+
* @param lineSeparator - The regular expression used to split the file into lines. Defaults to /[\n|\r]/.
|
|
47
|
+
* @returns An array of lines from the file, or null if the file cannot be read.
|
|
48
|
+
*/
|
|
49
|
+
export declare function fileLinesSync(path: string, lineSeparator?: RegExp): string[];
|
|
50
|
+
/**
|
|
51
|
+
* Writes the given data to a file in GZip format synchronously.
|
|
52
|
+
*
|
|
53
|
+
* @param filePath - The path to the file.
|
|
54
|
+
* @param data - The data to be written to the file. It can be a string or a Buffer.
|
|
55
|
+
* @param writeFileOptions - The options for writing the file (optional).
|
|
56
|
+
* @param zLibOptions - The options for compressing the data using zlib (optional).
|
|
57
|
+
*/
|
|
58
|
+
export declare function writeGZipSync(filePath: string, data: string | Buffer, writeFileOptions?: WriteFileOptions, zLibOptions?: ZlibOptions): void;
|
|
59
|
+
/**
|
|
60
|
+
* Reads a gzipped file synchronously and returns the uncompressed data as a Buffer.
|
|
61
|
+
*
|
|
62
|
+
* @param path - The path to the gzipped file.
|
|
63
|
+
* @param readFileOptions - The options to pass to the `readFileSync` function.
|
|
64
|
+
* @param zlibOptions - The options to pass to the `unzipSync` function.
|
|
65
|
+
* @returns The uncompressed data as a Buffer.
|
|
66
|
+
*/
|
|
67
|
+
export declare function readGZipSync(path: string, readFileOptions?: {
|
|
68
|
+
encoding?: null;
|
|
69
|
+
flag?: string;
|
|
70
|
+
}, zlibOptions?: ZlibOptions): Buffer;
|
|
71
|
+
/**
|
|
72
|
+
* Serializes an object to JSON and writes it to a file synchronously.
|
|
73
|
+
* @param filePath - The path of the file to write.
|
|
74
|
+
* @param object - The object to serialize and write to the file.
|
|
75
|
+
*/
|
|
76
|
+
export declare function serealizeObjectSync(filePath: string, object: any): void;
|
|
77
|
+
/**
|
|
78
|
+
* Deserializes an object from a file synchronously.
|
|
79
|
+
*
|
|
80
|
+
* @param filePath - The path to the file.
|
|
81
|
+
* @returns The deserialized object.
|
|
82
|
+
*/
|
|
83
|
+
export declare function deserealizeObjectSync(filePath: string): any;
|
|
84
|
+
/**
|
|
85
|
+
* Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`.
|
|
86
|
+
* @return `undefined` upon success.
|
|
87
|
+
* @see {@link unlinkSync}
|
|
88
|
+
*/
|
|
89
|
+
export declare function removeSync(path: PathLike): void;
|
|
90
|
+
/**
|
|
91
|
+
* check if file exists
|
|
92
|
+
*
|
|
93
|
+
*
|
|
94
|
+
* @param path file path
|
|
95
|
+
* @returns true if exists false otherwise
|
|
96
|
+
*
|
|
97
|
+
* @see{@link stat}
|
|
98
|
+
*/
|
|
99
|
+
export declare const exists: (path: PathLike) => Promise<boolean>;
|
|
100
|
+
/**
|
|
101
|
+
* add to file, if the file or folder does not exist it will be recursively created
|
|
102
|
+
* @param path
|
|
103
|
+
* @param data
|
|
104
|
+
* @param options
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
export declare function append(path: PathLike | FileHandle, data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding | null): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* write to file, if the folder does not exist it will be recursively created
|
|
110
|
+
*
|
|
111
|
+
* @param file filename or `FileHandle`
|
|
112
|
+
* @param data
|
|
113
|
+
* @param options
|
|
114
|
+
* @return Fulfills with `undefined` upon success.
|
|
115
|
+
*
|
|
116
|
+
*
|
|
117
|
+
* @see{@link exists}
|
|
118
|
+
* @see{@link mkdir}
|
|
119
|
+
* @see{@link writeFile}
|
|
120
|
+
*/
|
|
121
|
+
export declare function write(file: PathLike | FileHandle, data?: string | NodeJS.ArrayBufferView | Iterable<string | NodeJS.ArrayBufferView> | AsyncIterable<string | NodeJS.ArrayBufferView> | Stream, options?: (ObjectEncodingOptions & {
|
|
122
|
+
mode?: Mode | undefined;
|
|
123
|
+
flag?: OpenMode | undefined;
|
|
124
|
+
} & Abortable) | BufferEncoding | null): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Asynchronously reads the entire contents of a file that contains a valid JSON string, and converts the content into an object.
|
|
127
|
+
*
|
|
128
|
+
* @param file filename or `FileHandle`
|
|
129
|
+
* @param options
|
|
130
|
+
* @param reviver A function that transforms the results. This function is called for each member of the object.
|
|
131
|
+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
|
|
132
|
+
*
|
|
133
|
+
* @see{@link readFile}
|
|
134
|
+
* @see{@link JSON.parse}
|
|
135
|
+
*/
|
|
136
|
+
export declare function readJson<T>(file: PathLike | FileHandle, options?: ({
|
|
137
|
+
encoding?: null | undefined;
|
|
138
|
+
flag?: OpenMode | undefined;
|
|
139
|
+
} & Abortable) | null, reviver?: Reviver<any>): Promise<T>;
|
|
140
|
+
/**
|
|
141
|
+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string, and asynchronously writes data to a file, replacing the file if it already exists.
|
|
142
|
+
*
|
|
143
|
+
* @param file filename or `FileHandle`
|
|
144
|
+
* @param obj A JavaScript value, usually an object or array, to be converted.
|
|
145
|
+
* @param replacer A function that transforms the results.
|
|
146
|
+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
|
|
147
|
+
* @returns
|
|
148
|
+
* @see {@link JSON.stringify}
|
|
149
|
+
* @see {@link write}
|
|
150
|
+
*/
|
|
151
|
+
export declare function writeJson(file: PathLike | FileHandle, obj: any, options?: (ObjectEncodingOptions & {
|
|
152
|
+
mode?: Mode | undefined;
|
|
153
|
+
flag?: OpenMode | undefined;
|
|
154
|
+
} & Abortable) | BufferEncoding | null, replacer?: Replacer<any>, space?: string | number): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* If `path` refers to a symbolic link, then the link is removed without affecting
|
|
157
|
+
* the file or directory to which that link refers. If the `path` refers to a file
|
|
158
|
+
* path that is not a symbolic link, the file is deleted. See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more detail.
|
|
159
|
+
* @return Fulfills with `undefined` upon success.
|
|
160
|
+
* @see {@link unlink}
|
|
161
|
+
*/
|
|
162
|
+
export declare function remove(path: PathLike): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Writes data to a file and creates the necessary directory structure if it doesn't exist.
|
|
165
|
+
*
|
|
166
|
+
* @param path - The path to the file.
|
|
167
|
+
* @param data - The data to write to the file.
|
|
168
|
+
* @param options - The options for writing the file.
|
|
169
|
+
*/
|
|
170
|
+
export declare function writeFileAndDir(path: string, data?: Uint8Array | string, options?: WriteFileOptions): void;
|
|
171
|
+
export declare function copyFileRecursive(origPath: string, destPath: string): void;
|
|
172
|
+
/**
|
|
173
|
+
* remove sync, without throwing NotFound error if it doesn't exist
|
|
174
|
+
* @param path
|
|
175
|
+
* @param options
|
|
176
|
+
*/
|
|
177
|
+
export declare function silentRemove(path: string | URL, options?: FileSystemRemoveOptions): void;
|
|
178
|
+
/**
|
|
179
|
+
* Calculates the SHA256 hash of a file.
|
|
180
|
+
*
|
|
181
|
+
* @param filePath - The path to the file.
|
|
182
|
+
* @returns The SHA256 hash of the file as a hexadecimal string.
|
|
183
|
+
*/
|
|
184
|
+
export declare function sha256(filePath: string): string;
|
|
185
|
+
/**
|
|
186
|
+
* Tails a file and calls the callback with new data when the file is appended.
|
|
187
|
+
* @param filePath
|
|
188
|
+
* @param cb
|
|
189
|
+
* @returns
|
|
190
|
+
*/
|
|
191
|
+
export declare function tail(filePath: string, cb: (chunk: Uint8Array) => void): import("fs").FSWatcher;
|
|
192
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../src/fs/files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,EAKN,IAAI,EACJ,qBAAqB,EACrB,QAAQ,EAER,QAAQ,EAOR,gBAAgB,EAEhB,MAAM,IAAI,CAAC;AACZ,OAAO,EAAc,UAAU,EAAE,eAAe,EAA4C,MAAM,aAAa,CAAC;AAEhH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,OAAO,EAAuB,WAAW,EAAE,MAAM,MAAM,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,gCAAgC,CAAC;AAInE;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,eAAe,EAAE,OAAO,CAAC,EAAE,gBAAgB,QAI3G;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,QAEtD;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAOvE;AAED;;;;;;;;GAQG;AACH,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,QAY9H;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,SAAY,GAAG,MAAM,EAAE,CA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AC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,QASnE;AAKD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,EAAE,OAAO,CAAC,EAAE,uBAAuB,QAOjF;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAO/C;AAGD;;;;;GAKG;AAEH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,0BAwBrE"}
|
package/dist/fs/files.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exists = void 0;
|
|
4
|
+
exports.writeSync = writeSync;
|
|
5
|
+
exports.writeJsonSync = writeJsonSync;
|
|
6
|
+
exports.readJsonSync = readJsonSync;
|
|
7
|
+
exports.insertBetweenPlacweHoldersSync = insertBetweenPlacweHoldersSync;
|
|
8
|
+
exports.fileLinesSync = fileLinesSync;
|
|
9
|
+
exports.writeGZipSync = writeGZipSync;
|
|
10
|
+
exports.readGZipSync = readGZipSync;
|
|
11
|
+
exports.serealizeObjectSync = serealizeObjectSync;
|
|
12
|
+
exports.deserealizeObjectSync = deserealizeObjectSync;
|
|
13
|
+
exports.removeSync = removeSync;
|
|
14
|
+
exports.append = append;
|
|
15
|
+
exports.write = write;
|
|
16
|
+
exports.readJson = readJson;
|
|
17
|
+
exports.writeJson = writeJson;
|
|
18
|
+
exports.remove = remove;
|
|
19
|
+
exports.writeFileAndDir = writeFileAndDir;
|
|
20
|
+
exports.copyFileRecursive = copyFileRecursive;
|
|
21
|
+
exports.silentRemove = silentRemove;
|
|
22
|
+
exports.sha256 = sha256;
|
|
23
|
+
exports.tail = tail;
|
|
24
|
+
const fs_1 = require("fs");
|
|
25
|
+
const promises_1 = require("fs/promises");
|
|
26
|
+
const path_1 = require("path");
|
|
27
|
+
const crypto_1 = require("crypto");
|
|
28
|
+
const zlib_1 = require("zlib");
|
|
29
|
+
const promises_2 = require("@bitbeater/ecma-utils/promises");
|
|
30
|
+
// import { promises, reviver } from 'iggs-utils';
|
|
31
|
+
/** *
|
|
32
|
+
* If `data` is a plain object, it must have an own (not inherited) `toString`function property.
|
|
33
|
+
*
|
|
34
|
+
* The `mode` option only affects the newly created file. See {@link open} for more details.
|
|
35
|
+
*
|
|
36
|
+
* For detailed information, see the documentation of the asynchronous version of
|
|
37
|
+
* this API: {@link writeFile}.
|
|
38
|
+
* @param file filename or file descriptor
|
|
39
|
+
*/
|
|
40
|
+
function writeSync(file, data, options) {
|
|
41
|
+
const dirPath = (0, path_1.dirname)(file.toString());
|
|
42
|
+
if (!(0, fs_1.existsSync)(dirPath))
|
|
43
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(dirPath));
|
|
44
|
+
(0, fs_1.writeFileSync)(file, data || '');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Writes the given object as JSON to the specified file path synchronously.
|
|
48
|
+
* @param path - The file path where the JSON should be written.
|
|
49
|
+
* @param object - The object to be written as JSON.
|
|
50
|
+
*/
|
|
51
|
+
function writeJsonSync(path, object) {
|
|
52
|
+
(0, fs_1.writeFileSync)(path, JSON.stringify(object));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Reads and parses a JSON file synchronously.
|
|
56
|
+
*
|
|
57
|
+
* @template T - The type of the parsed JSON object.
|
|
58
|
+
* @param {string} path - The path to the JSON file.
|
|
59
|
+
* @param {reviver.Reviver<any>} [reviver] - Optional reviver function for JSON.parse.
|
|
60
|
+
* @returns {T} - The parsed JSON object.
|
|
61
|
+
*/
|
|
62
|
+
function readJsonSync(path, reviver) {
|
|
63
|
+
const data = (0, fs_1.readFileSync)(path);
|
|
64
|
+
if (!data)
|
|
65
|
+
return;
|
|
66
|
+
const retVal = JSON.parse(data.toString(), reviver);
|
|
67
|
+
return retVal;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Inserts the specified data between the given placeholders in the file synchronously.
|
|
71
|
+
* If the file does not exist, it creates the file and inserts the data.
|
|
72
|
+
*
|
|
73
|
+
* @param filePath - The path of the file.
|
|
74
|
+
* @param data - The data to be inserted between the placeholders.
|
|
75
|
+
* @param beginPlaceHolder - The beginning placeholder.
|
|
76
|
+
* @param endPlaceHolder - The ending placeholder.
|
|
77
|
+
*/
|
|
78
|
+
function insertBetweenPlacweHoldersSync(filePath, data, beginPlaceHolder, endPlaceHolder) {
|
|
79
|
+
const writeData = (0, fs_1.readFileSync)(filePath, 'utf-8');
|
|
80
|
+
if (!(0, fs_1.existsSync)(filePath)) {
|
|
81
|
+
(0, fs_1.writeFileSync)(filePath, writeData);
|
|
82
|
+
}
|
|
83
|
+
const fileContent = (0, fs_1.readFileSync)(filePath).toString();
|
|
84
|
+
const top = fileContent?.split?.(beginPlaceHolder)?.[0];
|
|
85
|
+
const bottom = fileContent?.split?.(endPlaceHolder).reverse?.()?.[0];
|
|
86
|
+
(0, fs_1.writeFileSync)(filePath, `${top}\n\r${beginPlaceHolder}\n\r${data}\n\r${endPlaceHolder}\n\r${bottom}`);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Reads a file and returns an array of string lines.
|
|
90
|
+
*
|
|
91
|
+
* @param path - The path to the file.
|
|
92
|
+
* @param lineSeparator - The regular expression used to split the file into lines. Defaults to /[\n|\r]/.
|
|
93
|
+
* @returns An array of lines from the file, or null if the file cannot be read.
|
|
94
|
+
*/
|
|
95
|
+
function fileLinesSync(path, lineSeparator = /[\n|\r]/) {
|
|
96
|
+
try {
|
|
97
|
+
const data = (0, fs_1.readFileSync)(path)?.toString();
|
|
98
|
+
if (!data)
|
|
99
|
+
return null;
|
|
100
|
+
return data.split(lineSeparator);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error(error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Writes the given data to a file in GZip format synchronously.
|
|
108
|
+
*
|
|
109
|
+
* @param filePath - The path to the file.
|
|
110
|
+
* @param data - The data to be written to the file. It can be a string or a Buffer.
|
|
111
|
+
* @param writeFileOptions - The options for writing the file (optional).
|
|
112
|
+
* @param zLibOptions - The options for compressing the data using zlib (optional).
|
|
113
|
+
*/
|
|
114
|
+
function writeGZipSync(filePath, data, writeFileOptions, zLibOptions) {
|
|
115
|
+
const buffer = data instanceof Buffer ? data : Buffer.from(data);
|
|
116
|
+
const zippBuffer = (0, zlib_1.gzipSync)(buffer, zLibOptions);
|
|
117
|
+
(0, fs_1.writeFileSync)(filePath, zippBuffer, writeFileOptions);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Reads a gzipped file synchronously and returns the uncompressed data as a Buffer.
|
|
121
|
+
*
|
|
122
|
+
* @param path - The path to the gzipped file.
|
|
123
|
+
* @param readFileOptions - The options to pass to the `readFileSync` function.
|
|
124
|
+
* @param zlibOptions - The options to pass to the `unzipSync` function.
|
|
125
|
+
* @returns The uncompressed data as a Buffer.
|
|
126
|
+
*/
|
|
127
|
+
function readGZipSync(path, readFileOptions, zlibOptions) {
|
|
128
|
+
const data = (0, fs_1.readFileSync)(path, readFileOptions);
|
|
129
|
+
return (0, zlib_1.unzipSync)(data, zlibOptions);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Serializes an object to JSON and writes it to a file synchronously.
|
|
133
|
+
* @param filePath - The path of the file to write.
|
|
134
|
+
* @param object - The object to serialize and write to the file.
|
|
135
|
+
*/
|
|
136
|
+
function serealizeObjectSync(filePath, object) {
|
|
137
|
+
writeGZipSync(filePath, JSON.stringify(object));
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Deserializes an object from a file synchronously.
|
|
141
|
+
*
|
|
142
|
+
* @param filePath - The path to the file.
|
|
143
|
+
* @returns The deserialized object.
|
|
144
|
+
*/
|
|
145
|
+
function deserealizeObjectSync(filePath) {
|
|
146
|
+
return JSON.parse(readGZipSync(filePath).toString());
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`.
|
|
150
|
+
* @return `undefined` upon success.
|
|
151
|
+
* @see {@link unlinkSync}
|
|
152
|
+
*/
|
|
153
|
+
function removeSync(path) {
|
|
154
|
+
try {
|
|
155
|
+
(0, fs_1.unlinkSync)(path);
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
if (e?.code !== 'ENOENT')
|
|
159
|
+
throw e;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* check if file exists
|
|
164
|
+
*
|
|
165
|
+
*
|
|
166
|
+
* @param path file path
|
|
167
|
+
* @returns true if exists false otherwise
|
|
168
|
+
*
|
|
169
|
+
* @see{@link stat}
|
|
170
|
+
*/
|
|
171
|
+
const exists = (path) => (0, promises_1.stat)(path)
|
|
172
|
+
.then(() => true)
|
|
173
|
+
.catch(e => {
|
|
174
|
+
if (e?.code === 'ENOENT')
|
|
175
|
+
return false;
|
|
176
|
+
throw e;
|
|
177
|
+
});
|
|
178
|
+
exports.exists = exists;
|
|
179
|
+
/**
|
|
180
|
+
* add to file, if the file or folder does not exist it will be recursively created
|
|
181
|
+
* @param path
|
|
182
|
+
* @param data
|
|
183
|
+
* @param options
|
|
184
|
+
* @returns
|
|
185
|
+
*/
|
|
186
|
+
function append(path, data, options) {
|
|
187
|
+
return (0, promises_1.appendFile)(path, data, options).catch(error => {
|
|
188
|
+
if (error.code === 'ENOENT')
|
|
189
|
+
return (0, promises_1.mkdir)((0, path_1.dirname)(path.toString()), { recursive: true }).then(() => append(path, data, options));
|
|
190
|
+
return error;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* write to file, if the folder does not exist it will be recursively created
|
|
195
|
+
*
|
|
196
|
+
* @param file filename or `FileHandle`
|
|
197
|
+
* @param data
|
|
198
|
+
* @param options
|
|
199
|
+
* @return Fulfills with `undefined` upon success.
|
|
200
|
+
*
|
|
201
|
+
*
|
|
202
|
+
* @see{@link exists}
|
|
203
|
+
* @see{@link mkdir}
|
|
204
|
+
* @see{@link writeFile}
|
|
205
|
+
*/
|
|
206
|
+
function write(file, data, options) {
|
|
207
|
+
const dirPath = (0, path_1.dirname)(file.toString());
|
|
208
|
+
return (0, exports.exists)(dirPath).then(exist => {
|
|
209
|
+
const _opt = typeof options === 'string' ? { encoding: options } : options;
|
|
210
|
+
let promise = (0, promises_2.of)();
|
|
211
|
+
if (!exist)
|
|
212
|
+
promise = (0, promises_1.mkdir)(dirPath, { ..._opt, recursive: true });
|
|
213
|
+
return promise.then(() => (0, promises_1.writeFile)(file, data || '', options));
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Asynchronously reads the entire contents of a file that contains a valid JSON string, and converts the content into an object.
|
|
218
|
+
*
|
|
219
|
+
* @param file filename or `FileHandle`
|
|
220
|
+
* @param options
|
|
221
|
+
* @param reviver A function that transforms the results. This function is called for each member of the object.
|
|
222
|
+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
|
|
223
|
+
*
|
|
224
|
+
* @see{@link readFile}
|
|
225
|
+
* @see{@link JSON.parse}
|
|
226
|
+
*/
|
|
227
|
+
function readJson(file, options, reviver) {
|
|
228
|
+
return (0, promises_1.readFile)(file, options).then(fileContent => JSON.parse(fileContent.toString(), reviver));
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string, and asynchronously writes data to a file, replacing the file if it already exists.
|
|
232
|
+
*
|
|
233
|
+
* @param file filename or `FileHandle`
|
|
234
|
+
* @param obj A JavaScript value, usually an object or array, to be converted.
|
|
235
|
+
* @param replacer A function that transforms the results.
|
|
236
|
+
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
|
|
237
|
+
* @returns
|
|
238
|
+
* @see {@link JSON.stringify}
|
|
239
|
+
* @see {@link write}
|
|
240
|
+
*/
|
|
241
|
+
function writeJson(file, obj, options, replacer, space) {
|
|
242
|
+
const data = JSON.stringify(obj, replacer, space);
|
|
243
|
+
return write(file, data, options);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* If `path` refers to a symbolic link, then the link is removed without affecting
|
|
247
|
+
* the file or directory to which that link refers. If the `path` refers to a file
|
|
248
|
+
* path that is not a symbolic link, the file is deleted. See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more detail.
|
|
249
|
+
* @return Fulfills with `undefined` upon success.
|
|
250
|
+
* @see {@link unlink}
|
|
251
|
+
*/
|
|
252
|
+
function remove(path) {
|
|
253
|
+
return (0, promises_1.unlink)(path).catch(e => (e.code === 'ENOENT' ? undefined : e));
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Writes data to a file and creates the necessary directory structure if it doesn't exist.
|
|
257
|
+
*
|
|
258
|
+
* @param path - The path to the file.
|
|
259
|
+
* @param data - The data to write to the file.
|
|
260
|
+
* @param options - The options for writing the file.
|
|
261
|
+
*/
|
|
262
|
+
function writeFileAndDir(path, data, options) {
|
|
263
|
+
const parentDir = (0, path_1.resolve)(path, '..');
|
|
264
|
+
if (!(0, fs_1.existsSync)(parentDir)) {
|
|
265
|
+
(0, fs_1.mkdirSync)(parentDir, { recursive: true });
|
|
266
|
+
}
|
|
267
|
+
data = typeof data === 'string' ? new TextEncoder().encode(data) : data;
|
|
268
|
+
(0, fs_1.writeFileSync)(path, data, options);
|
|
269
|
+
}
|
|
270
|
+
function copyFileRecursive(origPath, destPath) {
|
|
271
|
+
const destParentDir = (0, path_1.resolve)(destPath, '..');
|
|
272
|
+
if (!(0, fs_1.existsSync)(destParentDir)) {
|
|
273
|
+
(0, fs_1.mkdirSync)(destParentDir, { recursive: true });
|
|
274
|
+
}
|
|
275
|
+
(0, fs_1.copyFileSync)(origPath, destPath);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* remove sync, without throwing NotFound error if it doesn't exist
|
|
279
|
+
* @param path
|
|
280
|
+
* @param options
|
|
281
|
+
*/
|
|
282
|
+
function silentRemove(path, options) {
|
|
283
|
+
try {
|
|
284
|
+
(0, fs_1.rmSync)(path, options);
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
if ((error.code !== 'ENOENT'))
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Calculates the SHA256 hash of a file.
|
|
293
|
+
*
|
|
294
|
+
* @param filePath - The path to the file.
|
|
295
|
+
* @returns The SHA256 hash of the file as a hexadecimal string.
|
|
296
|
+
*/
|
|
297
|
+
function sha256(filePath) {
|
|
298
|
+
const hash = (0, crypto_1.createHash)('sha256');
|
|
299
|
+
const input = (0, fs_1.readFileSync)(filePath);
|
|
300
|
+
hash.update(input);
|
|
301
|
+
return hash.digest('hex');
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Tails a file and calls the callback with new data when the file is appended.
|
|
305
|
+
* @param filePath
|
|
306
|
+
* @param cb
|
|
307
|
+
* @returns
|
|
308
|
+
*/
|
|
309
|
+
function tail(filePath, cb) {
|
|
310
|
+
let previousFileSize = (0, fs_1.statSync)(filePath).size;
|
|
311
|
+
let previousCheckTime = 0;
|
|
312
|
+
return (0, fs_1.watch)(filePath, () => {
|
|
313
|
+
if (previousCheckTime === Date.now())
|
|
314
|
+
return;
|
|
315
|
+
previousCheckTime = Date.now();
|
|
316
|
+
const fileSize = (0, fs_1.statSync)(filePath).size;
|
|
317
|
+
if (fileSize === 0)
|
|
318
|
+
return;
|
|
319
|
+
const tailSize = fileSize - previousFileSize;
|
|
320
|
+
previousFileSize = fileSize;
|
|
321
|
+
if (tailSize <= 0)
|
|
322
|
+
return;
|
|
323
|
+
const buffer = new Uint8Array(tailSize);
|
|
324
|
+
const fd = (0, fs_1.openSync)(filePath, 'r');
|
|
325
|
+
(0, fs_1.readSync)(fd, buffer, 0, tailSize, fileSize - tailSize);
|
|
326
|
+
(0, fs_1.closeSync)(fd);
|
|
327
|
+
cb(buffer);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
//# sourceMappingURL=files.js.map
|