import chalk from 'chalk'; import fg from 'fast-glob'; import { normalize } from 'path'; import { TakeoffCommand } from 'commands'; import { TakeoffCmdParameters } from 'takeoff'; import { ExitCode } from 'task'; /** * Load plugins from the basePath. Will attempt to load both Typescript and JavaScript plugins * Returns a map of plugins with the key as the command and value as the plugin object */ export = async (cwdList: string[], params: TakeoffCmdParameters): Promise> => { const commandMap: Map = new Map(); for (const cwd of cwdList) { const commandPath = normalize(cwd); let commandPaths = []; try { commandPaths = await fg([`**/*.js`, `**/*.ts`], { cwd, ignore: [`**/*.spec.js`, `**/*spec.js`, `**/*.d.ts`], }); } catch (e) { throw e; } commandPaths.forEach((path: string) => { const requirePath = `${commandPath}/${path}`; try { const plugin: TakeoffCommand = require(requirePath)(params); commandMap.set(`${plugin.group}:${plugin.command}`, plugin); } catch (e) { params.exitWithMessage({ code: ExitCode.Error, extra: e, fail: `Unable to load command ${chalk.cyan(`${requirePath}`)}`, }); } }); } return commandMap; };