vivth 1.3.3 → 1.3.5
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 +65 -56
- package/README.src.md +5 -0
- package/package.json +3 -3
- package/src/bundler/CompileJS.mjs +4 -2
- package/src/bundler/adds/ToBundledJSPlugin.mjs +5 -3
- package/src/class/FileSafe.mjs +31 -2
- package/src/class/Paths.mjs +2 -7
- package/src/class/SafeExit.mjs +7 -24
- package/src/class/Setup.mjs +5 -8
- package/src/doc/JSautoDOC.mjs +1 -0
- package/src/doc/correctBeforeParse.mjs +1 -1
- package/types/src/bundler/CompileJS.d.mts +4 -2
- package/types/src/class/FileSafe.d.mts +23 -2
- package/types/src/class/Paths.d.mts +2 -5
- package/types/src/class/SafeExit.d.mts +7 -24
- package/types/src/class/Setup.d.mts +5 -8
package/README.md
CHANGED
|
@@ -31,6 +31,11 @@ npm i vivth
|
|
|
31
31
|
> > - abstracted via `esbuild`;
|
|
32
32
|
> - opionated `compiler`;
|
|
33
33
|
> > - abstracted via `pkg`, `deno`, and `bun`;
|
|
34
|
+
- when using runtime that doesn't provide specific common modules golbally(like `Deno`), dev should
|
|
35
|
+
import it statically from `node:module_name`, example:
|
|
36
|
+
```js
|
|
37
|
+
import process from "node:process";
|
|
38
|
+
```
|
|
34
39
|
|
|
35
40
|
## versions:
|
|
36
41
|
|
|
@@ -189,16 +194,18 @@ export const myBundledPlugin = ToBundledJSPlugin("/myProjectName/src/");
|
|
|
189
194
|
- <i>example</i>:
|
|
190
195
|
|
|
191
196
|
```js
|
|
197
|
+
import process from "node:process";
|
|
192
198
|
import { join } from "node:path";
|
|
199
|
+
|
|
193
200
|
import { CompileJS, Console, Paths, Setup } from "vivth";
|
|
194
201
|
|
|
195
202
|
const { paths, safeExit } = Setup;
|
|
196
203
|
new paths({
|
|
197
|
-
root: process
|
|
204
|
+
root: process.env.INIT_CWD ?? process.cwd(),
|
|
198
205
|
});
|
|
199
206
|
new safeExit({
|
|
200
207
|
eventNames: ["SIGINT", "SIGTERM"],
|
|
201
|
-
terminator: () => process.exit(0),
|
|
208
|
+
terminator: () => process.exit(0),
|
|
202
209
|
listener: (eventName) => {
|
|
203
210
|
process.once(eventName, function () {
|
|
204
211
|
if (!safeExit.instance) {
|
|
@@ -1074,6 +1081,33 @@ eventSignal_instance.remove.ref();
|
|
|
1074
1081
|
|
|
1075
1082
|
- collection of static methods of file access with added safety to mkdir before proceeding;
|
|
1076
1083
|
|
|
1084
|
+
#### reference:`FileSafe.exist`
|
|
1085
|
+
|
|
1086
|
+
- method to safely detects whether filePaths exist;
|
|
1087
|
+
- uses fs/promises access under the hood;
|
|
1088
|
+
- also returning promise of result & error as value;
|
|
1089
|
+
|
|
1090
|
+
```js
|
|
1091
|
+
/**
|
|
1092
|
+
* @param {string} filePath
|
|
1093
|
+
* @returns {ReturnType<typeof TryAsync<true>>}
|
|
1094
|
+
*/
|
|
1095
|
+
```
|
|
1096
|
+
|
|
1097
|
+
- <i>example</i>:
|
|
1098
|
+
|
|
1099
|
+
```js
|
|
1100
|
+
import { join } from "node:path";
|
|
1101
|
+
import { FileSafe, Paths } from "vivth";
|
|
1102
|
+
|
|
1103
|
+
const [, error] = await FileSafe.write(join(Paths.root, "/some/path.mjs"));
|
|
1104
|
+
if (!error) {
|
|
1105
|
+
// file exists
|
|
1106
|
+
} else {
|
|
1107
|
+
// file not exists
|
|
1108
|
+
}
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1077
1111
|
#### reference:`FileSafe.write`
|
|
1078
1112
|
|
|
1079
1113
|
- method to create file safely by recursively mkdir the dirname of the outFile;
|
|
@@ -1110,7 +1144,7 @@ const [, errorWrite] = await FileSafe.write(
|
|
|
1110
1144
|
/**
|
|
1111
1145
|
* @param {Parameters<typeof copyFile>[0]} sourceFile
|
|
1112
1146
|
* @param {Parameters<typeof copyFile>[1]} destinationFile
|
|
1113
|
-
* @param {Parameters<typeof copyFile>[2]} mode
|
|
1147
|
+
* @param {Parameters<typeof copyFile>[2]} [mode]
|
|
1114
1148
|
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
1115
1149
|
*/
|
|
1116
1150
|
```
|
|
@@ -1703,11 +1737,8 @@ const [resultOfMatchedAllAndGrouped, error] =
|
|
|
1703
1737
|
* ```
|
|
1704
1738
|
* - node/bun compatible:
|
|
1705
1739
|
* ```js
|
|
1706
|
-
* process
|
|
1707
|
-
*
|
|
1708
|
-
* - deno: need for `deno run --allow-env --allow-read your_script.ts`:
|
|
1709
|
-
* ```js
|
|
1710
|
-
* Deno.env.get("INIT_CWD") ?? Deno.cwd()
|
|
1740
|
+
* import process from 'node:process';
|
|
1741
|
+
* process.env.INIT_CWD ?? process.cwd()
|
|
1711
1742
|
* ```
|
|
1712
1743
|
* - other: you need to check your JSRuntime for the rootPath reference;
|
|
1713
1744
|
*/
|
|
@@ -1970,14 +2001,10 @@ const [result, error] = await q.callback(keyID, async ({ isLastOnQ }) => {
|
|
|
1970
2001
|
* ['SIGINT', 'SIGTERM']
|
|
1971
2002
|
* ```
|
|
1972
2003
|
* @param {()=>void} options.terminator
|
|
1973
|
-
* - standard node
|
|
2004
|
+
* - standard, process must be imported statically from 'node:process':
|
|
1974
2005
|
* ```js
|
|
1975
2006
|
* () => process.exit(0),
|
|
1976
2007
|
* ```
|
|
1977
|
-
* - Deno:
|
|
1978
|
-
* ```js
|
|
1979
|
-
* () => Deno.exit(0),
|
|
1980
|
-
* ```
|
|
1981
2008
|
* @param {(eventName:string)=>void} [options.listener]
|
|
1982
2009
|
* - default value
|
|
1983
2010
|
* ```js
|
|
@@ -1988,17 +2015,6 @@ const [result, error] = await q.callback(keyID, async ({ isLastOnQ }) => {
|
|
|
1988
2015
|
* });
|
|
1989
2016
|
* }
|
|
1990
2017
|
* ```
|
|
1991
|
-
* - example Deno:
|
|
1992
|
-
* ```js
|
|
1993
|
-
* (eventName) => {
|
|
1994
|
-
* const sig = Deno.signal(eventName);
|
|
1995
|
-
* for await (const _ of sig) {
|
|
1996
|
-
* exiting.correction(true);
|
|
1997
|
-
* sig.dispose();
|
|
1998
|
-
* Console.log(`safe exit via "${eventName}"`);
|
|
1999
|
-
* }
|
|
2000
|
-
* }
|
|
2001
|
-
* ```
|
|
2002
2018
|
* - if your exit callback doesn't uses `process` global object you need to input on the SafeExit instantiation
|
|
2003
2019
|
*/
|
|
2004
2020
|
````
|
|
@@ -2006,22 +2022,19 @@ const [result, error] = await q.callback(keyID, async ({ isLastOnQ }) => {
|
|
|
2006
2022
|
- <i>example</i>:
|
|
2007
2023
|
|
|
2008
2024
|
```js
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
new SafeExit({
|
|
2012
|
-
eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
2013
|
-
terminator : () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
2014
|
-
// optional deno example
|
|
2015
|
-
listener : (eventName) => {
|
|
2016
|
-
const sig = Deno.signal(eventName);
|
|
2017
|
-
for await (const _ of sig) {
|
|
2018
|
-
exiting.correction(true);
|
|
2019
|
-
sig.dispose();
|
|
2020
|
-
Console.log(`safe exit via "${eventName}"`);
|
|
2021
|
-
}
|
|
2022
|
-
}
|
|
2023
|
-
});
|
|
2025
|
+
import process from "node:process";
|
|
2026
|
+
import { SafeExit, Console } from "vivth";
|
|
2024
2027
|
|
|
2028
|
+
new SafeExit({
|
|
2029
|
+
eventNames: ["SIGINT", "SIGTERM", ...eventNames],
|
|
2030
|
+
terminator: () => process.exit(0),
|
|
2031
|
+
listener: (eventName) => {
|
|
2032
|
+
process.once(eventName, function () {
|
|
2033
|
+
SafeExit.instance?.exiting.correction(true);
|
|
2034
|
+
Console.log(`safe exit via "${eventName}"`);
|
|
2035
|
+
});
|
|
2036
|
+
},
|
|
2037
|
+
});
|
|
2025
2038
|
```
|
|
2026
2039
|
|
|
2027
2040
|
#### reference:`SafeExit_instance.exiting`
|
|
@@ -2098,24 +2111,20 @@ const [result, error] = await q.callback(keyID, async ({ isLastOnQ }) => {
|
|
|
2098
2111
|
- <i>example</i>:
|
|
2099
2112
|
|
|
2100
2113
|
```js
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
new Setup.safeExit({
|
|
2104
|
-
// eventNames are blank by default, you need to manually name them all;
|
|
2105
|
-
// 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
2106
|
-
eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
2107
|
-
terminator = () => process.exit(0), // OR on deno () => Deno.exit(0),
|
|
2108
|
-
// optional deno example
|
|
2109
|
-
listener = (eventName) => {
|
|
2110
|
-
const sig = Deno.signal(eventName);
|
|
2111
|
-
for await (const _ of sig) {
|
|
2112
|
-
exiting.correction(true);
|
|
2113
|
-
sig.dispose();
|
|
2114
|
-
Console.log(`safe exit via "${eventName}"`);
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
});
|
|
2114
|
+
import { Setup, Console } from "vivth";
|
|
2118
2115
|
|
|
2116
|
+
new Setup.safeExit({
|
|
2117
|
+
// eventNames are blank by default, you need to manually name them all;
|
|
2118
|
+
// 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
2119
|
+
eventNames: ["SIGINT", "SIGTERM", ...eventNames],
|
|
2120
|
+
terminator: () => process.exit(0),
|
|
2121
|
+
listener: (eventName) => {
|
|
2122
|
+
process.once(eventName, function () {
|
|
2123
|
+
SafeExit.instance?.exiting.correction(true);
|
|
2124
|
+
Console.log(`safe exit via "${eventName}"`);
|
|
2125
|
+
});
|
|
2126
|
+
},
|
|
2127
|
+
});
|
|
2119
2128
|
```
|
|
2120
2129
|
|
|
2121
2130
|
#### reference:`Setup.paths`
|
package/README.src.md
CHANGED
|
@@ -31,6 +31,11 @@ npm i vivth
|
|
|
31
31
|
> > - abstracted via `esbuild`;
|
|
32
32
|
> - opionated `compiler`;
|
|
33
33
|
> > - abstracted via `pkg`, `deno`, and `bun`;
|
|
34
|
+
- when using runtime that doesn't provide specific common modules golbally(like `Deno`), dev should
|
|
35
|
+
import it statically from `node:module_name`, example:
|
|
36
|
+
```js
|
|
37
|
+
import process from 'node:process';
|
|
38
|
+
```
|
|
34
39
|
|
|
35
40
|
## versions:
|
|
36
41
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vivth",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"description": "library primitives",
|
|
5
5
|
"main": "index.mjs",
|
|
6
6
|
"types": "./types/index.d.mts",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
},
|
|
37
37
|
"homepage": "https://github.com/hakimjazuli/vivth#readme",
|
|
38
38
|
"scripts": {
|
|
39
|
-
"dev-wo": "concurrently 'bun ./dev/
|
|
40
|
-
"dev": "concurrently 'bun --watch ./dev/
|
|
39
|
+
"dev-wo": "concurrently 'bun ./dev/auto-doc.mjs' 'bun tsc --watch'",
|
|
40
|
+
"dev": "concurrently 'bun --watch ./dev/auto-doc.mjs' 'bun tsc --watch'"
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false
|
|
43
43
|
}
|
|
@@ -114,16 +114,18 @@ const getBinaryExtension = () => {
|
|
|
114
114
|
* bundledJSFile:string|undefined
|
|
115
115
|
* }>>}
|
|
116
116
|
* @example
|
|
117
|
+
* import process from 'node:process';
|
|
117
118
|
* import { join } from 'node:path';
|
|
119
|
+
*
|
|
118
120
|
* import { CompileJS, Console, Paths, Setup } from 'vivth';
|
|
119
121
|
*
|
|
120
122
|
* const { paths, safeExit } = Setup;
|
|
121
123
|
* new paths({
|
|
122
|
-
* root: process
|
|
124
|
+
* root: process.env.INIT_CWD ?? process.cwd(),
|
|
123
125
|
* });
|
|
124
126
|
* new safeExit({
|
|
125
127
|
* eventNames: ['SIGINT', 'SIGTERM'],
|
|
126
|
-
* terminator: () => process.exit(0),
|
|
128
|
+
* terminator: () => process.exit(0),
|
|
127
129
|
* listener: (eventName) => {
|
|
128
130
|
* process.once(eventName, function () {
|
|
129
131
|
* if (!safeExit.instance) {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import { basename, extname } from 'node:path';
|
|
4
|
-
import { readFile
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
|
|
5
6
|
import { CreateESPlugin } from '../CreateESPlugin.mjs';
|
|
6
7
|
import { GetNamedImportAlias } from '../../function/GetNamedImportAlias.mjs';
|
|
8
|
+
import { FileSafe } from '../../class/FileSafe.mjs';
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* @param {string} originalContent_
|
|
@@ -74,8 +76,8 @@ export function ToBundledJSPlugin(includedInPath) {
|
|
|
74
76
|
new RegExp(`${originalBaseName}\$`.replace('.', '\\.'), ''),
|
|
75
77
|
`${realRef}${fileExt}`
|
|
76
78
|
);
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
79
|
+
const [, error] = await FileSafe.exist(bundledFile);
|
|
80
|
+
if (error) {
|
|
79
81
|
const originalContent = (await readFile(filePath)).toString('utf-8');
|
|
80
82
|
ret.contents = removeVivthDevCodeBlock(originalContent);
|
|
81
83
|
return ret;
|
package/src/class/FileSafe.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { writeFile, mkdir, copyFile, rename, rm, access } from 'node:fs/promises';
|
|
3
4
|
import { dirname } from 'node:path';
|
|
5
|
+
import { constants } from 'node:fs';
|
|
4
6
|
|
|
5
7
|
import { TryAsync } from '../function/TryAsync.mjs';
|
|
6
8
|
|
|
@@ -9,6 +11,33 @@ import { TryAsync } from '../function/TryAsync.mjs';
|
|
|
9
11
|
* - collection of static methods of file access with added safety to mkdir before proceeding;
|
|
10
12
|
*/
|
|
11
13
|
export class FileSafe {
|
|
14
|
+
/**
|
|
15
|
+
* @description
|
|
16
|
+
* - method to safely detects whether filePaths exist;
|
|
17
|
+
* - uses fs/promises access under the hood;
|
|
18
|
+
* - also returning promise of result & error as value;
|
|
19
|
+
* @param {string} filePath
|
|
20
|
+
* @returns {ReturnType<typeof TryAsync<true>>}
|
|
21
|
+
* @example
|
|
22
|
+
* import { join } from 'node:path';
|
|
23
|
+
* import { FileSafe, Paths } from 'vivth';
|
|
24
|
+
*
|
|
25
|
+
* const [, error] = await FileSafe.write(
|
|
26
|
+
* join(Paths.root, '/some/path.mjs'),
|
|
27
|
+
* );
|
|
28
|
+
* if (!error) {
|
|
29
|
+
* // file exists
|
|
30
|
+
* } else {
|
|
31
|
+
* // file not exists
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
static exist = async (filePath) => {
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
return await TryAsync(async () => {
|
|
37
|
+
await access(filePath, constants.F_OK);
|
|
38
|
+
return true;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
12
41
|
/**
|
|
13
42
|
* @description
|
|
14
43
|
* - method to create file safely by recursively mkdir the dirname of the outFile;
|
|
@@ -42,7 +71,7 @@ export class FileSafe {
|
|
|
42
71
|
* - also returning promise of result & error as value;
|
|
43
72
|
* @param {Parameters<typeof copyFile>[0]} sourceFile
|
|
44
73
|
* @param {Parameters<typeof copyFile>[1]} destinationFile
|
|
45
|
-
* @param {Parameters<typeof copyFile>[2]} mode
|
|
74
|
+
* @param {Parameters<typeof copyFile>[2]} [mode]
|
|
46
75
|
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
47
76
|
* @example
|
|
48
77
|
* import { join } from 'node:path';
|
package/src/class/Paths.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import { relative } from 'node:path';
|
|
4
|
-
|
|
5
3
|
import { Console } from './Console.mjs';
|
|
6
4
|
|
|
7
5
|
/**
|
|
@@ -25,11 +23,8 @@ export class Paths {
|
|
|
25
23
|
* ```
|
|
26
24
|
* - node/bun compatible:
|
|
27
25
|
* ```js
|
|
28
|
-
* process
|
|
29
|
-
*
|
|
30
|
-
* - deno: need for `deno run --allow-env --allow-read your_script.ts`:
|
|
31
|
-
* ```js
|
|
32
|
-
* Deno.env.get("INIT_CWD") ?? Deno.cwd()
|
|
26
|
+
* import process from 'node:process';
|
|
27
|
+
* process.env.INIT_CWD ?? process.cwd()
|
|
33
28
|
* ```
|
|
34
29
|
* - other: you need to check your JSRuntime for the rootPath reference;
|
|
35
30
|
* @example
|
package/src/class/SafeExit.mjs
CHANGED
|
@@ -36,14 +36,10 @@ export class SafeExit {
|
|
|
36
36
|
* ['SIGINT', 'SIGTERM']
|
|
37
37
|
* ```
|
|
38
38
|
* @param {()=>void} options.terminator
|
|
39
|
-
* - standard node
|
|
39
|
+
* - standard, process must be imported statically from 'node:process':
|
|
40
40
|
* ```js
|
|
41
41
|
* () => process.exit(0),
|
|
42
42
|
* ```
|
|
43
|
-
* - Deno:
|
|
44
|
-
* ```js
|
|
45
|
-
* () => Deno.exit(0),
|
|
46
|
-
* ```
|
|
47
43
|
* @param {(eventName:string)=>void} [options.listener]
|
|
48
44
|
* - default value
|
|
49
45
|
* ```js
|
|
@@ -54,32 +50,19 @@ export class SafeExit {
|
|
|
54
50
|
* });
|
|
55
51
|
* }
|
|
56
52
|
* ```
|
|
57
|
-
* - example Deno:
|
|
58
|
-
* ```js
|
|
59
|
-
* (eventName) => {
|
|
60
|
-
* const sig = Deno.signal(eventName);
|
|
61
|
-
* for await (const _ of sig) {
|
|
62
|
-
* exiting.correction(true);
|
|
63
|
-
* sig.dispose();
|
|
64
|
-
* Console.log(`safe exit via "${eventName}"`);
|
|
65
|
-
* }
|
|
66
|
-
* }
|
|
67
|
-
* ```
|
|
68
53
|
* - if your exit callback doesn't uses `process` global object you need to input on the SafeExit instantiation
|
|
69
54
|
* @example
|
|
55
|
+
* import process from 'node:process';
|
|
70
56
|
* import { SafeExit, Console } from 'vivth';
|
|
71
57
|
*
|
|
72
58
|
* new SafeExit({
|
|
73
59
|
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
74
|
-
* terminator : () => process.exit(0),
|
|
75
|
-
* // optional deno example
|
|
60
|
+
* terminator : () => process.exit(0),
|
|
76
61
|
* listener : (eventName) => {
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* Console.log(`safe exit via "${eventName}"`);
|
|
82
|
-
* }
|
|
62
|
+
* process.once(eventName, function () {
|
|
63
|
+
* SafeExit.instance?.exiting.correction(true);
|
|
64
|
+
* Console.log(`safe exit via "${eventName}"`);
|
|
65
|
+
* });
|
|
83
66
|
* }
|
|
84
67
|
* });
|
|
85
68
|
*/
|
package/src/class/Setup.mjs
CHANGED
|
@@ -20,15 +20,12 @@ export class Setup {
|
|
|
20
20
|
* // eventNames are blank by default, you need to manually name them all;
|
|
21
21
|
* // 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
22
22
|
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
23
|
-
* terminator
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* for await (const _ of sig) {
|
|
28
|
-
* exiting.correction(true);
|
|
29
|
-
* sig.dispose();
|
|
23
|
+
* terminator : () => process.exit(0),
|
|
24
|
+
* listener : (eventName) => {
|
|
25
|
+
* process.once(eventName, function () {
|
|
26
|
+
* SafeExit.instance?.exiting.correction(true);
|
|
30
27
|
* Console.log(`safe exit via "${eventName}"`);
|
|
31
|
-
* }
|
|
28
|
+
* });
|
|
32
29
|
* }
|
|
33
30
|
* });
|
|
34
31
|
*/
|
package/src/doc/JSautoDOC.mjs
CHANGED
|
@@ -5,6 +5,7 @@ import { readFile } from 'node:fs/promises';
|
|
|
5
5
|
|
|
6
6
|
import prettier from 'prettier';
|
|
7
7
|
import chokidar from 'chokidar';
|
|
8
|
+
|
|
8
9
|
import { EventSignal } from '../class/EventSignal.mjs';
|
|
9
10
|
import { parsedFile } from './parsedFile.mjs';
|
|
10
11
|
import { SafeExit } from '../class/SafeExit.mjs';
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
4
|
import { basename } from 'node:path';
|
|
4
5
|
|
|
5
6
|
import { TryAsync } from '../function/TryAsync.mjs';
|
|
6
|
-
import { readFile, writeFile } from 'node:fs/promises';
|
|
7
7
|
import { Console } from '../class/Console.mjs';
|
|
8
8
|
import { Timeout } from '../function/Timeout.mjs';
|
|
9
9
|
import { LitExp } from '../class/LitExp.mjs';
|
|
@@ -47,16 +47,18 @@
|
|
|
47
47
|
* bundledJSFile:string|undefined
|
|
48
48
|
* }>>}
|
|
49
49
|
* @example
|
|
50
|
+
* import process from 'node:process';
|
|
50
51
|
* import { join } from 'node:path';
|
|
52
|
+
*
|
|
51
53
|
* import { CompileJS, Console, Paths, Setup } from 'vivth';
|
|
52
54
|
*
|
|
53
55
|
* const { paths, safeExit } = Setup;
|
|
54
56
|
* new paths({
|
|
55
|
-
* root: process
|
|
57
|
+
* root: process.env.INIT_CWD ?? process.cwd(),
|
|
56
58
|
* });
|
|
57
59
|
* new safeExit({
|
|
58
60
|
* eventNames: ['SIGINT', 'SIGTERM'],
|
|
59
|
-
* terminator: () => process.exit(0),
|
|
61
|
+
* terminator: () => process.exit(0),
|
|
60
62
|
* listener: (eventName) => {
|
|
61
63
|
* process.once(eventName, function () {
|
|
62
64
|
* if (!safeExit.instance) {
|
|
@@ -3,6 +3,27 @@
|
|
|
3
3
|
* - collection of static methods of file access with added safety to mkdir before proceeding;
|
|
4
4
|
*/
|
|
5
5
|
export class FileSafe {
|
|
6
|
+
/**
|
|
7
|
+
* @description
|
|
8
|
+
* - method to safely detects whether filePaths exist;
|
|
9
|
+
* - uses fs/promises access under the hood;
|
|
10
|
+
* - also returning promise of result & error as value;
|
|
11
|
+
* @param {string} filePath
|
|
12
|
+
* @returns {ReturnType<typeof TryAsync<true>>}
|
|
13
|
+
* @example
|
|
14
|
+
* import { join } from 'node:path';
|
|
15
|
+
* import { FileSafe, Paths } from 'vivth';
|
|
16
|
+
*
|
|
17
|
+
* const [, error] = await FileSafe.write(
|
|
18
|
+
* join(Paths.root, '/some/path.mjs'),
|
|
19
|
+
* );
|
|
20
|
+
* if (!error) {
|
|
21
|
+
* // file exists
|
|
22
|
+
* } else {
|
|
23
|
+
* // file not exists
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
static exist: (filePath: string) => ReturnType<typeof TryAsync<true>>;
|
|
6
27
|
/**
|
|
7
28
|
* @description
|
|
8
29
|
* - method to create file safely by recursively mkdir the dirname of the outFile;
|
|
@@ -28,7 +49,7 @@ export class FileSafe {
|
|
|
28
49
|
* - also returning promise of result & error as value;
|
|
29
50
|
* @param {Parameters<typeof copyFile>[0]} sourceFile
|
|
30
51
|
* @param {Parameters<typeof copyFile>[1]} destinationFile
|
|
31
|
-
* @param {Parameters<typeof copyFile>[2]} mode
|
|
52
|
+
* @param {Parameters<typeof copyFile>[2]} [mode]
|
|
32
53
|
* @returns {ReturnType<typeof TryAsync<void>>}
|
|
33
54
|
* @example
|
|
34
55
|
* import { join } from 'node:path';
|
|
@@ -40,7 +61,7 @@ export class FileSafe {
|
|
|
40
61
|
* { encoding: 'utf-8' }
|
|
41
62
|
* );
|
|
42
63
|
*/
|
|
43
|
-
static copy: (sourceFile: Parameters<typeof copyFile>[0], destinationFile: Parameters<typeof copyFile>[1], mode
|
|
64
|
+
static copy: (sourceFile: Parameters<typeof copyFile>[0], destinationFile: Parameters<typeof copyFile>[1], mode?: Parameters<typeof copyFile>[2]) => ReturnType<typeof TryAsync<void>>;
|
|
44
65
|
/**
|
|
45
66
|
* @description
|
|
46
67
|
* - method to rename file/dir safely by recursively mkdir the dirname of the dest;
|
|
@@ -51,11 +51,8 @@ export class Paths {
|
|
|
51
51
|
* ```
|
|
52
52
|
* - node/bun compatible:
|
|
53
53
|
* ```js
|
|
54
|
-
* process
|
|
55
|
-
*
|
|
56
|
-
* - deno: need for `deno run --allow-env --allow-read your_script.ts`:
|
|
57
|
-
* ```js
|
|
58
|
-
* Deno.env.get("INIT_CWD") ?? Deno.cwd()
|
|
54
|
+
* import process from 'node:process';
|
|
55
|
+
* process.env.INIT_CWD ?? process.cwd()
|
|
59
56
|
* ```
|
|
60
57
|
* - other: you need to check your JSRuntime for the rootPath reference;
|
|
61
58
|
* @example
|
|
@@ -26,14 +26,10 @@ export class SafeExit {
|
|
|
26
26
|
* ['SIGINT', 'SIGTERM']
|
|
27
27
|
* ```
|
|
28
28
|
* @param {()=>void} options.terminator
|
|
29
|
-
* - standard node
|
|
29
|
+
* - standard, process must be imported statically from 'node:process':
|
|
30
30
|
* ```js
|
|
31
31
|
* () => process.exit(0),
|
|
32
32
|
* ```
|
|
33
|
-
* - Deno:
|
|
34
|
-
* ```js
|
|
35
|
-
* () => Deno.exit(0),
|
|
36
|
-
* ```
|
|
37
33
|
* @param {(eventName:string)=>void} [options.listener]
|
|
38
34
|
* - default value
|
|
39
35
|
* ```js
|
|
@@ -44,32 +40,19 @@ export class SafeExit {
|
|
|
44
40
|
* });
|
|
45
41
|
* }
|
|
46
42
|
* ```
|
|
47
|
-
* - example Deno:
|
|
48
|
-
* ```js
|
|
49
|
-
* (eventName) => {
|
|
50
|
-
* const sig = Deno.signal(eventName);
|
|
51
|
-
* for await (const _ of sig) {
|
|
52
|
-
* exiting.correction(true);
|
|
53
|
-
* sig.dispose();
|
|
54
|
-
* Console.log(`safe exit via "${eventName}"`);
|
|
55
|
-
* }
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
43
|
* - if your exit callback doesn't uses `process` global object you need to input on the SafeExit instantiation
|
|
59
44
|
* @example
|
|
45
|
+
* import process from 'node:process';
|
|
60
46
|
* import { SafeExit, Console } from 'vivth';
|
|
61
47
|
*
|
|
62
48
|
* new SafeExit({
|
|
63
49
|
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
64
|
-
* terminator : () => process.exit(0),
|
|
65
|
-
* // optional deno example
|
|
50
|
+
* terminator : () => process.exit(0),
|
|
66
51
|
* listener : (eventName) => {
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* Console.log(`safe exit via "${eventName}"`);
|
|
72
|
-
* }
|
|
52
|
+
* process.once(eventName, function () {
|
|
53
|
+
* SafeExit.instance?.exiting.correction(true);
|
|
54
|
+
* Console.log(`safe exit via "${eventName}"`);
|
|
55
|
+
* });
|
|
73
56
|
* }
|
|
74
57
|
* });
|
|
75
58
|
*/
|
|
@@ -13,15 +13,12 @@ export class Setup {
|
|
|
13
13
|
* // eventNames are blank by default, you need to manually name them all;
|
|
14
14
|
* // 'exit' will be omited, as it might cause async callbacks failed to execute;
|
|
15
15
|
* eventNames: ['SIGINT', 'SIGTERM', ...eventNames],
|
|
16
|
-
* terminator
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* for await (const _ of sig) {
|
|
21
|
-
* exiting.correction(true);
|
|
22
|
-
* sig.dispose();
|
|
16
|
+
* terminator : () => process.exit(0),
|
|
17
|
+
* listener : (eventName) => {
|
|
18
|
+
* process.once(eventName, function () {
|
|
19
|
+
* SafeExit.instance?.exiting.correction(true);
|
|
23
20
|
* Console.log(`safe exit via "${eventName}"`);
|
|
24
|
-
* }
|
|
21
|
+
* });
|
|
25
22
|
* }
|
|
26
23
|
* });
|
|
27
24
|
*/
|