cno-bedrock 0.0.4 → 0.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/CHANGELOG.md +38 -0
- package/package.json +116 -116
- package/scripts/generate-docs.js +0 -0
- package/src/cno-assert.js +222 -15
- package/src/cno-assert.test.js +331 -0
- package/src/cno-base64.js +230 -0
- package/src/cno-base64.test.js +262 -0
- package/src/cno-bytes.js +387 -0
- package/src/cno-bytes.test.js +222 -0
- package/src/cno-constants.js +121 -0
- package/src/cno-files.js +2135 -0
- package/src/cno-files.test.js +939 -0
- package/src/cno-math.js +407 -0
- package/src/cno-math.test.js +249 -0
- package/src/cno-noop.js +6 -2
- package/src/cno-options.js +73 -4
- package/src/cno-options.test.js +65 -0
- package/src/cno-sha2-256.js +831 -0
- package/src/cno-sha2-256.test.js +697 -0
- package/src/cno-test-matrix.js +709 -0
- package/src/cno-test-matrix.test.js +199 -0
- package/src/cno-timed-cache-map.js +103 -0
- package/src/cno-timed-cache-map.test.js +113 -0
- package/src/cno-tree.js +365 -0
- package/src/cno-tree.test.js +147 -0
- package/src/cno-utility.js +545 -2
- package/src/cno-utility.test.js +276 -0
- package/src/cno-vector.js +1053 -0
- package/src/cno-vector.test.js +523 -0
- package/src/lib.js +5 -138
- package/src/lib.test.js +19 -7
- package/src/stream-test.js +116 -0
- package/test/big-lorem.txt +29 -0
package/src/cno-files.js
ADDED
|
@@ -0,0 +1,2135 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
# [cno-files.js](src/cno-files.js)
|
|
4
|
+
> Micropackage: FileSystem-related convenience functions.
|
|
5
|
+
|
|
6
|
+
Author: Anadian
|
|
7
|
+
|
|
8
|
+
Code license: MIT
|
|
9
|
+
```
|
|
10
|
+
Copyright 2025 Anadian
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
12
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
13
|
+
without restriction, including without limitation the rights to use, copy, modify,
|
|
14
|
+
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to the following
|
|
16
|
+
conditions:
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all copies
|
|
18
|
+
or substantial portions of the Software.
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
23
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
24
|
+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
|
+
```
|
|
26
|
+
Documentation License: [](http://creativecommons.org/licenses/by-sa/4.0/)
|
|
27
|
+
> The source-code comments and documentation are written in [GitHub Flavored Markdown](https://github.github.com/gfm/).
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
//# Dependencies
|
|
32
|
+
//## Internal
|
|
33
|
+
import deriveOptions from './cno-options.js';
|
|
34
|
+
import { getSHA256FromFilePath, getBase16FromBuffer, getBase64FromBuffer } from './cno-bytes.js';
|
|
35
|
+
import { isDefined } from './cno-utility.js';
|
|
36
|
+
import TimedCacheMap from './cno-timed-cache-map.js';
|
|
37
|
+
//## Standard
|
|
38
|
+
import FSNS from 'node:fs/promises';
|
|
39
|
+
import PathNS from 'node:path';
|
|
40
|
+
import URLNS from 'node:url';
|
|
41
|
+
import OSNS from 'node:os';
|
|
42
|
+
//## External
|
|
43
|
+
import HJSON from 'hjson';
|
|
44
|
+
|
|
45
|
+
//# Constants
|
|
46
|
+
const FILENAME = 'cno-files.js';
|
|
47
|
+
//## Errors
|
|
48
|
+
|
|
49
|
+
//# Global Variables
|
|
50
|
+
/**## Functions*/
|
|
51
|
+
/**
|
|
52
|
+
### getHJSONFromPath
|
|
53
|
+
> Reads the file at the given path, parses its content as HJSON, and returns it as an object.
|
|
54
|
+
|
|
55
|
+
#### Parametres
|
|
56
|
+
| name | type | description |
|
|
57
|
+
| --- | --- | --- |
|
|
58
|
+
| path | string | The path of the file to parse. |
|
|
59
|
+
|
|
60
|
+
#### Returns
|
|
61
|
+
| type | description |
|
|
62
|
+
| --- | --- |
|
|
63
|
+
| Promise<object> | A promise which resolves to the parsed object. |
|
|
64
|
+
|
|
65
|
+
#### Throws
|
|
66
|
+
| code | type | condition |
|
|
67
|
+
| --- | --- | --- |
|
|
68
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
69
|
+
|
|
70
|
+
#### History
|
|
71
|
+
| version | change |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| 0.0.1 | WIP |
|
|
74
|
+
*/
|
|
75
|
+
function getHJSONFromPath( path ){
|
|
76
|
+
const FUNCTION_NAME = 'getHJSONFromPath';
|
|
77
|
+
// Variables
|
|
78
|
+
var _return = Promise.resolve(null);
|
|
79
|
+
var return_error = null;
|
|
80
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${path}`});
|
|
81
|
+
// Parametre checks
|
|
82
|
+
if( typeof(path) !== 'string' ){
|
|
83
|
+
return_error = new TypeError('Param "path" is not of type string.');
|
|
84
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
85
|
+
throw return_error;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Function
|
|
89
|
+
_return = FSNS.readFile( path, 'utf8' ).then(
|
|
90
|
+
( file_string ) => {
|
|
91
|
+
try{
|
|
92
|
+
_return = HJSON.parse( file_string );
|
|
93
|
+
} catch( error ){
|
|
94
|
+
return_error = new Error( `For path "${path}": HJSON.parse threw an error: ${error}`, { cause: error } );
|
|
95
|
+
throw return_error;
|
|
96
|
+
}
|
|
97
|
+
return _return;
|
|
98
|
+
},
|
|
99
|
+
( error ) => {
|
|
100
|
+
return_error = new Error( `For path "${path}": FSNS.readFile threw an error: ${error}`, { cause: error } );
|
|
101
|
+
throw return_error;
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// Return
|
|
106
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
107
|
+
return _return;
|
|
108
|
+
} // getHJSONFromPath
|
|
109
|
+
/**
|
|
110
|
+
### getHJSONFromObject
|
|
111
|
+
> Serialises the given object as [HJSON](https://hjson.github.io/) and returns the resulting string.
|
|
112
|
+
|
|
113
|
+
#### Parametres
|
|
114
|
+
| name | type | description |
|
|
115
|
+
| --- | --- | --- |
|
|
116
|
+
| input_object | object | The object to serialise. |
|
|
117
|
+
| hjson_stringify_options | object | Options passed to [HJSON.stringify](https://hjson.github.io/api.html#stringify). \[default: null\] |
|
|
118
|
+
|
|
119
|
+
#### Returns
|
|
120
|
+
| type | description |
|
|
121
|
+
| --- | --- |
|
|
122
|
+
| string | The HJSON string representing the given object. |
|
|
123
|
+
|
|
124
|
+
#### Throws
|
|
125
|
+
| code | type | condition |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
128
|
+
|
|
129
|
+
#### History
|
|
130
|
+
| version | change |
|
|
131
|
+
| --- | --- |
|
|
132
|
+
| 0.1.1 | WIP |
|
|
133
|
+
*/
|
|
134
|
+
function getHJSONFromObject( input_object, hjson_stringify_options = null ){
|
|
135
|
+
var _return = null;
|
|
136
|
+
var return_error = null;
|
|
137
|
+
var effective_hjson_stringify_options = hjson_stringify_options ?? {
|
|
138
|
+
bracesSameLine: true,
|
|
139
|
+
quotes: 'all',
|
|
140
|
+
separator: true,
|
|
141
|
+
space: '\t'
|
|
142
|
+
};
|
|
143
|
+
try{
|
|
144
|
+
_return = HJSON.stringify( input_object, effective_hjson_stringify_options );
|
|
145
|
+
} catch( error )/* c8 ignore start */{
|
|
146
|
+
return_error = new Error( `HJSON.stringify threw an error: ${error}`, { cause: error } );
|
|
147
|
+
throw return_error;
|
|
148
|
+
} /* c8 ignore stop */
|
|
149
|
+
return _return;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
### writeHJSONToPath
|
|
153
|
+
> Serialises the given object as [HJSON](https://hjson.github.io/) and writes it to the given path.
|
|
154
|
+
|
|
155
|
+
#### Parametres
|
|
156
|
+
| name | type | description |
|
|
157
|
+
| --- | --- | --- |
|
|
158
|
+
| input_object | object | The object to serialise. \[default: null\] |
|
|
159
|
+
| path | string | The path of the file to write to. \[default: ''\] |
|
|
160
|
+
|
|
161
|
+
#### Returns
|
|
162
|
+
| type | description |
|
|
163
|
+
| --- | --- |
|
|
164
|
+
| Promise<void> | A promise which resolves when the file has been written. |
|
|
165
|
+
|
|
166
|
+
#### Throws
|
|
167
|
+
| code | type | condition |
|
|
168
|
+
| --- | --- | --- |
|
|
169
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
170
|
+
|
|
171
|
+
#### History
|
|
172
|
+
| version | change |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| 0.1.1 | WIP |
|
|
175
|
+
*/
|
|
176
|
+
function writeHJSONToPath( input_object = null, path = '' ){
|
|
177
|
+
var return_error = null;
|
|
178
|
+
var _return = Promise.resolve(null).then(
|
|
179
|
+
() => {
|
|
180
|
+
var hjson_string = getHJSONFromObject( input_object );
|
|
181
|
+
return FSNS.writeFile( path, hjson_string, 'utf8' ).then(
|
|
182
|
+
null,
|
|
183
|
+
( error ) => {
|
|
184
|
+
return_error = new Error( `FSNS.writeFile rejected with error: ${error}`, { cause: error } );
|
|
185
|
+
throw return_error;
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
},
|
|
189
|
+
null
|
|
190
|
+
).then(
|
|
191
|
+
null,
|
|
192
|
+
( error ) => {
|
|
193
|
+
return_error = new Error( `For path "${path}": ${error}`, { cause: error } );
|
|
194
|
+
throw return_error;
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
return _return;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
### getDirentArrayFromDirectoryPath
|
|
201
|
+
> Reads the directory at the given path and returns an array of [`Dirent`](https://nodejs.org/api/fs.html#class-dirent) objects describing its entries.
|
|
202
|
+
|
|
203
|
+
#### Parametres
|
|
204
|
+
| name | type | description |
|
|
205
|
+
| --- | --- | --- |
|
|
206
|
+
| path | string | The path of the directory to read. \[default: ''\] |
|
|
207
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
208
|
+
|
|
209
|
+
##### `options` Properties
|
|
210
|
+
| name | type | default | description |
|
|
211
|
+
| --- | --- | --- | --- |
|
|
212
|
+
| encoding | string | 'utf8' | The encoding to use for the entry names. |
|
|
213
|
+
| withFileTypes | boolean | true | Must be `true`; the returned array contains `Dirent` objects rather than plain name strings. |
|
|
214
|
+
|
|
215
|
+
#### Returns
|
|
216
|
+
| type | description |
|
|
217
|
+
| --- | --- |
|
|
218
|
+
| Promise<Array> | A promise which resolves to an array of `Dirent` objects. |
|
|
219
|
+
|
|
220
|
+
#### Throws
|
|
221
|
+
| code | type | condition |
|
|
222
|
+
| --- | --- | --- |
|
|
223
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
224
|
+
| 'ERR_INVALID_ARG_VALUE' | TypeError | Thrown if `options.withFileTypes` is not `true`. |
|
|
225
|
+
|
|
226
|
+
#### History
|
|
227
|
+
| version | change |
|
|
228
|
+
| --- | --- |
|
|
229
|
+
| 0.1.1 | Added `input_options` and the `withFileTypes` requirement. |
|
|
230
|
+
| 0.0.1 | WIP |
|
|
231
|
+
*/
|
|
232
|
+
function getDirentArrayFromDirectoryPath( path = '', input_options = null ){
|
|
233
|
+
const FUNCTION_NAME = 'getDirentArrayFromDirectoryPath';
|
|
234
|
+
const DEFAULT_OPTIONS = {
|
|
235
|
+
encoding: 'utf8', // The encoding to use for the entry names.
|
|
236
|
+
withFileTypes: true // Must be `true`; the returned array contains `Dirent` objects rather than plain name strings.
|
|
237
|
+
};// Variables
|
|
238
|
+
var _return = null;
|
|
239
|
+
var return_error = null;
|
|
240
|
+
var options = {};
|
|
241
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${path}`});
|
|
242
|
+
// Parametre checks
|
|
243
|
+
if( typeof(path) !== 'string' ){
|
|
244
|
+
return_error = new TypeError('Param "path" is not of type string.');
|
|
245
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
246
|
+
throw return_error;
|
|
247
|
+
}
|
|
248
|
+
// Options
|
|
249
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
250
|
+
if( options.withFileTypes !== true ){
|
|
251
|
+
return_error = new TypeError('Option "withFileTypes" must be `true`; this function only returns `Dirent` objects.');
|
|
252
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
253
|
+
throw return_error;
|
|
254
|
+
}
|
|
255
|
+
// Function
|
|
256
|
+
_return = FSNS.readdir( path, { encoding: options.encoding, withFileTypes: true } ).then(
|
|
257
|
+
null,
|
|
258
|
+
( error ) => {
|
|
259
|
+
return_error = new Error( `For path "${path}": FSNS.readdir rejected with error: ${error}`, { cause: error } );
|
|
260
|
+
throw return_error;
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
// Return
|
|
264
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
265
|
+
return _return;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
### getUnixTypeFromStatObject
|
|
269
|
+
> Returns a string describing the kind of filesystem entry a [`stat`](https://nodejs.org/api/fs.html#class-stats) object represents, e.g. `'file'` or `'directory'`, by [resolving the type bitflags in its `mode`](https://nodejs.org/api/fs.html#file-type-constants). Works with both number- and `BigInt`-valued stat objects.
|
|
270
|
+
|
|
271
|
+
#### Parametres
|
|
272
|
+
| name | type | description |
|
|
273
|
+
| --- | --- | --- |
|
|
274
|
+
| stat_object | object | The stat object to inspect. |
|
|
275
|
+
|
|
276
|
+
#### Returns
|
|
277
|
+
| type | description |
|
|
278
|
+
| --- | --- |
|
|
279
|
+
| string | One of `'fifo'`, `'character-device'`, `'directory'`, `'block-device'`, `'file'`, `'symbolic-link'`, `'socket'`, or `'unknown'`. |
|
|
280
|
+
|
|
281
|
+
#### History
|
|
282
|
+
| version | change |
|
|
283
|
+
| --- | --- |
|
|
284
|
+
| 0.1.1 | Handles `BigInt` modes and non-object input; WIP |
|
|
285
|
+
*/
|
|
286
|
+
function getUnixTypeFromStatObject( stat_object ){
|
|
287
|
+
var _return = 'unknown';
|
|
288
|
+
var mode_number = Number( stat_object?.mode ?? Number.NaN );
|
|
289
|
+
if( Number.isNaN( mode_number ) === true ){
|
|
290
|
+
return _return;
|
|
291
|
+
}
|
|
292
|
+
switch( ( mode_number & FSNS.constants.S_IFMT ) ){
|
|
293
|
+
case FSNS.constants.S_IFIFO: _return = 'fifo'; break;
|
|
294
|
+
case FSNS.constants.S_IFCHR: _return = 'character-device'; break;
|
|
295
|
+
case FSNS.constants.S_IFDIR: _return = 'directory'; break;
|
|
296
|
+
case FSNS.constants.S_IFBLK: _return = 'block-device'; break;
|
|
297
|
+
case FSNS.constants.S_IFREG: _return = 'file'; break;
|
|
298
|
+
case FSNS.constants.S_IFLNK: _return = 'symbolic-link'; break;
|
|
299
|
+
case FSNS.constants.S_IFSOCK: _return = 'socket'; break;
|
|
300
|
+
default: _return = 'unknown'; break;
|
|
301
|
+
}
|
|
302
|
+
return _return;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
### getUnixTypeFromDirentObject
|
|
306
|
+
> Returns a string describing the kind of filesystem entry the given [`Dirent`](https://nodejs.org/api/fs.html#class-dirent) object represents, e.g. `'file'` or `'directory'`. The type is taken directly from the `Dirent`'s own type-test methods so symlinks are classified as symlinks, not by their targets.
|
|
307
|
+
|
|
308
|
+
#### Parametres
|
|
309
|
+
| name | type | description |
|
|
310
|
+
| --- | --- | --- |
|
|
311
|
+
| dirent_object | object | The `Dirent`-like object to inspect; anything with the standard `Dirent` type-test methods (`isFile`, `isDirectory`, `isSymbolicLink`, `isFIFO`, `isCharacterDevice`, `isBlockDevice`, `isSocket`) will do. |
|
|
312
|
+
|
|
313
|
+
#### Returns
|
|
314
|
+
| type | description |
|
|
315
|
+
| --- | --- |
|
|
316
|
+
| string | One of `'file'`, `'directory'`, `'symbolic-link'`, `'fifo'`, `'character-device'`, `'block-device'`, `'socket'`, or `'unknown'`. |
|
|
317
|
+
|
|
318
|
+
#### History
|
|
319
|
+
| version | change |
|
|
320
|
+
| --- | --- |
|
|
321
|
+
| 0.1.1 | Simplified to a flat chain of type tests, handling `Dirent`-like objects and non-objects; WIP |
|
|
322
|
+
*/
|
|
323
|
+
function getUnixTypeFromDirentObject( dirent_object ){
|
|
324
|
+
var _return = 'unknown';
|
|
325
|
+
if( typeof( dirent_object?.isFile ) !== 'function' ){
|
|
326
|
+
return _return;
|
|
327
|
+
}
|
|
328
|
+
if( dirent_object.isFile() === true ) _return = 'file';
|
|
329
|
+
else if( dirent_object.isDirectory() === true ) _return = 'directory';
|
|
330
|
+
else if( dirent_object.isSymbolicLink() === true ) _return = 'symbolic-link';
|
|
331
|
+
else if( dirent_object.isFIFO() === true ) _return = 'fifo';
|
|
332
|
+
else if( dirent_object.isCharacterDevice() === true ) _return = 'character-device';
|
|
333
|
+
else if( dirent_object.isBlockDevice() === true ) _return = 'block-device';
|
|
334
|
+
else if( dirent_object.isSocket() === true ) _return = 'socket';
|
|
335
|
+
else _return = 'unknown';
|
|
336
|
+
return _return;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
### getPathFromDirentObject
|
|
340
|
+
> Returns the full path of the entry described by the given [`Dirent`](https://nodejs.org/api/fs.html#class-dirent) object, made by joining the dirent's parent directory path with its name.
|
|
341
|
+
|
|
342
|
+
#### Parametres
|
|
343
|
+
| name | type | description |
|
|
344
|
+
| --- | --- | --- |
|
|
345
|
+
| dirent_object | object | The `Dirent` object to use; must have `parentPath` (or, on older versions of NodeJS, `path`) and `name` properties. |
|
|
346
|
+
|
|
347
|
+
#### Returns
|
|
348
|
+
| type | description |
|
|
349
|
+
| --- | --- |
|
|
350
|
+
| string | The joined path of the entry. |
|
|
351
|
+
|
|
352
|
+
#### Throws
|
|
353
|
+
| code | type | condition |
|
|
354
|
+
| --- | --- | --- |
|
|
355
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
356
|
+
|
|
357
|
+
#### History
|
|
358
|
+
| version | change |
|
|
359
|
+
| --- | --- |
|
|
360
|
+
| 0.1.1 | Falls back to the legacy `path` property; WIP |
|
|
361
|
+
*/
|
|
362
|
+
function getPathFromDirentObject( dirent_object ){
|
|
363
|
+
var _return = null;
|
|
364
|
+
var return_error = null;
|
|
365
|
+
// Parametre checks
|
|
366
|
+
if( typeof( dirent_object ) !== 'object' || dirent_object === null ){
|
|
367
|
+
return_error = new TypeError('Param "dirent_object" is not of type object.');
|
|
368
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
369
|
+
throw return_error;
|
|
370
|
+
}
|
|
371
|
+
// Function
|
|
372
|
+
try{
|
|
373
|
+
_return = PathNS.join( dirent_object.parentPath ?? dirent_object.path, dirent_object.name );
|
|
374
|
+
} catch(error) /* c8 ignore start */{
|
|
375
|
+
return_error = new Error(`PathNS.join threw an error: ${error}`, { cause: error });
|
|
376
|
+
throw return_error;
|
|
377
|
+
} /* c8 ignore stop */
|
|
378
|
+
return _return;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
### isExcludedFromPath
|
|
382
|
+
> Returns `true` if the given entry name or root-relative path matches any of the given `RegExp`s.
|
|
383
|
+
|
|
384
|
+
#### Parametres
|
|
385
|
+
| name | type | description |
|
|
386
|
+
| --- | --- | --- |
|
|
387
|
+
| name | string | The name of the entry, e.g. `'node_modules'`. |
|
|
388
|
+
| relative_path | string | The path of the entry relative to the root of the tree, e.g. `'packages/core/node_modules'`. |
|
|
389
|
+
| exclude_regexp_array | Array | An array of `RegExp` objects to test the name and path against. \[default: []\] |
|
|
390
|
+
|
|
391
|
+
#### Returns
|
|
392
|
+
| type | description |
|
|
393
|
+
| --- | --- |
|
|
394
|
+
| boolean | `true` if `name` or `relative_path` matches any of the `RegExp`s; `false` otherwise. |
|
|
395
|
+
|
|
396
|
+
#### History
|
|
397
|
+
| version | change |
|
|
398
|
+
| --- | --- |
|
|
399
|
+
| 0.1.1 | Introduced. |
|
|
400
|
+
*/
|
|
401
|
+
function isExcludedFromPath( name, relative_path, exclude_regexp_array = [] ){
|
|
402
|
+
for( const regexp of exclude_regexp_array ){
|
|
403
|
+
if( regexp.test( name ) === true ){
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
if( regexp.test( relative_path ) === true ){
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
### addSizes
|
|
414
|
+
> Adds two apparent-size values together, supporting any mix of number- and `BigInt`-valued sizes (as produced by `stat`ing with `bigint: true`).
|
|
415
|
+
|
|
416
|
+
#### Parametres
|
|
417
|
+
| name | type | description |
|
|
418
|
+
| --- | --- | --- |
|
|
419
|
+
| a | number\|BigInt | The first augend; `null` and `undefined` are treated as `0`. |
|
|
420
|
+
| b | number\|BigInt | The second augend; `null` and `undefined` are treated as `0`. |
|
|
421
|
+
|
|
422
|
+
#### Returns
|
|
423
|
+
| type | description |
|
|
424
|
+
| --- | --- |
|
|
425
|
+
| number\|BigInt | The sum; `BigInt` if either augend is a `BigInt`, number otherwise. |
|
|
426
|
+
|
|
427
|
+
#### History
|
|
428
|
+
| version | change |
|
|
429
|
+
| --- | --- |
|
|
430
|
+
| 0.1.1 | Introduced. |
|
|
431
|
+
*/
|
|
432
|
+
function addSizes( a, b ){
|
|
433
|
+
var left = a ?? 0;
|
|
434
|
+
var right = b ?? 0;
|
|
435
|
+
if( typeof( left ) === 'bigint' || typeof( right ) === 'bigint' ){
|
|
436
|
+
return BigInt( left ) + BigInt( right );
|
|
437
|
+
}
|
|
438
|
+
return left + right;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
### createChildThis
|
|
442
|
+
> Creates a new [TreeItem](#treeitem) with the given options and this item as its parent, and returns it. Meant to be used (via `TreeItem.prototype.createChildItem`) with `this` bound to a `TreeItem`.
|
|
443
|
+
|
|
444
|
+
#### Parametres
|
|
445
|
+
| name | type | description |
|
|
446
|
+
| --- | --- | --- |
|
|
447
|
+
| child_options | object | Run-time options for the new child, as accepted by [TreeItem](#treeitem). \[default: null\] |
|
|
448
|
+
|
|
449
|
+
#### Returns
|
|
450
|
+
| type | description |
|
|
451
|
+
| --- | --- |
|
|
452
|
+
| object | The new child `TreeItem`. |
|
|
453
|
+
|
|
454
|
+
#### History
|
|
455
|
+
| version | change |
|
|
456
|
+
| --- | --- |
|
|
457
|
+
| 0.1.1 | WIP |
|
|
458
|
+
*/
|
|
459
|
+
function createChildThis( child_options = null ){
|
|
460
|
+
var options = { ...( child_options ?? {} ), parentItem: this };
|
|
461
|
+
return new TreeItem( options );
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
### statThis
|
|
465
|
+
> [`stat`s](#statoptions) the path of the item bound to this function, storing the result in the item's `statObject` (from which `unixType` and `size` are derived) and resolving to the item. Meant to be used via `TreeItem.prototype.stat`.
|
|
466
|
+
|
|
467
|
+
#### Parametres
|
|
468
|
+
| name | type | description |
|
|
469
|
+
| --- | --- | --- |
|
|
470
|
+
| input_options | object | Options passed to [statOptions](#statoptions). \[default: null\] |
|
|
471
|
+
|
|
472
|
+
#### Returns
|
|
473
|
+
| type | description |
|
|
474
|
+
| --- | --- |
|
|
475
|
+
| Promise<object> | A promise which resolves to the item itself. |
|
|
476
|
+
|
|
477
|
+
#### Throws
|
|
478
|
+
| code | type | condition |
|
|
479
|
+
| --- | --- | --- |
|
|
480
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
481
|
+
|
|
482
|
+
#### History
|
|
483
|
+
| version | change |
|
|
484
|
+
| --- | --- |
|
|
485
|
+
| 0.1.1 | Resolves to the item; attaches `cause`; WIP |
|
|
486
|
+
*/
|
|
487
|
+
function statThis( input_options = null ){
|
|
488
|
+
const FUNCTION_NAME = 'statThis';
|
|
489
|
+
var _return = null;
|
|
490
|
+
var return_error = null;
|
|
491
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${this?.absolutePath}`});
|
|
492
|
+
_return = statOptions( { ...( input_options ?? {} ), path: this.absolutePath } ).then(
|
|
493
|
+
( stat_object ) => {
|
|
494
|
+
this.statObject = stat_object;
|
|
495
|
+
return this;
|
|
496
|
+
},
|
|
497
|
+
( error ) => {
|
|
498
|
+
return_error = new Error( `For ${this.name ?? this.absolutePath}: statOptions rejected with error: ${error}`, { cause: error } );
|
|
499
|
+
throw return_error;
|
|
500
|
+
}
|
|
501
|
+
);
|
|
502
|
+
// Return
|
|
503
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
504
|
+
return _return;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
### hashThis
|
|
508
|
+
> Computes the [SHA2-256](https://en.wikipedia.org/wiki/SHA-2) hash digest of the file at the path of the item bound to this function, formats it, stores it in a property named after the hash algorithm (e.g. `sha256`), and resolves to the formatted hash string. Meant to be used via `TreeItem.prototype.hash`.
|
|
509
|
+
|
|
510
|
+
#### Parametres
|
|
511
|
+
| name | type | description |
|
|
512
|
+
| --- | --- | --- |
|
|
513
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
514
|
+
|
|
515
|
+
##### `options` Properties
|
|
516
|
+
| name | type | default | description |
|
|
517
|
+
| --- | --- | --- | --- |
|
|
518
|
+
| hashOptions | object | { hash: 'sha256', prefix: false } | `hash` names the hash algorithm; only `'sha256'` is currently supported. `prefix` is passed through to the hashing function to control the [MultiHash](https://multiformats.io/multihash/) prefix of the raw digest. |
|
|
519
|
+
| formatOptions | object | { format: 'base16', prefix: false } | `format` is either `'base16'` or `'base64'`; `prefix` and the remaining properties are passed through to [getBase16FromBuffer](https://github.com/Anadian/cno-bedrock/blob/main/src/cno-bytes.js) or [getBase64FromBuffer](https://github.com/Anadian/cno-bedrock/blob/main/src/cno-base64.js) to control the formatted output. |
|
|
520
|
+
|
|
521
|
+
#### Returns
|
|
522
|
+
| type | description |
|
|
523
|
+
| --- | --- |
|
|
524
|
+
| Promise<string> | A promise which resolves to the formatted hash string. |
|
|
525
|
+
|
|
526
|
+
#### Throws
|
|
527
|
+
| code | type | condition |
|
|
528
|
+
| --- | --- | --- |
|
|
529
|
+
| 'ERR_INVALID_ARG_VALUE' | TypeError | Thrown if `options.hashOptions.hash` isn't `'sha256'` or `options.formatOptions.format` isn't `'base16'`/`'base64'`. |
|
|
530
|
+
|
|
531
|
+
#### History
|
|
532
|
+
| version | change |
|
|
533
|
+
| --- | --- |
|
|
534
|
+
| 0.1.1 | Fixed option derivation, promise chaining, and added parametre checks; WIP |
|
|
535
|
+
*/
|
|
536
|
+
function hashThis( input_options = null ){
|
|
537
|
+
const FUNCTION_NAME = 'hashThis';
|
|
538
|
+
const DEFAULT_OPTIONS = {
|
|
539
|
+
hashOptions: {
|
|
540
|
+
hash: 'sha256', // The hash algorithm; only 'sha256' is currently supported.
|
|
541
|
+
prefix: false // Whether to prefix the raw digest buffer with the MultiHash prefix code.
|
|
542
|
+
},
|
|
543
|
+
formatOptions: {
|
|
544
|
+
format: 'base16', // 'base16' or 'base64'.
|
|
545
|
+
prefix: false // Whether to prefix the formatted string.
|
|
546
|
+
}
|
|
547
|
+
};// Variables
|
|
548
|
+
var _return = null;
|
|
549
|
+
var return_error = null;
|
|
550
|
+
var options = {};
|
|
551
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${this?.absolutePath}`});
|
|
552
|
+
// Options
|
|
553
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
554
|
+
options.hashOptions = options.hashOptions ?? DEFAULT_OPTIONS.hashOptions;
|
|
555
|
+
options.formatOptions = options.formatOptions ?? DEFAULT_OPTIONS.formatOptions;
|
|
556
|
+
if( options.validationFunction( options ) === true ){
|
|
557
|
+
if( options.noop !== true ){
|
|
558
|
+
// Parametre checks
|
|
559
|
+
if( options.hashOptions.hash !== 'sha256' ){
|
|
560
|
+
return_error = new TypeError(`Unsupported hash algorithm: '${options.hashOptions.hash}'; only 'sha256' is currently supported.`);
|
|
561
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
562
|
+
throw return_error;
|
|
563
|
+
}
|
|
564
|
+
if( options.formatOptions.format !== 'base16' && options.formatOptions.format !== 'base64' ){
|
|
565
|
+
return_error = new TypeError(`Unsupported hash format: '${options.formatOptions.format}'; must be 'base16' or 'base64'.`);
|
|
566
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
567
|
+
throw return_error;
|
|
568
|
+
}
|
|
569
|
+
// Function
|
|
570
|
+
_return = getSHA256FromFilePath( this.absolutePath, { ...options.hashOptions, logFunction: options.logFunction } ).then(
|
|
571
|
+
( hash_buffer ) => {
|
|
572
|
+
if( options.formatOptions.format === 'base64' ){
|
|
573
|
+
return getBase64FromBuffer( hash_buffer, options.formatOptions );
|
|
574
|
+
}
|
|
575
|
+
return getBase16FromBuffer( hash_buffer, options.formatOptions );
|
|
576
|
+
},
|
|
577
|
+
( error ) => {
|
|
578
|
+
return_error = new Error( `For ${this.name ?? this.absolutePath}: getSHA256FromFilePath rejected with error: ${error}`, { cause: error } );
|
|
579
|
+
throw return_error;
|
|
580
|
+
}
|
|
581
|
+
);
|
|
582
|
+
_return = _return.then(
|
|
583
|
+
( hash_string ) => {
|
|
584
|
+
this[options.hashOptions.hash] = hash_string;
|
|
585
|
+
return hash_string;
|
|
586
|
+
},
|
|
587
|
+
null
|
|
588
|
+
);
|
|
589
|
+
} else{ // noop
|
|
590
|
+
_return = Promise.resolve( null );
|
|
591
|
+
} // noop
|
|
592
|
+
} else{ // validationFunction
|
|
593
|
+
_return = Promise.resolve( null );
|
|
594
|
+
} // validationFunction
|
|
595
|
+
// Return
|
|
596
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
597
|
+
return _return;
|
|
598
|
+
} // hashThis
|
|
599
|
+
/**
|
|
600
|
+
### processAsyncThis
|
|
601
|
+
> Asynchronously processes the item bound to this function and, when `recursive` is `true`, its descendants: each entry is stat'd, regular files are optionally hashed, and directories are read to create and process child items. Meant to be used via `TreeItem.prototype.processAsync`.
|
|
602
|
+
|
|
603
|
+
#### Parametres
|
|
604
|
+
| name | type | description |
|
|
605
|
+
| --- | --- | --- |
|
|
606
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
607
|
+
|
|
608
|
+
##### `options` Properties
|
|
609
|
+
| name | type | default | description |
|
|
610
|
+
| --- | --- | --- | --- |
|
|
611
|
+
| recursive | boolean | false | Whether to descend into child directories. |
|
|
612
|
+
| stat | boolean | true | Whether to stat each item; must be `true` for recursion to work because an un-stat'd item's `unixType` is unknown. |
|
|
613
|
+
| statOptions | object | null | Options passed to [TreeItem.prototype.stat](#treeitemprototypestat) for each item. |
|
|
614
|
+
| hash | boolean | false | Whether to hash regular files via [TreeItem.prototype.hash](#treeitemprototypehash). |
|
|
615
|
+
| hashOptions | object | null | Options passed to [TreeItem.prototype.hash](#treeitemprototypehash). |
|
|
616
|
+
| formatOptions | object | null | Options passed to [TreeItem.prototype.hash](#treeitemprototypehash). |
|
|
617
|
+
| excludeRegExpArray | Array | [] | An array of `RegExp` objects; entries whose name or root-relative path match any of them are omitted and not descended into. |
|
|
618
|
+
| readdirOptions | object | null | Options passed to [getDirentArrayFromDirectoryPath](#getdirentarrayfromdirectorypath) for each directory. |
|
|
619
|
+
| maxDepth | number | Infinity | The maximum depth to descend into child directories; must be `>= 0`. |
|
|
620
|
+
| depth | number | 0 | The depth of the item itself within the tree; incremented automatically for each generation of children. |
|
|
621
|
+
| rootPath | string | The item's own `absolutePath` (dynamic) | The absolute path of the root of the tree, against which `relative_path` values used for exclusion testing are calculated. |
|
|
622
|
+
|
|
623
|
+
#### Returns
|
|
624
|
+
| type | description |
|
|
625
|
+
| --- | --- |
|
|
626
|
+
| Promise<object> | A promise which resolves to the item itself once it and its processed descendants have been handled. |
|
|
627
|
+
|
|
628
|
+
#### Throws
|
|
629
|
+
| code | type | condition |
|
|
630
|
+
| --- | --- | --- |
|
|
631
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
632
|
+
| 'ERR_INVALID_ARG_VALUE' | TypeError | Thrown if a given argument has the right type but an unusable value (e.g. a non-`RegExp` in `excludeRegExpArray`). |
|
|
633
|
+
|
|
634
|
+
#### History
|
|
635
|
+
| version | change |
|
|
636
|
+
| --- | --- |
|
|
637
|
+
| 0.1.1 | Fixed option derivation and deferred `unixType` checks until after statting; added exclusion, depth control, and resolving to the item. |
|
|
638
|
+
*/
|
|
639
|
+
function processAsyncThis( input_options = null ){
|
|
640
|
+
const FUNCTION_NAME = 'processAsyncThis';
|
|
641
|
+
const DEFAULT_OPTIONS = {
|
|
642
|
+
recursive: false, // Whether to descend into child directories.
|
|
643
|
+
stat: true, // Whether to stat each item.
|
|
644
|
+
statOptions: null, // Options passed to `stat` for each item.
|
|
645
|
+
hash: false, // Whether to hash regular files.
|
|
646
|
+
hashOptions: null, // Options passed to `hash`.
|
|
647
|
+
formatOptions: null, // Options passed to `hash`.
|
|
648
|
+
excludeRegExpArray: [], // An array of `RegExp` objects used to omit entries by name or root-relative path.
|
|
649
|
+
readdirOptions: null, // Options passed to `getDirentArrayFromDirectoryPath`.
|
|
650
|
+
maxDepth: Number.POSITIVE_INFINITY, // The maximum depth to descend into child directories.
|
|
651
|
+
depth: 0, // The depth of the item itself within the tree.
|
|
652
|
+
rootPath: null // The absolute path of the root of the tree; dynamically defaults to the item's own absolute path.
|
|
653
|
+
};// Variables
|
|
654
|
+
var _return = null;
|
|
655
|
+
var return_error = null;
|
|
656
|
+
var options = {};
|
|
657
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${this?.absolutePath}`});
|
|
658
|
+
// Options
|
|
659
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS, ( pre_dynamic ) => {
|
|
660
|
+
var post_dynamic = {};
|
|
661
|
+
post_dynamic.rootPath = isDefined( pre_dynamic.rootPath ) === true ? pre_dynamic.rootPath : this.absolutePath;
|
|
662
|
+
return post_dynamic;
|
|
663
|
+
} );
|
|
664
|
+
// Parametre checks
|
|
665
|
+
if( Array.isArray( options.excludeRegExpArray ) !== true ){
|
|
666
|
+
return_error = new TypeError('Option "excludeRegExpArray" is not of type Array.');
|
|
667
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
668
|
+
throw return_error;
|
|
669
|
+
}
|
|
670
|
+
for( const regexp of options.excludeRegExpArray ){
|
|
671
|
+
if( !( regexp instanceof RegExp ) ){
|
|
672
|
+
return_error = new TypeError(`Option "excludeRegExpArray" contains a non-RegExp value: ${regexp}`);
|
|
673
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
674
|
+
throw return_error;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if( typeof( options.maxDepth ) !== 'number' || Number.isNaN( options.maxDepth ) || options.maxDepth < 0 ){
|
|
678
|
+
return_error = new TypeError('Option "maxDepth" is not a number greater than or equal to zero.');
|
|
679
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
680
|
+
throw return_error;
|
|
681
|
+
}
|
|
682
|
+
if( options.noop === true ){
|
|
683
|
+
return Promise.resolve( this );
|
|
684
|
+
}
|
|
685
|
+
// Function
|
|
686
|
+
_return = Promise.resolve( this );
|
|
687
|
+
if( options.stat === true ){
|
|
688
|
+
_return = _return.then( ( item ) => item.stat( options.statOptions ), null );
|
|
689
|
+
}
|
|
690
|
+
_return = _return.then(
|
|
691
|
+
( item ) => {
|
|
692
|
+
if( item.unixType === 'file' && options.hash === true ){
|
|
693
|
+
return item.hash( { hashOptions: options.hashOptions, formatOptions: options.formatOptions } ).then( () => item );
|
|
694
|
+
} else if( item.unixType === 'directory' && options.recursive === true && options.depth < options.maxDepth ){
|
|
695
|
+
return getDirentArrayFromDirectoryPath( item.absolutePath, options.readdirOptions ).then(
|
|
696
|
+
( dirent_array ) => {
|
|
697
|
+
var child_promises = [];
|
|
698
|
+
for( const dirent of dirent_array ){
|
|
699
|
+
var child_path = PathNS.join( item.absolutePath, dirent.name );
|
|
700
|
+
var relative_path = PathNS.relative( options.rootPath, child_path );
|
|
701
|
+
if( isExcludedFromPath( dirent.name, relative_path, options.excludeRegExpArray ) === true ){
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
var child_index = item.addChild( { direntObject: dirent } );
|
|
705
|
+
child_promises.push( item.children[child_index].processAsync( { ...options, depth: options.depth + 1 } ) );
|
|
706
|
+
}
|
|
707
|
+
return Promise.all( child_promises ).then( () => item );
|
|
708
|
+
},
|
|
709
|
+
null
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
return Promise.resolve( item );
|
|
713
|
+
},
|
|
714
|
+
null
|
|
715
|
+
);
|
|
716
|
+
// Return
|
|
717
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
718
|
+
return _return;
|
|
719
|
+
} // processAsyncThis
|
|
720
|
+
/**
|
|
721
|
+
### aggregateThis
|
|
722
|
+
> Computes dirstat-like aggregate statistics for the tree rooted at the item bound to this function, in two passes: a bottom-up pass assigning `totalSize` (the item's own apparent size plus the total of everything beneath it), `fileCount`, `directoryCount`, and `otherCount` (all inclusive of the item itself) to every node; then a top-down pass assigning `percentageOfParent` (the percentage of the parent's `totalSize` that the node's `totalSize` represents; the root gets `100`) and, optionally, sorting every directory's `children` largest-`totalSize`-first. Meant to be used via `TreeItem.prototype.aggregate`.
|
|
723
|
+
|
|
724
|
+
#### Parametres
|
|
725
|
+
| name | type | description |
|
|
726
|
+
| --- | --- | --- |
|
|
727
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
728
|
+
|
|
729
|
+
##### `options` Properties
|
|
730
|
+
| name | type | default | description |
|
|
731
|
+
| --- | --- | --- | --- |
|
|
732
|
+
| sortBySize | boolean | false | Whether to sort the `children` of every directory by `totalSize`, largest first; the sort is stable so equal-sized siblings keep their original order. |
|
|
733
|
+
|
|
734
|
+
#### Returns
|
|
735
|
+
| type | description |
|
|
736
|
+
| --- | --- |
|
|
737
|
+
| object | The item itself, for chaining. |
|
|
738
|
+
|
|
739
|
+
#### History
|
|
740
|
+
| version | change |
|
|
741
|
+
| --- | --- |
|
|
742
|
+
| 0.1.1 | Introduced. |
|
|
743
|
+
*/
|
|
744
|
+
function aggregateThis( input_options = null ){
|
|
745
|
+
const FUNCTION_NAME = 'aggregateThis';
|
|
746
|
+
const DEFAULT_OPTIONS = {
|
|
747
|
+
sortBySize: false // Whether to sort every directory's children by `totalSize`, largest first.
|
|
748
|
+
};// Variables
|
|
749
|
+
var options = {};
|
|
750
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${this?.absolutePath}`});
|
|
751
|
+
// Options
|
|
752
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
753
|
+
// Function
|
|
754
|
+
const accumulate_subtree = ( node ) => {
|
|
755
|
+
var total_size = node.size ?? 0;
|
|
756
|
+
var file_count = 0;
|
|
757
|
+
var directory_count = 0;
|
|
758
|
+
var other_count = 0;
|
|
759
|
+
if( node.unixType === 'file' ) file_count = 1;
|
|
760
|
+
else if( node.unixType === 'directory' ) directory_count = 1;
|
|
761
|
+
else if( isDefined( node.unixType ) === true ) other_count = 1;
|
|
762
|
+
for( const child of node.children ){
|
|
763
|
+
var child_totals = accumulate_subtree( child );
|
|
764
|
+
total_size = addSizes( total_size, child_totals.totalSize );
|
|
765
|
+
file_count += child_totals.fileCount;
|
|
766
|
+
directory_count += child_totals.directoryCount;
|
|
767
|
+
other_count += child_totals.otherCount;
|
|
768
|
+
}
|
|
769
|
+
node.totalSize = total_size;
|
|
770
|
+
node.fileCount = file_count;
|
|
771
|
+
node.directoryCount = directory_count;
|
|
772
|
+
node.otherCount = other_count;
|
|
773
|
+
return { totalSize: total_size, fileCount: file_count, directoryCount: directory_count, otherCount: other_count };
|
|
774
|
+
};
|
|
775
|
+
const distribute = ( node, parent_total_size ) => {
|
|
776
|
+
if( options.sortBySize === true ){
|
|
777
|
+
node.children.sort( ( a, b ) => Number( b.totalSize ) - Number( a.totalSize ) );
|
|
778
|
+
}
|
|
779
|
+
if( parent_total_size == null || Number( parent_total_size ) === 0 ){
|
|
780
|
+
node.percentageOfParent = 100;
|
|
781
|
+
} else{
|
|
782
|
+
node.percentageOfParent = ( Number( node.totalSize ) / Number( parent_total_size ) ) * 100;
|
|
783
|
+
}
|
|
784
|
+
for( const child of node.children ){
|
|
785
|
+
distribute( child, node.totalSize );
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
accumulate_subtree( this );
|
|
789
|
+
distribute( this, null );
|
|
790
|
+
// Return
|
|
791
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${this?.absolutePath}`});
|
|
792
|
+
return this;
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
### serialiseThis
|
|
796
|
+
> Returns a plain-object representation of the tree rooted at the item bound to this function, suitable for `JSON.stringify`ing: `size` and `totalSize` are converted to strings if they are `BigInt`s and the `url` is converted to its href string. Meant to be used via `TreeItem.prototype.serialise`.
|
|
797
|
+
|
|
798
|
+
#### Parametres
|
|
799
|
+
| name | type | description |
|
|
800
|
+
| --- | --- | --- |
|
|
801
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
802
|
+
|
|
803
|
+
##### `options` Properties
|
|
804
|
+
| name | type | default | description |
|
|
805
|
+
| --- | --- | --- | --- |
|
|
806
|
+
| recursive | boolean | true | Whether to include the serialised `children` of the item. |
|
|
807
|
+
|
|
808
|
+
#### Returns
|
|
809
|
+
| type | description |
|
|
810
|
+
| --- | --- |
|
|
811
|
+
| object | A plain object with `name`, `relativePath`, `absolutePath`, `url`, `unixType`, `size`, `children`, and, when present, `sha256`, `totalSize`, `fileCount`, `directoryCount`, `otherCount`, and `percentageOfParent` properties. |
|
|
812
|
+
|
|
813
|
+
#### History
|
|
814
|
+
| version | change |
|
|
815
|
+
| --- | --- |
|
|
816
|
+
| 0.1.1 | Rewrote; now returns a JSON-safe object. |
|
|
817
|
+
*/
|
|
818
|
+
function serialiseThis( input_options = null ){
|
|
819
|
+
const FUNCTION_NAME = 'serialiseThis';
|
|
820
|
+
const DEFAULT_OPTIONS = {
|
|
821
|
+
recursive: true // Whether to include the serialised `children` of the item.
|
|
822
|
+
};// Variables
|
|
823
|
+
var options = {};
|
|
824
|
+
var serialise_value = ( value ) => ( typeof( value ) === 'bigint' ? value.toString() : value );
|
|
825
|
+
// Options
|
|
826
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
827
|
+
// Function
|
|
828
|
+
var output_object = {
|
|
829
|
+
name: this.name,
|
|
830
|
+
relativePath: this.relativePath,
|
|
831
|
+
absolutePath: this.absolutePath,
|
|
832
|
+
url: String( this.url ),
|
|
833
|
+
unixType: this.unixType ?? null,
|
|
834
|
+
size: serialise_value( this.size )
|
|
835
|
+
};
|
|
836
|
+
if( isDefined( this.sha256 ) === true ){
|
|
837
|
+
output_object.sha256 = this.sha256;
|
|
838
|
+
}
|
|
839
|
+
if( isDefined( this.totalSize ) === true ){
|
|
840
|
+
output_object.totalSize = serialise_value( this.totalSize );
|
|
841
|
+
output_object.fileCount = this.fileCount;
|
|
842
|
+
output_object.directoryCount = this.directoryCount;
|
|
843
|
+
output_object.otherCount = this.otherCount;
|
|
844
|
+
output_object.percentageOfParent = this.percentageOfParent;
|
|
845
|
+
}
|
|
846
|
+
if( options.recursive === true ){
|
|
847
|
+
output_object.children = this.children.map( ( child ) => child.serialise( { recursive: true } ) );
|
|
848
|
+
} else{
|
|
849
|
+
output_object.children = [];
|
|
850
|
+
}
|
|
851
|
+
// Return
|
|
852
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned object with ${output_object.children.length} children.`});
|
|
853
|
+
return output_object;
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
### getNameFromPath
|
|
857
|
+
> Returns the [basename](https://nodejs.org/api/path.html#pathbasenamepath) of the given path.
|
|
858
|
+
|
|
859
|
+
#### Parametres
|
|
860
|
+
| name | type | description |
|
|
861
|
+
| --- | --- | --- |
|
|
862
|
+
| input_path | string | The path to derive the name of. \[default: null\] |
|
|
863
|
+
|
|
864
|
+
#### Returns
|
|
865
|
+
| type | description |
|
|
866
|
+
| --- | --- |
|
|
867
|
+
| string | The last segment of the path; `'.'` if `input_path` is `null` or `undefined`. |
|
|
868
|
+
|
|
869
|
+
#### Throws
|
|
870
|
+
| code | type | condition |
|
|
871
|
+
| --- | --- | --- |
|
|
872
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
873
|
+
|
|
874
|
+
#### History
|
|
875
|
+
| version | change |
|
|
876
|
+
| --- | --- |
|
|
877
|
+
| 0.1.1 | WIP |
|
|
878
|
+
*/
|
|
879
|
+
function getNameFromPath( input_path = null ){
|
|
880
|
+
var _return = null;
|
|
881
|
+
var return_error = null;
|
|
882
|
+
var path_string = input_path ?? '.';
|
|
883
|
+
var normalised_path = '';
|
|
884
|
+
try{
|
|
885
|
+
normalised_path = PathNS.normalize( path_string );
|
|
886
|
+
} catch(error) /* c8 ignore start */{
|
|
887
|
+
return_error = new Error(`PathNS.normalize( '${path_string}' ) threw an error: ${error}`, { cause: error });
|
|
888
|
+
throw return_error;
|
|
889
|
+
} /* c8 ignore stop */
|
|
890
|
+
try{
|
|
891
|
+
_return = PathNS.basename( normalised_path );
|
|
892
|
+
} catch(error) /* c8 ignore start */{
|
|
893
|
+
return_error = new Error(`PathNS.basename( '${normalised_path}' ) threw an error: ${error}`, { cause: error });
|
|
894
|
+
throw return_error;
|
|
895
|
+
} /* c8 ignore stop */
|
|
896
|
+
return _return;
|
|
897
|
+
} // getNameFromPath
|
|
898
|
+
/**
|
|
899
|
+
### getDirectoryFromPath
|
|
900
|
+
> Returns the [dirname](https://nodejs.org/api/path.html#pathdirnamepath) of the given path.
|
|
901
|
+
|
|
902
|
+
#### Parametres
|
|
903
|
+
| name | type | description |
|
|
904
|
+
| --- | --- | --- |
|
|
905
|
+
| input_path | string | The path to derive the directory of. \[default: null\] |
|
|
906
|
+
|
|
907
|
+
#### Returns
|
|
908
|
+
| type | description |
|
|
909
|
+
| --- | --- |
|
|
910
|
+
| string | The directory portion of the path; `'..'` if `input_path` is `null` or `undefined`. |
|
|
911
|
+
|
|
912
|
+
#### Throws
|
|
913
|
+
| code | type | condition |
|
|
914
|
+
| --- | --- | --- |
|
|
915
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
916
|
+
|
|
917
|
+
#### History
|
|
918
|
+
| version | change |
|
|
919
|
+
| --- | --- |
|
|
920
|
+
| 0.1.1 | WIP |
|
|
921
|
+
*/
|
|
922
|
+
function getDirectoryFromPath( input_path = null ){
|
|
923
|
+
var _return = null;
|
|
924
|
+
var return_error = null;
|
|
925
|
+
var path_string = input_path ?? '..';
|
|
926
|
+
var normalised_path = '';
|
|
927
|
+
try{
|
|
928
|
+
normalised_path = PathNS.normalize( path_string );
|
|
929
|
+
} catch(error) /* c8 ignore start */{
|
|
930
|
+
return_error = new Error(`PathNS.normalize( '${path_string}' ) threw an error: ${error}`, { cause: error });
|
|
931
|
+
throw return_error;
|
|
932
|
+
} /* c8 ignore stop */
|
|
933
|
+
try{
|
|
934
|
+
_return = PathNS.dirname( normalised_path );
|
|
935
|
+
} catch(error) /* c8 ignore start */{
|
|
936
|
+
return_error = new Error(`PathNS.dirname( '${normalised_path}' ) threw an error: ${error}`, { cause: error });
|
|
937
|
+
throw return_error;
|
|
938
|
+
} /* c8 ignore stop */
|
|
939
|
+
return _return;
|
|
940
|
+
} // getDirectoryFromPath
|
|
941
|
+
/**
|
|
942
|
+
### TreeItem
|
|
943
|
+
> Smart constructor for a node of a filesystem tree: pairs a name and path with lazily-derived [`file://` URL](https://nodejs.org/api/url.html#urlpathtofileurlpath), `unixType`, [`stat`](https://nodejs.org/api/fs.html#class-stats) object, and apparent `size` properties, plus an array of child `TreeItem`s. Can also be called without `new`.
|
|
944
|
+
|
|
945
|
+
#### Parametres
|
|
946
|
+
| name | type | description |
|
|
947
|
+
| --- | --- | --- |
|
|
948
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
949
|
+
|
|
950
|
+
##### `options` Properties
|
|
951
|
+
| name | type | default | description |
|
|
952
|
+
| --- | --- | --- | --- |
|
|
953
|
+
| name | string | Derived from `direntObject` or `path` (dynamic) | The name of the entry, e.g. `'node_modules'`. |
|
|
954
|
+
| path | string | null | The path of the entry; used to derive `name` and `relativePath` when they aren't specified. |
|
|
955
|
+
| relativePath | string | dynamic | The path of the entry, either absolute or relative to the process's working directory; defaults to `parentItem`'s `relativePath` joined with `name`, then to `''` (which resolves to the working directory). |
|
|
956
|
+
| absolutePath | string | [PathNS.resolve](https://nodejs.org/api/path.html#pathresolvepaths)(`relativePath`) (dynamic) | The absolute path of the entry. |
|
|
957
|
+
| url | [URL](https://nodejs.org/api/url.html#class-url) | [URLNS.pathToFileURL](https://nodejs.org/api/url.html#urlpathtofileurlpath)(`absolutePath`) (dynamic) | The `file://` URL of the entry. |
|
|
958
|
+
| parentItem | object | null | The `TreeItem` this entry is a child of. |
|
|
959
|
+
| direntObject | object | null | A [`Dirent`](https://nodejs.org/api/fs.html#class-dirent) object; sets `name`, `relativePath`, and `unixType` from it. |
|
|
960
|
+
| statObject | object | null | A [`stat`](https://nodejs.org/api/fs.html#class-stats) object; sets `unixType` from its `mode`. |
|
|
961
|
+
| unixType | string | null | The kind of filesystem entry, e.g. `'file'`; see [getUnixTypeFromStatObject](#getunixtypefromstatobject). |
|
|
962
|
+
| size | number\|BigInt | `statObject.size` (dynamic) | The apparent size of the entry in bytes. |
|
|
963
|
+
| children | Array | [] | The child `TreeItem`s of this entry. |
|
|
964
|
+
|
|
965
|
+
#### Properties
|
|
966
|
+
> The following instance methods are also defined on every `TreeItem`:
|
|
967
|
+
- [`createChildItem`](#treeitemprototypecreatechilditem)
|
|
968
|
+
- [`addChild`](#treeitemprototypeaddchild)
|
|
969
|
+
- [`stat`](#treeitemprototypestat)
|
|
970
|
+
- [`hash`](#treeitemprototypehash)
|
|
971
|
+
- [`processAsync`](#treeitemprototypeprocessasync)
|
|
972
|
+
- [`aggregate`](#treeitemprototypeaggregate)
|
|
973
|
+
- [`serialise`](#treeitemprototypeserialise)
|
|
974
|
+
|
|
975
|
+
#### Throws
|
|
976
|
+
| code | type | condition |
|
|
977
|
+
| --- | --- | --- |
|
|
978
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
979
|
+
|
|
980
|
+
#### History
|
|
981
|
+
| version | change |
|
|
982
|
+
| --- | --- |
|
|
983
|
+
| 0.1.1 | Rewrote: fixed the duplicated/broken property definitions, added `size` and the `aggregate`/`serialise` methods. |
|
|
984
|
+
*/
|
|
985
|
+
function TreeItem( input_options ){
|
|
986
|
+
if( !new.target ){
|
|
987
|
+
return new TreeItem( input_options );
|
|
988
|
+
}
|
|
989
|
+
input_options = input_options ?? {};
|
|
990
|
+
Object.defineProperties( this, {
|
|
991
|
+
_cache: {
|
|
992
|
+
value: new TimedCacheMap(),
|
|
993
|
+
writable: true
|
|
994
|
+
},
|
|
995
|
+
name: {
|
|
996
|
+
value: input_options.name ?? null,
|
|
997
|
+
writable: true,
|
|
998
|
+
enumerable: true
|
|
999
|
+
},
|
|
1000
|
+
parentItem: {
|
|
1001
|
+
value: input_options.parentItem ?? null,
|
|
1002
|
+
writable: true
|
|
1003
|
+
},
|
|
1004
|
+
relativePath: {
|
|
1005
|
+
enumerable: true,
|
|
1006
|
+
get(){
|
|
1007
|
+
return this._cache.get( 'relativePath' );
|
|
1008
|
+
},
|
|
1009
|
+
set( value ){
|
|
1010
|
+
return this._cache.set( 'relativePath', value );
|
|
1011
|
+
}
|
|
1012
|
+
},
|
|
1013
|
+
absolutePath: {
|
|
1014
|
+
enumerable: true,
|
|
1015
|
+
get(){
|
|
1016
|
+
var _return = this._cache.get( 'absolutePath' );
|
|
1017
|
+
if( isDefined( _return ) === false ){
|
|
1018
|
+
_return = PathNS.resolve( this.relativePath ?? '' );
|
|
1019
|
+
}
|
|
1020
|
+
return _return;
|
|
1021
|
+
},
|
|
1022
|
+
set( value ){
|
|
1023
|
+
var _return = this._cache.set( 'absolutePath', value );
|
|
1024
|
+
this._cache.set( 'url', undefined ); // Invalidate the cached `url`.
|
|
1025
|
+
return _return;
|
|
1026
|
+
}
|
|
1027
|
+
},
|
|
1028
|
+
url: {
|
|
1029
|
+
enumerable: true,
|
|
1030
|
+
get(){
|
|
1031
|
+
var _return = this._cache.get( 'url' );
|
|
1032
|
+
if( isDefined( _return ) === false ){
|
|
1033
|
+
_return = URLNS.pathToFileURL( this.absolutePath );
|
|
1034
|
+
}
|
|
1035
|
+
return _return;
|
|
1036
|
+
},
|
|
1037
|
+
set( value ){
|
|
1038
|
+
return this._cache.set( 'url', value );
|
|
1039
|
+
}
|
|
1040
|
+
},
|
|
1041
|
+
unixType: {
|
|
1042
|
+
enumerable: true,
|
|
1043
|
+
get(){
|
|
1044
|
+
return this._cache.get( 'unixType' );
|
|
1045
|
+
},
|
|
1046
|
+
set( value ){
|
|
1047
|
+
return this._cache.set( 'unixType', value );
|
|
1048
|
+
}
|
|
1049
|
+
},
|
|
1050
|
+
statObject: {
|
|
1051
|
+
enumerable: true,
|
|
1052
|
+
get(){
|
|
1053
|
+
return this._cache.get( 'statObject' );
|
|
1054
|
+
},
|
|
1055
|
+
set( value ){
|
|
1056
|
+
var _return = this._cache.set( 'statObject', value );
|
|
1057
|
+
if( isDefined( value ) === true ){
|
|
1058
|
+
this.unixType = getUnixTypeFromStatObject( value );
|
|
1059
|
+
}
|
|
1060
|
+
return _return;
|
|
1061
|
+
}
|
|
1062
|
+
},
|
|
1063
|
+
direntObject: {
|
|
1064
|
+
enumerable: true,
|
|
1065
|
+
get(){
|
|
1066
|
+
return this._cache.get( 'direntObject' );
|
|
1067
|
+
},
|
|
1068
|
+
set( value ){
|
|
1069
|
+
this._cache.set( 'direntObject', value );
|
|
1070
|
+
this.name = value.name;
|
|
1071
|
+
this.relativePath = getPathFromDirentObject( value );
|
|
1072
|
+
this.unixType = getUnixTypeFromDirentObject( value );
|
|
1073
|
+
}
|
|
1074
|
+
},
|
|
1075
|
+
size: {
|
|
1076
|
+
enumerable: true,
|
|
1077
|
+
get(){
|
|
1078
|
+
var _return = this._cache.get( 'size' );
|
|
1079
|
+
if( isDefined( _return ) === false ){
|
|
1080
|
+
_return = this.statObject?.size ?? null;
|
|
1081
|
+
}
|
|
1082
|
+
return _return;
|
|
1083
|
+
},
|
|
1084
|
+
set( value ){
|
|
1085
|
+
return this._cache.set( 'size', value );
|
|
1086
|
+
}
|
|
1087
|
+
},
|
|
1088
|
+
children: {
|
|
1089
|
+
value: input_options.children ?? [],
|
|
1090
|
+
writable: true,
|
|
1091
|
+
configurable: true,
|
|
1092
|
+
enumerable: true
|
|
1093
|
+
},
|
|
1094
|
+
createChildItem: {
|
|
1095
|
+
value: createChildThis.bind( this )
|
|
1096
|
+
},
|
|
1097
|
+
addChild: {
|
|
1098
|
+
value: ( child_options = null ) => {
|
|
1099
|
+
var new_child = this.createChildItem( child_options );
|
|
1100
|
+
this.children.push( new_child );
|
|
1101
|
+
return this.children.length - 1;
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
stat: {
|
|
1105
|
+
value: statThis.bind( this )
|
|
1106
|
+
},
|
|
1107
|
+
hash: {
|
|
1108
|
+
value: hashThis.bind( this )
|
|
1109
|
+
},
|
|
1110
|
+
processAsync: {
|
|
1111
|
+
value: processAsyncThis.bind( this )
|
|
1112
|
+
},
|
|
1113
|
+
aggregate: {
|
|
1114
|
+
value: aggregateThis.bind( this )
|
|
1115
|
+
},
|
|
1116
|
+
serialise: {
|
|
1117
|
+
value: serialiseThis.bind( this )
|
|
1118
|
+
}
|
|
1119
|
+
} );
|
|
1120
|
+
// Parametre checks
|
|
1121
|
+
if( isDefined( input_options.direntObject ) === true && typeof( input_options.direntObject ) !== 'object' ){
|
|
1122
|
+
var return_error = new TypeError('Option "direntObject" is not of type object.');
|
|
1123
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1124
|
+
throw return_error;
|
|
1125
|
+
}
|
|
1126
|
+
// Function
|
|
1127
|
+
if( isDefined( input_options.direntObject ) === true ){
|
|
1128
|
+
this.direntObject = input_options.direntObject; // Sets `name`, `relativePath` and `unixType` from the dirent.
|
|
1129
|
+
}
|
|
1130
|
+
if( this.name === null ){
|
|
1131
|
+
this.name = isDefined( input_options.path ) === true ? getNameFromPath( input_options.path ) : null;
|
|
1132
|
+
}
|
|
1133
|
+
if( isDefined( input_options.relativePath ) === true && input_options.relativePath !== '' ){
|
|
1134
|
+
this.relativePath = input_options.relativePath;
|
|
1135
|
+
} else if( isDefined( input_options.path ) === true && input_options.path !== '' ){
|
|
1136
|
+
this.relativePath = input_options.path;
|
|
1137
|
+
} else if( isDefined( this.parentItem ) === true ){
|
|
1138
|
+
this.relativePath = PathNS.join( this.parentItem.relativePath ?? '', this.name ?? '' );
|
|
1139
|
+
}
|
|
1140
|
+
if( isDefined( this.relativePath ) === false ){
|
|
1141
|
+
this.relativePath = '';
|
|
1142
|
+
}
|
|
1143
|
+
if( isDefined( input_options.absolutePath ) === true ) this.absolutePath = input_options.absolutePath;
|
|
1144
|
+
if( isDefined( input_options.url ) === true ) this.url = input_options.url;
|
|
1145
|
+
if( isDefined( input_options.statObject ) === true ) this.statObject = input_options.statObject;
|
|
1146
|
+
if( isDefined( input_options.unixType ) === true ) this.unixType = input_options.unixType;
|
|
1147
|
+
if( isDefined( input_options.size ) === true ) this.size = input_options.size;
|
|
1148
|
+
// Return
|
|
1149
|
+
return this;
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
### TreeItem.prototype.createChildItem
|
|
1153
|
+
> Creates a new [TreeItem](#treeitem) with the given options and this item as its parent, and returns it.
|
|
1154
|
+
|
|
1155
|
+
#### Parametres
|
|
1156
|
+
| name | type | description |
|
|
1157
|
+
| --- | --- | --- |
|
|
1158
|
+
| child_options | object | Run-time options for the new child, as accepted by [TreeItem](#treeitem). \[default: null\] |
|
|
1159
|
+
|
|
1160
|
+
#### Returns
|
|
1161
|
+
| type | description |
|
|
1162
|
+
| --- | --- |
|
|
1163
|
+
| object | The new child `TreeItem`. |
|
|
1164
|
+
|
|
1165
|
+
#### History
|
|
1166
|
+
| version | change |
|
|
1167
|
+
| --- | --- |
|
|
1168
|
+
| 0.1.1 | WIP |
|
|
1169
|
+
*/
|
|
1170
|
+
/**
|
|
1171
|
+
### TreeItem.prototype.addChild
|
|
1172
|
+
> Creates a new child [TreeItem](#treeitem) from the given options, appends it to this item's `children`, and returns its index.
|
|
1173
|
+
|
|
1174
|
+
#### Parametres
|
|
1175
|
+
| name | type | description |
|
|
1176
|
+
| --- | --- | --- |
|
|
1177
|
+
| child_options | object | Run-time options for the new child, as accepted by [TreeItem](#treeitem). \[default: null\] |
|
|
1178
|
+
|
|
1179
|
+
#### Returns
|
|
1180
|
+
| type | description |
|
|
1181
|
+
| --- | --- |
|
|
1182
|
+
| number | The index of the newly added child within `children`. |
|
|
1183
|
+
|
|
1184
|
+
#### History
|
|
1185
|
+
| version | change |
|
|
1186
|
+
| --- | --- |
|
|
1187
|
+
| 0.1.1 | Now returns the index of the new child rather than the new length of `children`. |
|
|
1188
|
+
*/
|
|
1189
|
+
/**
|
|
1190
|
+
### TreeItem.prototype.stat
|
|
1191
|
+
> [`stat`s](#statoptions) the path of this item, storing the result in the item's `statObject` (from which `unixType` and `size` are derived).
|
|
1192
|
+
|
|
1193
|
+
#### Parametres
|
|
1194
|
+
| name | type | description |
|
|
1195
|
+
| --- | --- | --- |
|
|
1196
|
+
| input_options | object | Options passed to [statOptions](#statoptions). \[default: null\] |
|
|
1197
|
+
|
|
1198
|
+
#### Returns
|
|
1199
|
+
| type | description |
|
|
1200
|
+
| --- | --- |
|
|
1201
|
+
| Promise<object> | A promise which resolves to the item itself. |
|
|
1202
|
+
|
|
1203
|
+
#### Throws
|
|
1204
|
+
| code | type | condition |
|
|
1205
|
+
| --- | --- | --- |
|
|
1206
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1207
|
+
|
|
1208
|
+
#### History
|
|
1209
|
+
| version | change |
|
|
1210
|
+
| --- | --- |
|
|
1211
|
+
| 0.1.1 | WIP |
|
|
1212
|
+
*/
|
|
1213
|
+
/**
|
|
1214
|
+
### TreeItem.prototype.hash
|
|
1215
|
+
> Computes the [SHA2-256](https://en.wikipedia.org/wiki/SHA-2) hash digest of the file at the path of this item, stores it in the item's `sha256` property, and resolves to the formatted hash string.
|
|
1216
|
+
|
|
1217
|
+
#### Parametres
|
|
1218
|
+
| name | type | description |
|
|
1219
|
+
| --- | --- | --- |
|
|
1220
|
+
| input_options | object | Run-time options; see [hashThis](#hashthis). \[default: null\] |
|
|
1221
|
+
|
|
1222
|
+
#### Returns
|
|
1223
|
+
| type | description |
|
|
1224
|
+
| --- | --- |
|
|
1225
|
+
| Promise<string> | A promise which resolves to the formatted hash string. |
|
|
1226
|
+
|
|
1227
|
+
#### Throws
|
|
1228
|
+
| code | type | condition |
|
|
1229
|
+
| --- | --- | --- |
|
|
1230
|
+
| 'ERR_INVALID_ARG_VALUE' | TypeError | Thrown if a given option has the right type but an unusable value. |
|
|
1231
|
+
|
|
1232
|
+
#### History
|
|
1233
|
+
| version | change |
|
|
1234
|
+
| --- | --- |
|
|
1235
|
+
| 0.1.1 | WIP |
|
|
1236
|
+
*/
|
|
1237
|
+
/**
|
|
1238
|
+
### TreeItem.prototype.processAsync
|
|
1239
|
+
> Asynchronously processes this item and, when `recursive` is `true`, its descendants: statting, optionally hashing, and creating child items. See [processAsyncThis](#processasyncthis) for the full list of options.
|
|
1240
|
+
|
|
1241
|
+
#### Parametres
|
|
1242
|
+
| name | type | description |
|
|
1243
|
+
| --- | --- | --- |
|
|
1244
|
+
| input_options | object | Run-time options; see [processAsyncThis](#processasyncthis). \[default: null\] |
|
|
1245
|
+
|
|
1246
|
+
#### Returns
|
|
1247
|
+
| type | description |
|
|
1248
|
+
| --- | --- |
|
|
1249
|
+
| Promise<object> | A promise which resolves to the item itself. |
|
|
1250
|
+
|
|
1251
|
+
#### Throws
|
|
1252
|
+
| code | type | condition |
|
|
1253
|
+
| --- | --- | --- |
|
|
1254
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1255
|
+
|
|
1256
|
+
#### History
|
|
1257
|
+
| version | change |
|
|
1258
|
+
| --- | --- |
|
|
1259
|
+
| 0.1.1 | WIP |
|
|
1260
|
+
*/
|
|
1261
|
+
/**
|
|
1262
|
+
### TreeItem.prototype.aggregate
|
|
1263
|
+
> Computes dirstat-like aggregate statistics for the tree rooted at this item; see [aggregateThis](#aggregatethis).
|
|
1264
|
+
|
|
1265
|
+
#### Parametres
|
|
1266
|
+
| name | type | description |
|
|
1267
|
+
| --- | --- | --- |
|
|
1268
|
+
| input_options | object | Run-time options; see [aggregateThis](#aggregatethis). \[default: null\] |
|
|
1269
|
+
|
|
1270
|
+
#### Returns
|
|
1271
|
+
| type | description |
|
|
1272
|
+
| --- | --- |
|
|
1273
|
+
| object | The item itself, for chaining. |
|
|
1274
|
+
|
|
1275
|
+
#### History
|
|
1276
|
+
| version | change |
|
|
1277
|
+
| --- | --- |
|
|
1278
|
+
| 0.1.1 | Introduced. |
|
|
1279
|
+
*/
|
|
1280
|
+
/**
|
|
1281
|
+
### TreeItem.prototype.serialise
|
|
1282
|
+
> Returns a plain-object representation of the tree rooted at this item; see [serialiseThis](#serialisethis).
|
|
1283
|
+
|
|
1284
|
+
#### Parametres
|
|
1285
|
+
| name | type | description |
|
|
1286
|
+
| --- | --- | --- |
|
|
1287
|
+
| input_options | object | Run-time options; see [serialiseThis](#serialisethis). \[default: null\] |
|
|
1288
|
+
|
|
1289
|
+
#### Returns
|
|
1290
|
+
| type | description |
|
|
1291
|
+
| --- | --- |
|
|
1292
|
+
| object | A plain object representation of the tree. |
|
|
1293
|
+
|
|
1294
|
+
#### History
|
|
1295
|
+
| version | change |
|
|
1296
|
+
| --- | --- |
|
|
1297
|
+
| 0.1.1 | Introduced. */
|
|
1298
|
+
/**
|
|
1299
|
+
### TreeItem.createFromDirectory
|
|
1300
|
+
> Asynchronously processes the directory at the given path into a full [TreeItem](#treeitem) tree \(see [TreeItem.prototype.processAsync](#treeitemprototypeprocessasync) with `recursive: true`\) and resolves to the root item.
|
|
1301
|
+
|
|
1302
|
+
#### Parametres
|
|
1303
|
+
| name | type | description |
|
|
1304
|
+
| --- | --- | --- |
|
|
1305
|
+
| input_directory | string | The path of the directory to process; the process's working directory is used when `null`. \[default: null\] |
|
|
1306
|
+
|
|
1307
|
+
#### Returns
|
|
1308
|
+
| type | description |
|
|
1309
|
+
| --- | --- |
|
|
1310
|
+
| Promise<object> | A promise which resolves to the root `TreeItem` of the processed tree. |
|
|
1311
|
+
|
|
1312
|
+
#### Throws
|
|
1313
|
+
| code | type | condition |
|
|
1314
|
+
| --- | --- | --- |
|
|
1315
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1316
|
+
|
|
1317
|
+
#### History
|
|
1318
|
+
| version | change |
|
|
1319
|
+
| --- | --- |
|
|
1320
|
+
| 0.1.1 | Now resolves to the processed root `TreeItem`. |
|
|
1321
|
+
*/
|
|
1322
|
+
TreeItem.createFromDirectory = function( input_directory = null ){
|
|
1323
|
+
var _return = null;
|
|
1324
|
+
var item = new TreeItem( { path: input_directory } );
|
|
1325
|
+
_return = item.processAsync( { recursive: true } );
|
|
1326
|
+
return _return;
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
### TreeRoot
|
|
1330
|
+
> Smart constructor for the root node of a filesystem tree; a minimal wrapper pairing a `name` with an empty `children` array. Can also be called without `new`.
|
|
1331
|
+
|
|
1332
|
+
#### Parametres
|
|
1333
|
+
| name | type | description |
|
|
1334
|
+
| --- | --- | --- |
|
|
1335
|
+
| input_options | object | Run-time options. \[default: null\] |
|
|
1336
|
+
|
|
1337
|
+
##### `options` Properties
|
|
1338
|
+
| name | type | default | description |
|
|
1339
|
+
| --- | --- | --- | --- |
|
|
1340
|
+
| name | string | '[root]' | The name of the root node. |
|
|
1341
|
+
| children | Array | [] | The children of the root node. |
|
|
1342
|
+
|
|
1343
|
+
#### History
|
|
1344
|
+
| version | change |
|
|
1345
|
+
| --- | --- |
|
|
1346
|
+
| 0.1.1 | Added documentation. |
|
|
1347
|
+
*/
|
|
1348
|
+
function TreeRoot( input_options ){
|
|
1349
|
+
if( !new.target ){
|
|
1350
|
+
return new TreeRoot( input_options );
|
|
1351
|
+
}
|
|
1352
|
+
Object.defineProperties( this, {
|
|
1353
|
+
name: {
|
|
1354
|
+
value: input_options?.name ?? '[root]',
|
|
1355
|
+
enumerable: true,
|
|
1356
|
+
writable: true
|
|
1357
|
+
},
|
|
1358
|
+
children: {
|
|
1359
|
+
value: input_options?.children ?? [],
|
|
1360
|
+
enumerable: true,
|
|
1361
|
+
writable: true
|
|
1362
|
+
}
|
|
1363
|
+
} );
|
|
1364
|
+
return this;
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
### getPathFromRoot
|
|
1368
|
+
> Creates and returns a path string, relative to the the filesystem's root, from provided path fragments.
|
|
1369
|
+
|
|
1370
|
+
#### Parametres
|
|
1371
|
+
| name | type | description |
|
|
1372
|
+
| --- | --- | --- |
|
|
1373
|
+
| ...segments | string[] | The segments to combine ala [Node's PathNS.join](https://nodejs.org/api/path.html#pathjoinpaths). |
|
|
1374
|
+
|
|
1375
|
+
#### Returns
|
|
1376
|
+
| type | description |
|
|
1377
|
+
| --- | --- |
|
|
1378
|
+
| string | The root-relative path string made by joining the segments. |
|
|
1379
|
+
|
|
1380
|
+
#### Throws
|
|
1381
|
+
| code | type | condition |
|
|
1382
|
+
| --- | --- | --- |
|
|
1383
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1384
|
+
|
|
1385
|
+
#### History
|
|
1386
|
+
| version | change |
|
|
1387
|
+
| --- | --- |
|
|
1388
|
+
| 0.0.1 | WIP |
|
|
1389
|
+
*/
|
|
1390
|
+
function getPathFromRoot( ...segments ){
|
|
1391
|
+
const FUNCTION_NAME = 'getPathFromRoot';
|
|
1392
|
+
// Variables
|
|
1393
|
+
//var arguments_array = Array.from(arguments);
|
|
1394
|
+
var _return = null;
|
|
1395
|
+
//var return_error = null;
|
|
1396
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${segments.join(',')}`});
|
|
1397
|
+
// Parametre checks
|
|
1398
|
+
/*if( typeof(...segments) !== 'string[]' ){
|
|
1399
|
+
return_error = new TypeError('Param "...segments" is not of type string[].');
|
|
1400
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1401
|
+
throw return_error;
|
|
1402
|
+
}*/
|
|
1403
|
+
_return = PathNS.join( '/', ...segments );
|
|
1404
|
+
|
|
1405
|
+
// Function
|
|
1406
|
+
// Return
|
|
1407
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1408
|
+
return _return;
|
|
1409
|
+
} // getPathFromRoot
|
|
1410
|
+
/**
|
|
1411
|
+
### getPathFromCWD
|
|
1412
|
+
> Creates and returns a path string, relative to the process's current working directory, from provided path fragments.
|
|
1413
|
+
|
|
1414
|
+
#### Parametres
|
|
1415
|
+
| name | type | description |
|
|
1416
|
+
| --- | --- | --- |
|
|
1417
|
+
| ...segments | string[] | The segments to combine ala [Node's PathNS.join](https://nodejs.org/api/path.html#pathjoinpaths). |
|
|
1418
|
+
|
|
1419
|
+
#### Returns
|
|
1420
|
+
| type | description |
|
|
1421
|
+
| --- | --- |
|
|
1422
|
+
| string | The path string made by joining the segments. |
|
|
1423
|
+
|
|
1424
|
+
#### Throws
|
|
1425
|
+
| code | type | condition |
|
|
1426
|
+
| --- | --- | --- |
|
|
1427
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1428
|
+
|
|
1429
|
+
#### History
|
|
1430
|
+
| version | change |
|
|
1431
|
+
| --- | --- |
|
|
1432
|
+
| 0.0.1 | WIP |
|
|
1433
|
+
*/
|
|
1434
|
+
function getPathFromCWD( ...segments ){
|
|
1435
|
+
const FUNCTION_NAME = 'getPathFromCWD';
|
|
1436
|
+
// Variables
|
|
1437
|
+
//var arguments_array = Array.from(arguments);
|
|
1438
|
+
var _return = null;
|
|
1439
|
+
//var return_error = null;
|
|
1440
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${segments.join(',')}`});
|
|
1441
|
+
// Parametre checks
|
|
1442
|
+
/*if( typeof(...segments) !== 'string[]' ){
|
|
1443
|
+
return_error = new TypeError('Param "...segments" is not of type string[].');
|
|
1444
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1445
|
+
throw return_error;
|
|
1446
|
+
}*/
|
|
1447
|
+
_return = PathNS.join( process.cwd(), ...segments );
|
|
1448
|
+
|
|
1449
|
+
// Function
|
|
1450
|
+
// Return
|
|
1451
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1452
|
+
return _return;
|
|
1453
|
+
} // getPathFromCWD
|
|
1454
|
+
/**
|
|
1455
|
+
### getPathFromHome
|
|
1456
|
+
> Creates and returns a path string, relative to the user's home directory, from provided path fragments.
|
|
1457
|
+
|
|
1458
|
+
#### Parametres
|
|
1459
|
+
| name | type | description |
|
|
1460
|
+
| --- | --- | --- |
|
|
1461
|
+
| ...segments | string[] | The segments to combine ala [Node's PathNS.join](https://nodejs.org/api/path.html#pathjoinpaths). |
|
|
1462
|
+
|
|
1463
|
+
#### Returns
|
|
1464
|
+
| type | description |
|
|
1465
|
+
| --- | --- |
|
|
1466
|
+
| string | The home-relative path string made by joining the segments. |
|
|
1467
|
+
|
|
1468
|
+
#### Throws
|
|
1469
|
+
| code | type | condition |
|
|
1470
|
+
| --- | --- | --- |
|
|
1471
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1472
|
+
|
|
1473
|
+
#### History
|
|
1474
|
+
| version | change |
|
|
1475
|
+
| --- | --- |
|
|
1476
|
+
| 0.0.1 | WIP |
|
|
1477
|
+
*/
|
|
1478
|
+
function getPathFromHome( ...segments ){
|
|
1479
|
+
const FUNCTION_NAME = 'getPathFromHome';
|
|
1480
|
+
// Variables
|
|
1481
|
+
//var arguments_array = Array.from(arguments);
|
|
1482
|
+
var _return = null;
|
|
1483
|
+
//var return_error = null;
|
|
1484
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${segments.join(',')}`});
|
|
1485
|
+
// Parametre checks
|
|
1486
|
+
/*if( typeof(...segments) !== 'string[]' ){
|
|
1487
|
+
return_error = new TypeError('Param "...segments" is not of type string[].');
|
|
1488
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1489
|
+
throw return_error;
|
|
1490
|
+
}*/
|
|
1491
|
+
_return = PathNS.join( OSNS.homedir(), ...segments );
|
|
1492
|
+
|
|
1493
|
+
// Function
|
|
1494
|
+
// Return
|
|
1495
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1496
|
+
return _return;
|
|
1497
|
+
} // getPathFromHome
|
|
1498
|
+
|
|
1499
|
+
/**
|
|
1500
|
+
### addUnixType
|
|
1501
|
+
> Adds a `unixType` property to the given stat object, describing the kind of filesystem entry it represents, by [resolving the type bitflags in its `mode`](https://nodejs.org/api/fs.html#file-type-constants), and returns it. Works with both number- and `BigInt`-valued stat objects.
|
|
1502
|
+
|
|
1503
|
+
#### Parametres
|
|
1504
|
+
| name | type | description |
|
|
1505
|
+
| --- | --- | --- |
|
|
1506
|
+
| stat_object | object | The stat object to augment. |
|
|
1507
|
+
|
|
1508
|
+
#### Returns
|
|
1509
|
+
| type | description |
|
|
1510
|
+
| --- | --- |
|
|
1511
|
+
| object | The same stat object, with a `unixType` property added. |
|
|
1512
|
+
|
|
1513
|
+
#### History
|
|
1514
|
+
| version | change |
|
|
1515
|
+
| --- | --- |
|
|
1516
|
+
| 0.1.1 | Fixed the `FSNS.constants` typo and added `BigInt` handling. |
|
|
1517
|
+
*/
|
|
1518
|
+
function addUnixType( stat_object ){
|
|
1519
|
+
var _return = stat_object;
|
|
1520
|
+
var mode_number = Number( stat_object?.mode ?? Number.NaN );
|
|
1521
|
+
if( Number.isNaN( mode_number ) === true ){
|
|
1522
|
+
_return.unixType = 'unknown';
|
|
1523
|
+
return _return;
|
|
1524
|
+
}
|
|
1525
|
+
switch( ( mode_number & FSNS.constants.S_IFMT ) ){
|
|
1526
|
+
case FSNS.constants.S_IFIFO: _return.unixType = 'fifo'; break;
|
|
1527
|
+
case FSNS.constants.S_IFCHR: _return.unixType = 'character-device'; break;
|
|
1528
|
+
case FSNS.constants.S_IFDIR: _return.unixType = 'directory'; break;
|
|
1529
|
+
case FSNS.constants.S_IFBLK: _return.unixType = 'block-device'; break;
|
|
1530
|
+
case FSNS.constants.S_IFREG: _return.unixType = 'file'; break;
|
|
1531
|
+
case FSNS.constants.S_IFLNK: _return.unixType = 'symbolic-link'; break;
|
|
1532
|
+
case FSNS.constants.S_IFSOCK: _return.unixType = 'socket'; break;
|
|
1533
|
+
default: _return.unixType = 'unknown'; break;
|
|
1534
|
+
}
|
|
1535
|
+
return _return;
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
/**
|
|
1539
|
+
### getFormattedStat
|
|
1540
|
+
> Returns the reformatted stat object from the given stat object.
|
|
1541
|
+
|
|
1542
|
+
#### Parametres
|
|
1543
|
+
| name | type | description |
|
|
1544
|
+
| --- | --- | --- |
|
|
1545
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
1546
|
+
|
|
1547
|
+
##### `options` Properties
|
|
1548
|
+
| name | type | default | description |
|
|
1549
|
+
| --- | --- | --- | --- |
|
|
1550
|
+
| statObject | object | {} | The stat object to reformat. |
|
|
1551
|
+
| propertyNames | object | { dev: 'deviceID', ino: 'inode', mode: true, nlink: 'numberOfLinks', uid: 'userID', gid: 'groupID', rdev: 'representsDevice', size: 'fileSize', blksize: 'blockSize', blocks: true, atime: 'accessTime', ctime: 'changeTime', mtime: 'modificationTime', birthtime: 'birthTime' } | Maps source property names to destination property names; `false` omits the property, `true` or an unrecognised value keeps the original name. |
|
|
1552
|
+
| unixType | boolean | `true` | Adds an additional `unixType` property to the stat object based on the stat objects `mode` bitflag. |
|
|
1553
|
+
|
|
1554
|
+
#### Returns
|
|
1555
|
+
| type | description |
|
|
1556
|
+
| --- | --- |
|
|
1557
|
+
| object | The newly formatted stat object. |
|
|
1558
|
+
|
|
1559
|
+
#### Throws
|
|
1560
|
+
| code | type | condition |
|
|
1561
|
+
| --- | --- | --- |
|
|
1562
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1563
|
+
|
|
1564
|
+
#### History
|
|
1565
|
+
| version | change |
|
|
1566
|
+
| --- | --- |
|
|
1567
|
+
| 0.1.1 | Fixed the undefined `arguments_array` logging bug. |
|
|
1568
|
+
| 0.0.1 | WIP |
|
|
1569
|
+
*/
|
|
1570
|
+
function getFormattedStat( input_options = {} ){
|
|
1571
|
+
const FUNCTION_NAME = 'getFormattedStat';
|
|
1572
|
+
const DEFAULT_OPTIONS = {
|
|
1573
|
+
noop: false, // Skip primary functionality.
|
|
1574
|
+
noDefaults: false, // Don't apply static default options.
|
|
1575
|
+
noDynamic: false, // Don't apply dynamic default options.
|
|
1576
|
+
statObject: {}, // The stat object to reformat,
|
|
1577
|
+
propertyNames: { dev: 'deviceID', ino: 'inode', mode: true, nlink: 'numberOfLinks', uid: 'userID', gid: 'groupID', rdev: 'representsDevice', size: 'fileSize', blksize: 'blockSize', blocks: true, atime: 'accessTime', ctime: 'changeTime', mtime: 'modificationTime', birthtime: 'birthTime' }, // Options for customising the format. See below.
|
|
1578
|
+
unixType: true
|
|
1579
|
+
};//Variables
|
|
1580
|
+
var _return = null;
|
|
1581
|
+
var return_error = null;
|
|
1582
|
+
var arguments_array = Array.from(arguments);
|
|
1583
|
+
var options = {};
|
|
1584
|
+
var output_stat = {};
|
|
1585
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${arguments_array}`});
|
|
1586
|
+
//Parametre checks
|
|
1587
|
+
if( typeof(input_options) !== 'object' ){
|
|
1588
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
1589
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1590
|
+
throw return_error;
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
// Options
|
|
1594
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
1595
|
+
if( options.noop !== true ){
|
|
1596
|
+
// Function
|
|
1597
|
+
var property_names_condition = ( options.propertyNames != null && typeof(options.propertyNames) === 'object' );
|
|
1598
|
+
var unix_type_condition = ( options.unixType === true );
|
|
1599
|
+
if( property_names_condition || unix_type_condition ){
|
|
1600
|
+
if( property_names_condition ){
|
|
1601
|
+
for( const key of Object.keys( options.statObject ) ){
|
|
1602
|
+
if( options.propertyNames[key] === false ){
|
|
1603
|
+
continue;
|
|
1604
|
+
} else{
|
|
1605
|
+
if( typeof( options.propertyNames[key] ) === 'string' ){
|
|
1606
|
+
output_stat[options.propertyNames[key]] = options.statObject[key];
|
|
1607
|
+
} else{
|
|
1608
|
+
output_stat[key] = options.statObject[key];
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
} else{
|
|
1613
|
+
output_stat = options.statObject;
|
|
1614
|
+
}
|
|
1615
|
+
if( unix_type_condition ){
|
|
1616
|
+
output_stat = addUnixType( output_stat );
|
|
1617
|
+
}
|
|
1618
|
+
_return = output_stat;
|
|
1619
|
+
} else{
|
|
1620
|
+
_return = options.statObject;
|
|
1621
|
+
}
|
|
1622
|
+
} // noop
|
|
1623
|
+
//Return
|
|
1624
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1625
|
+
return _return;
|
|
1626
|
+
} // getFormattedStat
|
|
1627
|
+
|
|
1628
|
+
/**
|
|
1629
|
+
### statPath
|
|
1630
|
+
> [`stat`s](https://nodejs.org/api/fs.html#fspromisesstatpath-options) the given path, optionally [respecting symlinks](https://nodejs.org/api/fs.html#fspromiseslstatpath-options), and returning numeric values as [`BigInt`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
|
|
1631
|
+
|
|
1632
|
+
#### Parametres
|
|
1633
|
+
| name | type | description |
|
|
1634
|
+
| --- | --- | --- |
|
|
1635
|
+
| path | string | The filepath to stat. |
|
|
1636
|
+
| followSymlinks | boolean | Whether to follow symlink to the file they point to or stat the symlink itself. \[default: true\] |
|
|
1637
|
+
| bigint | boolean | Whether numeric values in the returned stat object should be `BigInt`s. \[default: false\] |
|
|
1638
|
+
|
|
1639
|
+
#### Returns
|
|
1640
|
+
| type | description |
|
|
1641
|
+
| --- | --- |
|
|
1642
|
+
| object | The stat object. |
|
|
1643
|
+
|
|
1644
|
+
#### Throws
|
|
1645
|
+
| code | type | condition |
|
|
1646
|
+
| --- | --- | --- |
|
|
1647
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1648
|
+
|
|
1649
|
+
#### History
|
|
1650
|
+
| version | change |
|
|
1651
|
+
| --- | --- |
|
|
1652
|
+
| 0.0.1 | WIP |
|
|
1653
|
+
*/
|
|
1654
|
+
function statPath( path, followSymlinks = true, bigint = false ){
|
|
1655
|
+
// Constants
|
|
1656
|
+
const FUNCTION_NAME = 'statPath';
|
|
1657
|
+
// Variables
|
|
1658
|
+
var _return = Promise.resolve(null);
|
|
1659
|
+
var return_error = null;
|
|
1660
|
+
// Parametre checks
|
|
1661
|
+
if( typeof(path) !== 'string' ){
|
|
1662
|
+
return_error = new TypeError('Param "path" is not of type string.');
|
|
1663
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1664
|
+
throw return_error;
|
|
1665
|
+
}
|
|
1666
|
+
if( typeof(followSymlinks) !== 'boolean' ){
|
|
1667
|
+
return_error = new TypeError('Param "followSymlinks" is not of type boolean.');
|
|
1668
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1669
|
+
throw return_error;
|
|
1670
|
+
}
|
|
1671
|
+
if( typeof(bigint) !== 'boolean' ){
|
|
1672
|
+
return_error = new TypeError('Param "bigint" is not of type boolean.');
|
|
1673
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1674
|
+
throw return_error;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
// Function
|
|
1678
|
+
if( followSymlinks !== true ){
|
|
1679
|
+
_return = FSNS.lstat( path, { bigint: bigint } ).then(
|
|
1680
|
+
null,
|
|
1681
|
+
( error ) => {
|
|
1682
|
+
return_error = new Error( `For path "${path}": FSNS.lstat rejected with an error: ${error}`, { cause: error } );
|
|
1683
|
+
throw return_error;
|
|
1684
|
+
}
|
|
1685
|
+
);
|
|
1686
|
+
} else{
|
|
1687
|
+
_return = FSNS.stat( path, { bigint: bigint } ).then(
|
|
1688
|
+
null,
|
|
1689
|
+
( error ) => {
|
|
1690
|
+
return_error = new Error( `For path "${path}": FSNS.stat rejected with an error: ${error}`, { cause: error } );
|
|
1691
|
+
throw return_error;
|
|
1692
|
+
}
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
// Return
|
|
1696
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1697
|
+
return _return;
|
|
1698
|
+
} // statPath
|
|
1699
|
+
|
|
1700
|
+
/**
|
|
1701
|
+
### statOptions
|
|
1702
|
+
> Performs a given `stat` call with options possiibly modifying the output.
|
|
1703
|
+
|
|
1704
|
+
#### Parametres
|
|
1705
|
+
| name | type | description |
|
|
1706
|
+
| --- | --- | --- |
|
|
1707
|
+
| input_options | object | Run-time options. \[default: {}\] |
|
|
1708
|
+
|
|
1709
|
+
##### `options` Properties
|
|
1710
|
+
| name | type | default | description |
|
|
1711
|
+
| --- | --- | --- | --- |
|
|
1712
|
+
| path | string | '' | The path to stat. |
|
|
1713
|
+
| followSymbolicLinks | boolean | false | `true` to follow symlinks and [stat the file they point to](https://nodejs.org/api/fs.html#fspromisesstatpath-options), or `false` to [stat the symlink itself](https://nodejs.org/api/fs.html#fspromiseslstatpath-options). |
|
|
1714
|
+
| bigint | boolean | false | Whether to return values as [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)s rather than regular numbers. |
|
|
1715
|
+
| unixType | boolean | true | Whether to add a `unixType` property by [resolving the bitflags in the file mode bytes](https://nodejs.org/api/fs.html#file-type-constants). |
|
|
1716
|
+
| recursive | boolean | false | \[Reserved\] Whether to recursively stat any subdirectories and their children under path. |
|
|
1717
|
+
| sha256 | boolean | false | \[Reserved\] Whether to add a `sha256` property to the output containing the SHA2 256 hash digest as a hexadecimal string; use [TreeItem.prototype.hash](#treeitemprototypehash) or [dirstat](#dirstat) with `hash: true` instead. |
|
|
1718
|
+
| verbosePropertyNames | boolean | false | Whether to replace the normal technical stat object property names with more descriptive ones via [getFormattedStat](#getformattedstat). |
|
|
1719
|
+
|
|
1720
|
+
#### Returns
|
|
1721
|
+
| type | description |
|
|
1722
|
+
| --- | --- |
|
|
1723
|
+
| object | The modified stat object. |
|
|
1724
|
+
|
|
1725
|
+
#### Throws
|
|
1726
|
+
| code | type | condition |
|
|
1727
|
+
| --- | --- | --- |
|
|
1728
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1729
|
+
|
|
1730
|
+
#### History
|
|
1731
|
+
| version | change |
|
|
1732
|
+
| --- | --- |
|
|
1733
|
+
| 0.1.1 | Implemented `unixType`, fixed `verbosePropertyNames`, marked `recursive` and `sha256` as reserved. |
|
|
1734
|
+
| 0.0.1 | WIP |
|
|
1735
|
+
*/
|
|
1736
|
+
function statOptions( input_options = {} ){
|
|
1737
|
+
|
|
1738
|
+
// Constants
|
|
1739
|
+
const FUNCTION_NAME = 'statOptions';
|
|
1740
|
+
const DEFAULT_OPTIONS = {
|
|
1741
|
+
path: '', // The path to stat.
|
|
1742
|
+
followSymbolicLinks: false, // `true` to follow symlinks and stat the file they point to, `false` to stat the symlink itself.
|
|
1743
|
+
bigint: false, // Whether to return values as [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)s rather than regular numbers.
|
|
1744
|
+
unixType: true, // Whether to add a `unixType` property by [resolving the bitflags in the file mode bytes](https://nodejs.org/api/fs.html#file-type-constants).
|
|
1745
|
+
recursive: false, // [Reserved]
|
|
1746
|
+
sha256: false, // [Reserved]
|
|
1747
|
+
verbosePropertyNames: false, // Whether to replace the normal technical stat object property names with more descriptive ones.
|
|
1748
|
+
};// Variables
|
|
1749
|
+
var _return = null;
|
|
1750
|
+
var return_error = null;
|
|
1751
|
+
// Parametre checks
|
|
1752
|
+
if( typeof(input_options) !== 'object' ){
|
|
1753
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
1754
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1755
|
+
throw return_error;
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
// Options
|
|
1759
|
+
var options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS );
|
|
1760
|
+
if( options.validationFunction( options ) === true ){
|
|
1761
|
+
if( options.noop !== true ){
|
|
1762
|
+
// Function
|
|
1763
|
+
_return = statPath( options.path, options.followSymbolicLinks, options.bigint ).then(
|
|
1764
|
+
( stat_object ) => {
|
|
1765
|
+
if( options.unixType === true ){
|
|
1766
|
+
stat_object.unixType = getUnixTypeFromStatObject( stat_object );
|
|
1767
|
+
}
|
|
1768
|
+
if( options.verbosePropertyNames === true ){
|
|
1769
|
+
return getFormattedStat( { statObject: stat_object, unixType: false } );
|
|
1770
|
+
} else{
|
|
1771
|
+
return stat_object;
|
|
1772
|
+
}
|
|
1773
|
+
},
|
|
1774
|
+
( error ) => {
|
|
1775
|
+
return_error = new Error( `statPath rejected with error: ${error}`, { cause: error } );
|
|
1776
|
+
throw return_error;
|
|
1777
|
+
}
|
|
1778
|
+
);
|
|
1779
|
+
} // noop
|
|
1780
|
+
} // validation_function
|
|
1781
|
+
// Return
|
|
1782
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1783
|
+
return _return;
|
|
1784
|
+
} // statOptions
|
|
1785
|
+
|
|
1786
|
+
/**
|
|
1787
|
+
### dirstat
|
|
1788
|
+
> Asynchronously processes the file or directory at the given path into a [dirstat](https://github.com/cdown/dirstat)-like tree of filesystem statistics: every entry is [stat](#statoptions)'d, directories are read recursively, regular files are optionally [hashed](#treeitemprototypehash), and every node is given dirstat-like aggregates via [TreeItem.prototype.aggregate](#treeitemprototypeaggregate) \(`totalSize`, `fileCount`, `directoryCount`, `otherCount`, and `percentageOfParent`\). The result is a [TreeItem](#treeitem) tree rooted at the given path.
|
|
1789
|
+
|
|
1790
|
+
#### Parametres
|
|
1791
|
+
| name | type | description |
|
|
1792
|
+
| --- | --- | --- |
|
|
1793
|
+
| input_options | object | Run-time options. \[default: {}\\] |
|
|
1794
|
+
|
|
1795
|
+
##### `options` Properties
|
|
1796
|
+
| name | type | default | description |
|
|
1797
|
+
| --- | --- | --- | --- |
|
|
1798
|
+
| path | string | `process.cwd()` (dynamic) | The path of the file or directory to process. |
|
|
1799
|
+
| normalisePath | boolean | true | Whether to [normalise](https://nodejs.org/api/path.html#pathnormalizepath) the path before processing it. |
|
|
1800
|
+
| recursive | boolean | true | Whether to descend into child directories; when `false`, only the entry at `path` itself is processed (equivalent to `maxDepth: 0`). |
|
|
1801
|
+
| maxDepth | number | Infinity | The maximum depth to descend into child directories; entries at the maximum depth are listed but not descended into. Must be `>= 0`. |
|
|
1802
|
+
| excludeRegExpArray | Array | [] | An array of `RegExp` objects; entries whose name or path relative to `path` match any of them are omitted from the tree and not descended into. |
|
|
1803
|
+
| statOptions | object | { followSymbolicLinks: true, bigint: false } | Options passed to [statOptions](#statoptions) for each entry; use `bigint: true` to get `BigInt` sizes. |
|
|
1804
|
+
| hash | boolean | false | Whether to compute the [SHA2-256](https://en.wikipedia.org/wiki/SHA-2) hash digest of every regular file, stored as a Base16 string in each node's `sha256` property. |
|
|
1805
|
+
| hashOptions | object | null | Options passed to [TreeItem.prototype.hash](#treeitemprototypehash). |
|
|
1806
|
+
| formatOptions | object | null | Options passed to [TreeItem.prototype.hash](#treeitemprototypehash). |
|
|
1807
|
+
| readdirOptions | object | null | Options passed to [getDirentArrayFromDirectoryPath](#getdirentarrayfromdirectorypath) for each directory; only `encoding` is currently meaningful. |
|
|
1808
|
+
| sortBySize | boolean | false | Whether to sort the `children` of every directory by `totalSize`, largest first, once processing is complete. |
|
|
1809
|
+
|
|
1810
|
+
#### Returns
|
|
1811
|
+
| type | description |
|
|
1812
|
+
| --- | --- |
|
|
1813
|
+
| Promise<object> | A promise which resolves to the root [TreeItem](#treeitem) of the processed tree. |
|
|
1814
|
+
|
|
1815
|
+
#### Throws
|
|
1816
|
+
| code | type | condition |
|
|
1817
|
+
| --- | --- | --- |
|
|
1818
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | Thrown if a given argument isn't of the correct type. |
|
|
1819
|
+
| 'ERR_INVALID_ARG_VALUE' | TypeError | Thrown if a given argument has the right type but an unusable value, e.g. a non-`RegExp` in `excludeRegExpArray`. |
|
|
1820
|
+
|
|
1821
|
+
#### History
|
|
1822
|
+
| version | change |
|
|
1823
|
+
| --- | --- |
|
|
1824
|
+
| 0.1.1 | Rewrote as a complete filesystem tree processor. |
|
|
1825
|
+
| 0.0.1 | WIP |
|
|
1826
|
+
*/
|
|
1827
|
+
function dirstat( input_options = null ){
|
|
1828
|
+
const FUNCTION_NAME = 'dirstat';
|
|
1829
|
+
const DEFAULT_OPTIONS = {
|
|
1830
|
+
path: '', // The path to dirstat; dynamically defaults to `process.cwd()`.
|
|
1831
|
+
normalisePath: true, // Whether to normalise the path before processing it.
|
|
1832
|
+
recursive: true, // Whether to descend into child directories.
|
|
1833
|
+
maxDepth: Number.POSITIVE_INFINITY, // The maximum depth to descend into child directories.
|
|
1834
|
+
excludeRegExpArray: [], // An array of `RegExp` objects used to omit entries by name or path relative to `path`.
|
|
1835
|
+
statOptions: { followSymbolicLinks: true, bigint: false }, // Options passed to `statOptions` for each entry.
|
|
1836
|
+
hash: false, // Whether to hash every regular file.
|
|
1837
|
+
hashOptions: null, // Options passed to `hashThis`.
|
|
1838
|
+
formatOptions: null, // Options passed to `hashThis`.
|
|
1839
|
+
readdirOptions: null, // Options passed to `getDirentArrayFromDirectoryPath`.
|
|
1840
|
+
sortBySize: false // Whether to sort every directory's children by `totalSize`, largest first.
|
|
1841
|
+
};// Variables
|
|
1842
|
+
var _return = null;
|
|
1843
|
+
var return_error = null;
|
|
1844
|
+
var options = {};
|
|
1845
|
+
var target_path = '';
|
|
1846
|
+
var root_item = null;
|
|
1847
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${typeof(input_options)}`});
|
|
1848
|
+
// Parametre checks
|
|
1849
|
+
if( typeof(input_options) !== 'object' ){
|
|
1850
|
+
return_error = new TypeError('Param "input_options" is not of type object.');
|
|
1851
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1852
|
+
throw return_error;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
// Options
|
|
1856
|
+
options = deriveOptions.call( this, input_options, DEFAULT_OPTIONS, ( pre_dynamic ) => {
|
|
1857
|
+
var post_dynamic = {};
|
|
1858
|
+
post_dynamic.path = isDefined( pre_dynamic.path ) === true && pre_dynamic.path !== '' ? pre_dynamic.path : process.cwd();
|
|
1859
|
+
return post_dynamic;
|
|
1860
|
+
} );
|
|
1861
|
+
if( typeof( options.path ) !== 'string' ){
|
|
1862
|
+
return_error = new TypeError('Option "path" is not of type string.');
|
|
1863
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1864
|
+
throw return_error;
|
|
1865
|
+
}
|
|
1866
|
+
if( typeof( options.normalisePath ) !== 'boolean' ){
|
|
1867
|
+
return_error = new TypeError('Option "normalisePath" is not of type boolean.');
|
|
1868
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1869
|
+
throw return_error;
|
|
1870
|
+
}
|
|
1871
|
+
if( typeof( options.recursive ) !== 'boolean' ){
|
|
1872
|
+
return_error = new TypeError('Option "recursive" is not of type boolean.');
|
|
1873
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1874
|
+
throw return_error;
|
|
1875
|
+
}
|
|
1876
|
+
if( typeof( options.maxDepth ) !== 'number' || Number.isNaN( options.maxDepth ) || options.maxDepth < 0 ){
|
|
1877
|
+
return_error = new TypeError('Option "maxDepth" is not a number greater than or equal to zero.');
|
|
1878
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1879
|
+
throw return_error;
|
|
1880
|
+
}
|
|
1881
|
+
if( Array.isArray( options.excludeRegExpArray ) !== true ){
|
|
1882
|
+
return_error = new TypeError('Option "excludeRegExpArray" is not of type Array.');
|
|
1883
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1884
|
+
throw return_error;
|
|
1885
|
+
}
|
|
1886
|
+
for( const regexp of options.excludeRegExpArray ){
|
|
1887
|
+
if( !( regexp instanceof RegExp ) ){
|
|
1888
|
+
return_error = new TypeError(`Option "excludeRegExpArray" contains a non-RegExp value: ${regexp}`);
|
|
1889
|
+
return_error.code = 'ERR_INVALID_ARG_VALUE';
|
|
1890
|
+
throw return_error;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
if( typeof( options.hash ) !== 'boolean' ){
|
|
1894
|
+
return_error = new TypeError('Option "hash" is not of type boolean.');
|
|
1895
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1896
|
+
throw return_error;
|
|
1897
|
+
}
|
|
1898
|
+
if( typeof( options.sortBySize ) !== 'boolean' ){
|
|
1899
|
+
return_error = new TypeError('Option "sortBySize" is not of type boolean.');
|
|
1900
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1901
|
+
throw return_error;
|
|
1902
|
+
}
|
|
1903
|
+
if( isDefined( options.statOptions ) === true && typeof( options.statOptions ) !== 'object' ){
|
|
1904
|
+
return_error = new TypeError('Option "statOptions" is not of type object.');
|
|
1905
|
+
return_error.code = 'ERR_INVALID_ARG_TYPE';
|
|
1906
|
+
throw return_error;
|
|
1907
|
+
}
|
|
1908
|
+
if( options.noop === true ){
|
|
1909
|
+
return Promise.resolve( null );
|
|
1910
|
+
}
|
|
1911
|
+
// Function
|
|
1912
|
+
target_path = options.normalisePath === true ? PathNS.normalize( options.path ) : options.path;
|
|
1913
|
+
root_item = new TreeItem( { path: target_path } );
|
|
1914
|
+
_return = root_item.processAsync( {
|
|
1915
|
+
recursive: options.recursive,
|
|
1916
|
+
stat: true,
|
|
1917
|
+
statOptions: options.statOptions,
|
|
1918
|
+
hash: options.hash,
|
|
1919
|
+
hashOptions: options.hashOptions,
|
|
1920
|
+
formatOptions: options.formatOptions,
|
|
1921
|
+
excludeRegExpArray: options.excludeRegExpArray,
|
|
1922
|
+
readdirOptions: options.readdirOptions,
|
|
1923
|
+
maxDepth: options.maxDepth,
|
|
1924
|
+
depth: 0,
|
|
1925
|
+
rootPath: root_item.absolutePath
|
|
1926
|
+
} ).then(
|
|
1927
|
+
() => {
|
|
1928
|
+
root_item.aggregate( { sortBySize: options.sortBySize } );
|
|
1929
|
+
return root_item;
|
|
1930
|
+
},
|
|
1931
|
+
( error ) => {
|
|
1932
|
+
return_error = new Error( `For path "${root_item.absolutePath}": dirstat caught error: ${error}`, { cause: error } );
|
|
1933
|
+
throw return_error;
|
|
1934
|
+
}
|
|
1935
|
+
);
|
|
1936
|
+
// Return
|
|
1937
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1938
|
+
return _return;
|
|
1939
|
+
} // dirstat
|
|
1940
|
+
|
|
1941
|
+
/**
|
|
1942
|
+
### AmEntryModule
|
|
1943
|
+
> Returns a promise which resolves to `true` if the current module, as in `import.meta.url`, is being executed directly from the commandline. Handles symlinks and [npx invoctions](https://stackoverflow.com/a/71925565) as well.
|
|
1944
|
+
|
|
1945
|
+
#### Parametres
|
|
1946
|
+
| name | type | description |
|
|
1947
|
+
| --- | --- | --- |
|
|
1948
|
+
| input_options | object | \[Reserved\] Additional runtime options. |
|
|
1949
|
+
|
|
1950
|
+
#### Returns
|
|
1951
|
+
| type | description |
|
|
1952
|
+
| --- | --- |
|
|
1953
|
+
| Promise<boolean> | A promise which resolves to `true` if the module specified by `import.meta.url` matches the one specified by `realpath( process.argv[1] )`; `false` if `process.argv[1]` is not a string (e.g. when running via `node -e`). |
|
|
1954
|
+
|
|
1955
|
+
#### Throws
|
|
1956
|
+
| code | type | condition |
|
|
1957
|
+
| --- | --- | --- |
|
|
1958
|
+
| 'ERR_INVALID_ARG_TYPE' | TypeError | \[Reserved\] Thrown if a given argument isn't of the correct type. |
|
|
1959
|
+
|
|
1960
|
+
#### History
|
|
1961
|
+
| version | change |
|
|
1962
|
+
| --- | --- |
|
|
1963
|
+
| 0.1.1 | No longer throws when `process.argv[1]` is undefined. |
|
|
1964
|
+
| 0.1.1 | WIP |
|
|
1965
|
+
*/
|
|
1966
|
+
function AmEntryModule(){
|
|
1967
|
+
const FUNCTION_NAME = 'AmEntryModule';
|
|
1968
|
+
const argv1_path = process.argv[1];
|
|
1969
|
+
var return_error = null;
|
|
1970
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `received: ${argv1_path}`});
|
|
1971
|
+
// Parametre checks
|
|
1972
|
+
if( typeof( argv1_path ) !== 'string' ){
|
|
1973
|
+
return Promise.resolve( false );
|
|
1974
|
+
}
|
|
1975
|
+
// Function
|
|
1976
|
+
var _return = FSNS.realpath( argv1_path, 'utf8' ).then(
|
|
1977
|
+
( real_path ) => {
|
|
1978
|
+
var real_url = URLNS.pathToFileURL( real_path );
|
|
1979
|
+
return ( import.meta.url === real_url.href );
|
|
1980
|
+
},
|
|
1981
|
+
( error ) => {
|
|
1982
|
+
return_error = new Error( `Caught an error when calling realpath with path=${argv1_path}: ${error}`, { cause: error } );
|
|
1983
|
+
throw return_error;
|
|
1984
|
+
}
|
|
1985
|
+
);
|
|
1986
|
+
// Return
|
|
1987
|
+
this?.logger?.log({file: FILENAME, function: FUNCTION_NAME, level: 'debug', message: `returned: ${_return}`});
|
|
1988
|
+
return _return;
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
const NAMESPACE = {};
|
|
1992
|
+
Object.defineProperties( NAMESPACE, {
|
|
1993
|
+
fs: {
|
|
1994
|
+
value: FSNS,
|
|
1995
|
+
enumerable: true
|
|
1996
|
+
},
|
|
1997
|
+
path: {
|
|
1998
|
+
value: PathNS,
|
|
1999
|
+
enumerable: true
|
|
2000
|
+
},
|
|
2001
|
+
url: {
|
|
2002
|
+
value: URLNS,
|
|
2003
|
+
enumerable: true
|
|
2004
|
+
},
|
|
2005
|
+
os: {
|
|
2006
|
+
value: OSNS,
|
|
2007
|
+
enumerable: true
|
|
2008
|
+
},
|
|
2009
|
+
hjson: {
|
|
2010
|
+
value: HJSON,
|
|
2011
|
+
enumerable: true
|
|
2012
|
+
},
|
|
2013
|
+
getHJSONFromPath: {
|
|
2014
|
+
value: getHJSONFromPath,
|
|
2015
|
+
enumerable: true
|
|
2016
|
+
},
|
|
2017
|
+
getHJSONFromObject: {
|
|
2018
|
+
value: getHJSONFromObject,
|
|
2019
|
+
enumerable: true
|
|
2020
|
+
},
|
|
2021
|
+
writeHJSONToPath: {
|
|
2022
|
+
value: writeHJSONToPath,
|
|
2023
|
+
enumerable: true
|
|
2024
|
+
},
|
|
2025
|
+
getDirentArrayFromDirectoryPath: {
|
|
2026
|
+
value: getDirentArrayFromDirectoryPath,
|
|
2027
|
+
enumerable: true
|
|
2028
|
+
},
|
|
2029
|
+
getUnixTypeFromStatObject: {
|
|
2030
|
+
value: getUnixTypeFromStatObject,
|
|
2031
|
+
enumerable: true
|
|
2032
|
+
},
|
|
2033
|
+
getUnixTypeFromDirentObject: {
|
|
2034
|
+
value: getUnixTypeFromDirentObject,
|
|
2035
|
+
enumerable: true
|
|
2036
|
+
},
|
|
2037
|
+
getPathFromDirentObject: {
|
|
2038
|
+
value: getPathFromDirentObject,
|
|
2039
|
+
enumerable: true
|
|
2040
|
+
},
|
|
2041
|
+
isExcludedFromPath: {
|
|
2042
|
+
value: isExcludedFromPath,
|
|
2043
|
+
enumerable: true
|
|
2044
|
+
},
|
|
2045
|
+
addSizes: {
|
|
2046
|
+
value: addSizes,
|
|
2047
|
+
enumerable: true
|
|
2048
|
+
},
|
|
2049
|
+
createChildThis: {
|
|
2050
|
+
value: createChildThis,
|
|
2051
|
+
enumerable: true
|
|
2052
|
+
},
|
|
2053
|
+
statThis: {
|
|
2054
|
+
value: statThis,
|
|
2055
|
+
enumerable: true
|
|
2056
|
+
},
|
|
2057
|
+
hashThis: {
|
|
2058
|
+
value: hashThis,
|
|
2059
|
+
enumerable: true
|
|
2060
|
+
},
|
|
2061
|
+
processAsyncThis: {
|
|
2062
|
+
value: processAsyncThis,
|
|
2063
|
+
enumerable: true
|
|
2064
|
+
},
|
|
2065
|
+
aggregateThis: {
|
|
2066
|
+
value: aggregateThis,
|
|
2067
|
+
enumerable: true
|
|
2068
|
+
},
|
|
2069
|
+
serialiseThis: {
|
|
2070
|
+
value: serialiseThis,
|
|
2071
|
+
enumerable: true
|
|
2072
|
+
},
|
|
2073
|
+
TreeItem: {
|
|
2074
|
+
value: TreeItem,
|
|
2075
|
+
enumerable: true
|
|
2076
|
+
},
|
|
2077
|
+
TreeRoot: {
|
|
2078
|
+
value: TreeRoot,
|
|
2079
|
+
enumerable: true
|
|
2080
|
+
},
|
|
2081
|
+
getNameFromPath: {
|
|
2082
|
+
value: getNameFromPath,
|
|
2083
|
+
enumerable: true
|
|
2084
|
+
},
|
|
2085
|
+
getDirectoryFromPath: {
|
|
2086
|
+
value: getDirectoryFromPath,
|
|
2087
|
+
enumerable: true
|
|
2088
|
+
},
|
|
2089
|
+
getPathFromRoot: {
|
|
2090
|
+
value: getPathFromRoot,
|
|
2091
|
+
enumerable: true
|
|
2092
|
+
},
|
|
2093
|
+
getPathFromCWD: {
|
|
2094
|
+
value: getPathFromCWD,
|
|
2095
|
+
enumerable: true
|
|
2096
|
+
},
|
|
2097
|
+
getPathFromHome: {
|
|
2098
|
+
value: getPathFromHome,
|
|
2099
|
+
enumerable: true
|
|
2100
|
+
},
|
|
2101
|
+
addUnixType: {
|
|
2102
|
+
value: addUnixType,
|
|
2103
|
+
enumerable: true
|
|
2104
|
+
},
|
|
2105
|
+
getFormattedStat: {
|
|
2106
|
+
value: getFormattedStat,
|
|
2107
|
+
enumerable: true
|
|
2108
|
+
},
|
|
2109
|
+
statPath: {
|
|
2110
|
+
value: statPath,
|
|
2111
|
+
enumerable: true
|
|
2112
|
+
},
|
|
2113
|
+
statOptions: {
|
|
2114
|
+
value: statOptions,
|
|
2115
|
+
enumerable: true
|
|
2116
|
+
},
|
|
2117
|
+
dirstat: {
|
|
2118
|
+
value: dirstat,
|
|
2119
|
+
enumerable: true
|
|
2120
|
+
},
|
|
2121
|
+
AmEntryModule: {
|
|
2122
|
+
value: AmEntryModule,
|
|
2123
|
+
enumerable: true
|
|
2124
|
+
}
|
|
2125
|
+
} );
|
|
2126
|
+
|
|
2127
|
+
export { NAMESPACE as default, getHJSONFromPath, getHJSONFromObject, writeHJSONToPath, getDirentArrayFromDirectoryPath, getUnixTypeFromStatObject, getUnixTypeFromDirentObject, getPathFromDirentObject, isExcludedFromPath, addSizes, createChildThis, statThis, hashThis, processAsyncThis, aggregateThis, serialiseThis, TreeItem, TreeRoot, getNameFromPath, getDirectoryFromPath, getPathFromRoot, getPathFromCWD, getPathFromHome, addUnixType, getFormattedStat, statPath, statOptions, dirstat, AmEntryModule };
|
|
2128
|
+
|
|
2129
|
+
// Main
|
|
2130
|
+
if( await AmEntryModule() === true ){
|
|
2131
|
+
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
|
|
2135
|
+
// cno-files.js EOF
|