import { autoService, singleton, location, type ServiceProperties, } from 'knifecycle'; import { printStackTrace, YError } from 'yerror'; import { noop } from '../utils/utils.js'; import { type LogService } from './log.js'; export type ImporterService = (path: string) => Promise; /** * Allow to import ES modules. * @param {string} path * The module path * @return {Promise} * A promise of an imported module. */ async function initImporter({ log = noop, }: { log: LogService; }): Promise> { const importer = async (path: string) => { log('debug', `🛂 - Dynamic import of "${path}".`); try { return await import(path); } catch (err) { log('debug', `⚠️ - Got a runtime import error for "${path}" !`); log('debug-stack', printStackTrace(err as Error)); throw YError.wrap(err as Error, 'E_RUNTIME_IMPORT_FAILURE', [path]); } }; log('debug', '🛂 - Initializing the importer!'); return importer; } /* Architecture Note #1.6: Importer The `importer` service is just proxying [`import` ](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Operators/import) in a simply mockable manner. */ export default location( singleton(autoService(initImporter)), import.meta.url, ) as unknown as ServiceProperties & typeof initImporter;