(directory: string, options: Options = {})
| 15 | * @param options Additional options to customize the loading process. |
| 16 | */ |
| 17 | export function loadPluginFromDirectory(directory: string, options: Options = {}): Plugin { |
| 18 | const plugin = new Plugin() |
| 19 | |
| 20 | const { |
| 21 | brand = 'gluegun', |
| 22 | commandFilePattern = [`*.{js,ts}`, `!*.test.{js,ts}`], |
| 23 | extensionFilePattern = [`*.{js,ts}`, `!*.test.{js,ts}`], |
| 24 | hidden = false, |
| 25 | name, |
| 26 | } = options |
| 27 | |
| 28 | plugin.hidden = Boolean(options.hidden) |
| 29 | |
| 30 | if (!strings.isBlank(name)) { |
| 31 | plugin.name = name |
| 32 | } |
| 33 | |
| 34 | // directory check |
| 35 | if (filesystem.isNotDirectory(directory)) { |
| 36 | throw new Error(`Error: couldn't load plugin (not a directory): ${directory}`) |
| 37 | } |
| 38 | |
| 39 | plugin.directory = directory |
| 40 | |
| 41 | // the directory is the default name (unless we were told what it was) |
| 42 | if (strings.isBlank(name)) { |
| 43 | plugin.name = jetpack.inspect(directory).name |
| 44 | } |
| 45 | |
| 46 | const jetpackPlugin = jetpack.cwd(plugin.directory) |
| 47 | |
| 48 | // load any default commands passed in |
| 49 | plugin.commands = (options.preloadedCommands || []).map(loadCommandFromPreload) |
| 50 | |
| 51 | // load the commands found in the commands sub-directory |
| 52 | const commandSearchDirectories = ['commands', 'build/commands'] |
| 53 | commandSearchDirectories.forEach((dir) => { |
| 54 | if (jetpackPlugin.exists(dir) === 'dir') { |
| 55 | const commands = jetpackPlugin.cwd(dir).find({ matching: commandFilePattern, recursive: true }) |
| 56 | |
| 57 | plugin.commands = plugin.commands.concat( |
| 58 | commands.map((file) => loadCommandFromFile(path.join(directory, dir, file))), |
| 59 | ) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | // load the extensions found in the extensions sub-directory |
| 64 | const extensionSearchDirectories = ['extensions', 'build/extensions'] |
| 65 | extensionSearchDirectories.forEach((dir) => { |
| 66 | if (jetpackPlugin.exists(dir) === 'dir') { |
| 67 | const extensions = jetpackPlugin.cwd(dir).find({ matching: extensionFilePattern, recursive: false }) |
| 68 | |
| 69 | plugin.extensions = plugin.extensions.concat( |
| 70 | extensions.map((file) => loadExtensionFromFile(`${directory}/${dir}/${file}`)), |
| 71 | ) |
| 72 | } |
| 73 | }) |
| 74 |
no test coverage detected
searching dependent graphs…