(file: string, _options = {})
| 11 | * @param options Options, such as |
| 12 | */ |
| 13 | export 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 | } |
no test coverage detected
searching dependent graphs…