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

Function loadExtensionFromFile

src/loaders/extension-loader.ts:13–47  ·  view source on GitHub ↗
(file: string, _options = {})

Source from the content-addressed store, hash-verified

11 * @param options Options, such as
12 */
13export function loadExtensionFromFile(file: string, _options = {}): Extension {
14 const extension = new Extension()
15
16 // sanity check the input
17 if (strings.isBlank(file)) {
18 throw new Error(`Error: couldn't load extension (file is blank): ${file}`)
19 }
20
21 extension.file = file
22
23 // not a file?
24 if (filesystem.isNotFile(file)) {
25 throw new Error(`Error: couldn't load command (not a file): ${file}`)
26 }
27
28 // default is the name of the file without the extension
29 extension.name = (filesystem.inspect(file) as any).name.split('.')[0]
30
31 // require in the module -- best chance to bomb is here
32 let extensionModule = loadModule(file)
33
34 // if they use `export default` rather than `module.exports =`, we extract that
35 extensionModule = extensionModule.default || extensionModule
36
37 // should we try the default export?
38 const valid = extensionModule && typeof extensionModule === 'function'
39
40 if (valid) {
41 extension.setup = (toolbox: EmptyToolbox) => extensionModule(toolbox as Toolbox)
42 } else {
43 throw new Error(`Error: couldn't load ${extension.name}. Expected a function, got ${extensionModule}.`)
44 }
45
46 return extension
47}

Callers 2

loadPluginFromDirectoryFunction · 0.90

Calls 3

loadModuleFunction · 0.90
isBlankMethod · 0.80
isNotFileMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…