@gesslar/toolkit 0.2.7 → 0.2.8
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 +1 -1
- package/src/lib/FileObject.js +20 -3
- package/src/types/FileObject.d.ts +23 -0
package/package.json
CHANGED
package/src/lib/FileObject.js
CHANGED
|
@@ -353,12 +353,12 @@ export default class FileObject extends FS {
|
|
|
353
353
|
async read(encoding="utf8") {
|
|
354
354
|
const filePath = this.path
|
|
355
355
|
|
|
356
|
-
if(!(await this.exists))
|
|
357
|
-
throw Sass.new(`No such file '${filePath}'`)
|
|
358
|
-
|
|
359
356
|
if(!filePath)
|
|
360
357
|
throw Sass.new("No absolute path in file map")
|
|
361
358
|
|
|
359
|
+
if(!(await this.exists))
|
|
360
|
+
throw Sass.new(`No such file '${filePath}'`)
|
|
361
|
+
|
|
362
362
|
return await fs.readFile(filePath, encoding)
|
|
363
363
|
}
|
|
364
364
|
|
|
@@ -409,4 +409,21 @@ export default class FileObject extends FS {
|
|
|
409
409
|
|
|
410
410
|
throw Sass.new(`Content is neither valid JSON5 nor valid YAML:\n'${this.path}'`)
|
|
411
411
|
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Loads a file as a module and returns it.
|
|
415
|
+
*
|
|
416
|
+
* @returns {Promise<object>} The file contents as a module.
|
|
417
|
+
*/
|
|
418
|
+
async import() {
|
|
419
|
+
const fileUri = this.uri
|
|
420
|
+
|
|
421
|
+
if(!fileUri)
|
|
422
|
+
throw Sass.new("No URI in file map")
|
|
423
|
+
|
|
424
|
+
if(!(await this.exists))
|
|
425
|
+
throw Sass.new(`No such file '${fileUri}'`)
|
|
426
|
+
|
|
427
|
+
return await import(fileUri)
|
|
428
|
+
}
|
|
412
429
|
}
|
|
@@ -326,4 +326,27 @@ export default class FileObject extends FS {
|
|
|
326
326
|
|
|
327
327
|
/** Load an object from JSON5 or YAML file with type specification */
|
|
328
328
|
loadData(type?: 'json' | 'json5' | 'yaml' | 'any', encoding?: string): Promise<unknown>
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Dynamically import the file using the resolved file URI.
|
|
332
|
+
*
|
|
333
|
+
* Uses Node.js' native dynamic `import()` under the hood, allowing consumers to load
|
|
334
|
+
* ESM modules from disk with full path resolution handled by FileObject. The method
|
|
335
|
+
* verifies the file exists before attempting the import to provide clearer error
|
|
336
|
+
* messaging and prevent low-level loader failures.
|
|
337
|
+
*
|
|
338
|
+
* @typeParam TModule - Expected module shape. Defaults to a loose record to help with
|
|
339
|
+
* module namespace typing.
|
|
340
|
+
*
|
|
341
|
+
* @returns The imported module namespace object.
|
|
342
|
+
*
|
|
343
|
+
* @throws {Error} When the file does not exist or the path cannot be converted to a URI.
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* const configModule = await file.import<{ default: Config }>()
|
|
348
|
+
* const config = configModule.default
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
import<TModule = Record<string, unknown>>(): Promise<TModule>
|
|
329
352
|
}
|