@gesslar/toolkit 1.5.0 → 1.6.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/package.json +2 -2
- package/src/browser/lib/Data.js +0 -7
- package/src/browser/lib/Disposer.js +1 -24
- package/src/browser/lib/Util.js +1 -11
- package/src/types/browser/lib/Data.d.ts.map +1 -1
- package/src/types/browser/lib/Disposer.d.ts +0 -6
- package/src/types/browser/lib/Disposer.d.ts.map +1 -1
- package/src/types/browser/lib/Util.d.ts +0 -1
- package/src/types/browser/lib/Util.d.ts.map +1 -1
- package/src/browser/lib/Hook.js +0 -82
- package/src/browser/lib/test.js +0 -25
- package/src/browser/lib/test2.js +0 -46
- package/src/types/Cache.d.ts +0 -30
- package/src/types/Collection.d.ts +0 -321
- package/src/types/Contract.d.ts +0 -162
- package/src/types/Data.d.ts +0 -175
- package/src/types/DirectoryObject.d.ts +0 -135
- package/src/types/FS.d.ts +0 -40
- package/src/types/FileObject.d.ts +0 -388
- package/src/types/Glog.d.ts +0 -345
- package/src/types/Sass.d.ts +0 -24
- package/src/types/Schemer.d.ts +0 -179
- package/src/types/Tantrum.d.ts +0 -81
- package/src/types/Term.d.ts +0 -16
- package/src/types/Terms.d.ts +0 -145
- package/src/types/Type.d.ts +0 -26
- package/src/types/Util.d.ts +0 -275
- package/src/types/Valid.d.ts +0 -13
- package/src/types/browser/lib/Disposable.d.ts +0 -35
- package/src/types/browser/lib/Disposable.d.ts.map +0 -10
- package/src/types/browser/lib/Hook.d.ts +0 -11
- package/src/types/browser/lib/Hook.d.ts.map +0 -1
- package/src/types/browser/lib/test.d.ts +0 -2
- package/src/types/browser/lib/test.d.ts.map +0 -1
- package/src/types/browser/lib/test2.d.ts +0 -2
- package/src/types/browser/lib/test2.d.ts.map +0 -1
- package/src/types/lib/Chide.d.ts +0 -37
- package/src/types/lib/Chide.d.ts.map +0 -1
- package/src/types/lib/Collection.d.ts +0 -246
- package/src/types/lib/Collection.d.ts.map +0 -1
- package/src/types/lib/Data.d.ts +0 -206
- package/src/types/lib/Data.d.ts.map +0 -1
- package/src/types/lib/Disposable.d.ts +0 -33
- package/src/types/lib/Disposable.d.ts.map +0 -10
- package/src/types/lib/TypeSpec.d.ts +0 -92
- package/src/types/lib/TypeSpec.d.ts.map +0 -1
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
// Implementation: ../lib/FileObject.js
|
|
2
|
-
|
|
3
|
-
import DirectoryObject from './DirectoryObject.js'
|
|
4
|
-
import FS from './FS.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Configuration for data loading parsers in loadData method.
|
|
8
|
-
* Maps supported data types to their respective parser functions.
|
|
9
|
-
*/
|
|
10
|
-
export interface DataLoaderConfig {
|
|
11
|
-
[type: string]: Array<{ parse: (content: string) => unknown }>
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* FileObject encapsulates metadata and operations for a file, providing intelligent
|
|
16
|
-
* path resolution, metadata extraction, and file system operations. This class serves
|
|
17
|
-
* as the primary abstraction for file handling in the toolkit.
|
|
18
|
-
*
|
|
19
|
-
* FileObject automatically resolves relative paths, provides rich metadata without
|
|
20
|
-
* requiring file system access, and integrates seamlessly with DirectoryObject for
|
|
21
|
-
* hierarchical file operations. The class uses lazy evaluation for expensive operations
|
|
22
|
-
* and provides both synchronous metadata access and asynchronous file operations.
|
|
23
|
-
*
|
|
24
|
-
* Key features include automatic path normalization, extension parsing, URI generation,
|
|
25
|
-
* and parent directory integration. All path operations are cross-platform compatible.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```typescript
|
|
29
|
-
* import { FileObject } from '@gesslar/toolkit'
|
|
30
|
-
*
|
|
31
|
-
* // Basic file creation and metadata access
|
|
32
|
-
* const config = new FileObject('./config.json')
|
|
33
|
-
* console.log('Name:', config.name) // 'config.json'
|
|
34
|
-
* console.log('Module:', config.module) // 'config' (without extension)
|
|
35
|
-
* console.log('Extension:', config.extension) // '.json'
|
|
36
|
-
* console.log('Path:', config.path) // '/absolute/path/to/config.json'
|
|
37
|
-
* console.log('URI:', config.uri) // 'file:///absolute/path/to/config.json'
|
|
38
|
-
*
|
|
39
|
-
* // Working with different directory contexts
|
|
40
|
-
* const srcDir = new DirectoryObject('./src')
|
|
41
|
-
* const indexFile = new FileObject('index.js', srcDir)
|
|
42
|
-
* const componentFile = new FileObject('components/Button.tsx', './src')
|
|
43
|
-
*
|
|
44
|
-
* // File existence and operations
|
|
45
|
-
* if (await config.exists) {
|
|
46
|
-
* console.log('Config file exists')
|
|
47
|
-
* // File operations would go here using File utility
|
|
48
|
-
* } else {
|
|
49
|
-
* console.log('Config file not found at:', config.path)
|
|
50
|
-
* }
|
|
51
|
-
*
|
|
52
|
-
* // Integration with parent directory
|
|
53
|
-
* console.log('Parent directory:', indexFile.directory.path)
|
|
54
|
-
* console.log('Is in src?', indexFile.directory.name === 'src')
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
|
-
* @example
|
|
58
|
-
* ```typescript
|
|
59
|
-
* // Advanced usage with different file types and paths
|
|
60
|
-
* import { FileObject, DirectoryObject } from '@gesslar/toolkit'
|
|
61
|
-
*
|
|
62
|
-
* // Handle various file extensions and types
|
|
63
|
-
* const files = [
|
|
64
|
-
* new FileObject('./docs/README.md'),
|
|
65
|
-
* new FileObject('package.json'),
|
|
66
|
-
* new FileObject('../lib/utils.ts'),
|
|
67
|
-
* new FileObject('/absolute/path/to/script.sh')
|
|
68
|
-
* ]
|
|
69
|
-
*
|
|
70
|
-
* for (const fileItem of files) {
|
|
71
|
-
* console.log(`${fileItem.module}${fileItem.extension} -> ${fileItem.path}`)
|
|
72
|
-
*
|
|
73
|
-
* // Type-based processing
|
|
74
|
-
* switch (fileItem.extension) {
|
|
75
|
-
* case '.json':
|
|
76
|
-
* console.log('JSON file detected')
|
|
77
|
-
* break
|
|
78
|
-
* case '.md':
|
|
79
|
-
* console.log('Markdown documentation')
|
|
80
|
-
* break
|
|
81
|
-
* case '.ts':
|
|
82
|
-
* case '.js':
|
|
83
|
-
* console.log('JavaScript/TypeScript source')
|
|
84
|
-
* break
|
|
85
|
-
* default:
|
|
86
|
-
* console.log('Other file type')
|
|
87
|
-
* }
|
|
88
|
-
* }
|
|
89
|
-
*
|
|
90
|
-
* // Error handling for invalid paths
|
|
91
|
-
* try {
|
|
92
|
-
* const badFile = new FileObject('') // Empty path
|
|
93
|
-
* } catch (error) {
|
|
94
|
-
* console.error('Invalid file path:', error.message)
|
|
95
|
-
* }
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* @remarks
|
|
99
|
-
* FileObject is designed for metadata extraction and path operations. For actual
|
|
100
|
-
* file I/O operations (reading, writing, copying), use the File utility class
|
|
101
|
-
* which accepts FileObject instances as parameters.
|
|
102
|
-
*
|
|
103
|
-
* The `exists` property returns a Promise and should always be awaited. Path
|
|
104
|
-
* resolution happens synchronously during construction, making metadata access
|
|
105
|
-
* immediate and efficient.
|
|
106
|
-
*
|
|
107
|
-
* When working with DirectoryObject parents, the FileObject automatically
|
|
108
|
-
* inherits the directory's resolved path, ensuring consistency in hierarchical
|
|
109
|
-
* file operations.
|
|
110
|
-
*/
|
|
111
|
-
export default class FileObject extends FS {
|
|
112
|
-
/**
|
|
113
|
-
* Configuration for data parsing in the loadData method.
|
|
114
|
-
* Maps data type names to arrays of parser functions.
|
|
115
|
-
*/
|
|
116
|
-
static readonly dataLoaderConfig: DataLoaderConfig
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Create a new FileObject instance with intelligent path resolution.
|
|
120
|
-
*
|
|
121
|
-
* Constructs a FileObject from the provided filename and optional directory context.
|
|
122
|
-
* Automatically resolves relative paths to absolute paths, normalizes path separators
|
|
123
|
-
* for cross-platform compatibility, and establishes parent-child relationships with
|
|
124
|
-
* DirectoryObject instances.
|
|
125
|
-
*
|
|
126
|
-
* @param fileName - The file path, which can be relative or absolute. Empty strings
|
|
127
|
-
* and invalid paths will throw an error during construction.
|
|
128
|
-
* @param directory - Optional parent directory context. Can be a DirectoryObject instance,
|
|
129
|
-
* a string path that will be converted to a DirectoryObject, or null
|
|
130
|
-
* to use the current working directory as the parent.
|
|
131
|
-
*
|
|
132
|
-
* @throws {Error} When fileName is empty or contains invalid path characters
|
|
133
|
-
* @throws {Error} When the directory parameter is invalid or cannot be resolved
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* ```typescript
|
|
137
|
-
* // Simple file in current directory - most common usage
|
|
138
|
-
* const packageJson = new FileObject('package.json')
|
|
139
|
-
* const readme = new FileObject('./README.md') // Equivalent to above
|
|
140
|
-
*
|
|
141
|
-
* // File with string directory path
|
|
142
|
-
* const configFile = new FileObject('app.config.js', './config')
|
|
143
|
-
* const componentFile = new FileObject('Button.tsx', './src/components')
|
|
144
|
-
*
|
|
145
|
-
* // File with DirectoryObject parent for hierarchical operations
|
|
146
|
-
* const srcDir = new DirectoryObject('./src')
|
|
147
|
-
* const indexFile = new FileObject('index.js', srcDir)
|
|
148
|
-
* const utilsFile = new FileObject('utils/helpers.js', srcDir)
|
|
149
|
-
*
|
|
150
|
-
* // Absolute paths (directory parameter ignored)
|
|
151
|
-
* const systemFile = new FileObject('/etc/hosts')
|
|
152
|
-
* const winFile = new FileObject('C:\\Windows\\System32\\drivers\\etc\\hosts')
|
|
153
|
-
* ```
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```typescript
|
|
157
|
-
* // Complex directory structures and nested files
|
|
158
|
-
* const projectRoot = new DirectoryObject('./my-project')
|
|
159
|
-
* const srcDir = new DirectoryObject('src', projectRoot)
|
|
160
|
-
*
|
|
161
|
-
* // Create files within nested directory structure
|
|
162
|
-
* const mainApp = new FileObject('App.tsx', srcDir)
|
|
163
|
-
* const stylesheet = new FileObject('styles/main.css', srcDir)
|
|
164
|
-
* const testFile = new FileObject('__tests__/App.test.tsx', srcDir)
|
|
165
|
-
*
|
|
166
|
-
* console.log('Main app:', mainApp.path) // /absolute/path/my-project/src/App.tsx
|
|
167
|
-
* console.log('Stylesheet:', stylesheet.path) // /absolute/path/my-project/src/styles/main.css
|
|
168
|
-
* console.log('Test file:', testFile.path) // /absolute/path/my-project/src/__tests__/App.test.tsx
|
|
169
|
-
*
|
|
170
|
-
* // All files share the same parent directory reference
|
|
171
|
-
* console.log('Same parent?', mainApp.directory === srcDir) // true
|
|
172
|
-
* ```
|
|
173
|
-
*/
|
|
174
|
-
constructor(fileName: string, directory?: DirectoryObject | string | null)
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The original user-supplied path string used during construction.
|
|
178
|
-
*
|
|
179
|
-
* Preserves the exact path string passed to the constructor, including
|
|
180
|
-
* any relative path indicators (./, ../) or path separators. Useful
|
|
181
|
-
* for debugging, logging, or when you need to recreate the original
|
|
182
|
-
* user input.
|
|
183
|
-
*
|
|
184
|
-
* @example
|
|
185
|
-
* ```typescript
|
|
186
|
-
* const file1 = new FileObject('./config.json')
|
|
187
|
-
* const file2 = new FileObject('../package.json')
|
|
188
|
-
*
|
|
189
|
-
* console.log(file1.supplied) // './config.json'
|
|
190
|
-
* console.log(file2.supplied) // '../package.json'
|
|
191
|
-
* console.log(file1.path) // '/absolute/path/to/config.json'
|
|
192
|
-
* console.log(file2.path) // '/absolute/path/package.json'
|
|
193
|
-
* ```
|
|
194
|
-
*/
|
|
195
|
-
readonly supplied: string
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* The fully resolved absolute file path with normalized separators.
|
|
199
|
-
*
|
|
200
|
-
* Automatically resolved during construction using Node.js path utilities.
|
|
201
|
-
* Always uses forward slashes on Unix systems and backslashes on Windows.
|
|
202
|
-
* This is the canonical path that should be used for all file operations.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* ```typescript
|
|
206
|
-
* // Different inputs, same resolved path
|
|
207
|
-
* const file1 = new FileObject('./src/../config.json')
|
|
208
|
-
* const file2 = new FileObject('config.json')
|
|
209
|
-
*
|
|
210
|
-
* console.log(file1.path) // '/absolute/path/config.json'
|
|
211
|
-
* console.log(file2.path) // '/absolute/path/config.json'
|
|
212
|
-
* console.log(file1.path === file2.path) // true
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
readonly path: string
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* The file URI representation following RFC 3986 standard.
|
|
219
|
-
*
|
|
220
|
-
* Converts the absolute file path to a proper file:// URI scheme,
|
|
221
|
-
* handling URL encoding for special characters and proper formatting
|
|
222
|
-
* for cross-platform file URI access.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```typescript
|
|
226
|
-
* const file = new FileObject('./my project/config file.json')
|
|
227
|
-
* console.log(file.uri)
|
|
228
|
-
* // 'file:///absolute/path/my%20project/config%20file.json'
|
|
229
|
-
*
|
|
230
|
-
* // Can be used with URL constructor or file:// handlers
|
|
231
|
-
* const url = new URL(file.uri)
|
|
232
|
-
* console.log(url.pathname) // '/absolute/path/my project/config file.json'
|
|
233
|
-
* ```
|
|
234
|
-
*/
|
|
235
|
-
readonly uri: string
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* The complete filename including extension.
|
|
239
|
-
*
|
|
240
|
-
* Extracted from the resolved path using Node.js path utilities.
|
|
241
|
-
* Includes the file extension but excludes any directory components.
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* ```typescript
|
|
245
|
-
* const jsFile = new FileObject('./src/components/Button.tsx')
|
|
246
|
-
* const configFile = new FileObject('../.env.production')
|
|
247
|
-
*
|
|
248
|
-
* console.log(jsFile.name) // 'Button.tsx'
|
|
249
|
-
* console.log(configFile.name) // '.env.production'
|
|
250
|
-
* ```
|
|
251
|
-
*/
|
|
252
|
-
readonly name: string
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* The filename without its extension, suitable for module identification.
|
|
256
|
-
*
|
|
257
|
-
* Useful for generating module names, import statements, or when you need
|
|
258
|
-
* the base name without file type information. Handles complex extensions
|
|
259
|
-
* and dotfiles appropriately.
|
|
260
|
-
*/
|
|
261
|
-
readonly module: string
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* The file extension including the leading dot.
|
|
265
|
-
*
|
|
266
|
-
* Extracted using Node.js path utilities, always includes the dot prefix.
|
|
267
|
-
* Returns an empty string for files without extensions. Handles multiple
|
|
268
|
-
* extensions by returning only the last one.
|
|
269
|
-
*/
|
|
270
|
-
readonly extension: string
|
|
271
|
-
|
|
272
|
-
/** Type discriminator - always true for FileObject instances */
|
|
273
|
-
readonly isFile: true
|
|
274
|
-
|
|
275
|
-
/** Type discriminator - always false for FileObject instances */
|
|
276
|
-
readonly isDirectory: false
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* The parent DirectoryObject containing this file.
|
|
280
|
-
*
|
|
281
|
-
* Automatically created during FileObject construction based on the resolved
|
|
282
|
-
* file path. Provides access to parent directory operations and maintains
|
|
283
|
-
* the hierarchical relationship between files and directories.
|
|
284
|
-
*/
|
|
285
|
-
readonly directory: DirectoryObject
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Promise that resolves to whether the file exists on the filesystem.
|
|
289
|
-
*
|
|
290
|
-
* Performs an asynchronous filesystem check to determine file existence.
|
|
291
|
-
* The Promise will resolve to true if the file exists and is accessible,
|
|
292
|
-
* false otherwise. Always await this property before using the result.
|
|
293
|
-
*/
|
|
294
|
-
readonly exists: Promise<boolean>
|
|
295
|
-
|
|
296
|
-
/** Returns a JSON representation of the FileObject */
|
|
297
|
-
toJSON(): {
|
|
298
|
-
supplied: string
|
|
299
|
-
path: string
|
|
300
|
-
uri: string
|
|
301
|
-
name: string
|
|
302
|
-
module: string
|
|
303
|
-
extension: string
|
|
304
|
-
isFile: boolean
|
|
305
|
-
isDirectory: boolean
|
|
306
|
-
directory: string | null
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/** Check if a file can be read */
|
|
310
|
-
canRead(): Promise<boolean>
|
|
311
|
-
|
|
312
|
-
/** Check if a file can be written */
|
|
313
|
-
canWrite(): Promise<boolean>
|
|
314
|
-
|
|
315
|
-
/** Get the size of a file */
|
|
316
|
-
size(): Promise<number | null>
|
|
317
|
-
|
|
318
|
-
/** Get the last modification time of a file */
|
|
319
|
-
modified(): Promise<Date | null>
|
|
320
|
-
|
|
321
|
-
/** Read the content of a file */
|
|
322
|
-
read(encoding?: string): Promise<string>
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Write content to a file asynchronously.
|
|
326
|
-
* Validates that the parent directory exists before writing.
|
|
327
|
-
*
|
|
328
|
-
* @param content - The content to write
|
|
329
|
-
* @param encoding - The encoding in which to write (default: "utf8")
|
|
330
|
-
* @throws {Sass} If the file path is invalid or the parent directory doesn't exist
|
|
331
|
-
*
|
|
332
|
-
* @example
|
|
333
|
-
* ```typescript
|
|
334
|
-
* const file = new FileObject('./output/data.json')
|
|
335
|
-
* await file.write(JSON.stringify({key: 'value'}))
|
|
336
|
-
*
|
|
337
|
-
* // With custom encoding
|
|
338
|
-
* await file.write('content', 'utf16le')
|
|
339
|
-
* ```
|
|
340
|
-
*/
|
|
341
|
-
write(content: string, encoding?: string): Promise<void>
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Load and parse data from JSON5 or YAML file.
|
|
345
|
-
* Attempts to parse content as JSON5 first, then falls back to YAML if type is "any".
|
|
346
|
-
*
|
|
347
|
-
* @param type - The expected data format: "json", "json5", "yaml", or "any" (default: "any")
|
|
348
|
-
* @param encoding - The file encoding (default: "utf8")
|
|
349
|
-
* @returns The parsed data object
|
|
350
|
-
* @throws {Sass} If the content cannot be parsed or type is unsupported
|
|
351
|
-
*
|
|
352
|
-
* @example
|
|
353
|
-
* ```typescript
|
|
354
|
-
* // Load JSON5 config
|
|
355
|
-
* const config = await configFile.loadData('json5')
|
|
356
|
-
*
|
|
357
|
-
* // Auto-detect format (tries JSON5, then YAML)
|
|
358
|
-
* const data = await dataFile.loadData('any')
|
|
359
|
-
*
|
|
360
|
-
* // Load YAML explicitly
|
|
361
|
-
* const yaml = await yamlFile.loadData('yaml')
|
|
362
|
-
* ```
|
|
363
|
-
*/
|
|
364
|
-
loadData(type?: 'json' | 'json5' | 'yaml' | 'any', encoding?: string): Promise<unknown>
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Dynamically import the file using the resolved file URI.
|
|
368
|
-
*
|
|
369
|
-
* Uses Node.js' native dynamic `import()` under the hood, allowing consumers to load
|
|
370
|
-
* ESM modules from disk with full path resolution handled by FileObject. The method
|
|
371
|
-
* verifies the file exists before attempting the import to provide clearer error
|
|
372
|
-
* messaging and prevent low-level loader failures.
|
|
373
|
-
*
|
|
374
|
-
* @typeParam TModule - Expected module shape. Defaults to a loose record to help with
|
|
375
|
-
* module namespace typing.
|
|
376
|
-
*
|
|
377
|
-
* @returns The imported module namespace object.
|
|
378
|
-
*
|
|
379
|
-
* @throws {Error} When the file does not exist or the path cannot be converted to a URI.
|
|
380
|
-
*
|
|
381
|
-
* @example
|
|
382
|
-
* ```typescript
|
|
383
|
-
* const configModule = await file.import<{ default: Config }>()
|
|
384
|
-
* const config = configModule.default
|
|
385
|
-
* ```
|
|
386
|
-
*/
|
|
387
|
-
import<TModule = Record<string, unknown>>(): Promise<TModule>
|
|
388
|
-
}
|