docorm
Version:
Persistence layer with ORM features for JSON documents
60 lines • 2.72 kB
TypeScript
/**
* Utilities for dynamically importing JavaScript modules from a directory
*
* @module lib/import-dir.ts
*/
interface DirectoryImportOptions {
/** A flag indicating whether to import modules recursively from subdirectories. */
recurse: boolean;
/** An array of filename extensions to import. */
extensions: string[];
/** Type assertion (for importing JSON files). */
assertType?: 'json';
}
interface DirectoryImportCatalogue {
[submoduleName: string]: string | DirectoryImportCatalogue;
}
/**
* Catalogue the JavaScript modules contained in a directory.
*
* This is typically used with an absolute path, as in the following example:
*
* <code>
* import path, {dirname} from 'path'
* import {fileURLToPath} from 'url'
* import {importDirectory} from '../lib/import-dir.js'
* const __filename = fileURLToPath(import.meta.url)
* const __dirname = dirname(__filename)
* const availableModules = await catalogueDirectoryImports(path.join(__dirname, '../some/directory'), {recurse: true})
* </code>
*
* @param directoryPath The path of the directory to catalogue.
* @param options Import options. The assertType property must be set when importing JSON files.
* @return An object whose property keys are the module names (filenames without extensions) and property values are the
* paths of the module files. Paths begin with directoryPath, so they are absolute or relative according to whether
* directoryPath is absolute or relative.
*/
export declare function catalogueDirectoryImports(directoryPath: string, options?: DirectoryImportOptions): Promise<DirectoryImportCatalogue>;
/**
* Import all JavaScript modules contained in a directory.
*
* This is typically used with an absolute path, as in the following example:
*
* <code>
* import path, {dirname} from 'path'
* import {fileURLToPath} from 'url'
* import {importDirectory} from '../lib/import-dir.js'
* const __filename = fileURLToPath(import.meta.url)
* const __dirname = dirname(__filename)
* const importedModules = await importDirectory(path.join(__dirname, '../some/directory'), {recurse: true})
* </code>
*
* @param directoryPath - The path of the directory to import.
* @param options - Import options, including an import assert
* @return - An object whose property keys are the module names (filenames without extensions) and property
* values are the default exports of each module. If the import is recursive, there is also a key for each
* subdirectory, whose value is another object of the same sort.
*/
export declare function importDirectory(directoryPath: string, options?: DirectoryImportOptions): Promise<object>;
export {};
//# sourceMappingURL=import-dir.d.ts.map