MCPcopy
hub / github.com/infinitered/gluegun / loadPluginFromDirectory

Function loadPluginFromDirectory

src/loaders/plugin-loader.ts:17–96  ·  view source on GitHub ↗
(directory: string, options: Options = {})

Source from the content-addressed store, hash-verified

15 * @param options Additional options to customize the loading process.
16 */
17export 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

Callers 2

addPluginMethod · 0.90

Calls 6

loadCommandFromFileFunction · 0.90
loadExtensionFromFileFunction · 0.90
loadConfigFunction · 0.90
isBlankMethod · 0.80
isNotDirectoryMethod · 0.80
existsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…