|
1 | | -import * as fs from 'fs'; |
2 | | -import * as path from 'path'; |
3 | | -import { fileURLToPath } from 'url'; |
| 1 | +import * as fs from 'fs' |
| 2 | +import * as path from 'path' |
| 3 | +import { fileURLToPath } from 'url' |
| 4 | +import { dynamicImport } from './dynamicImport.js' |
4 | 5 |
|
5 | 6 | // Helper to get __dirname equivalent in ES Modules |
6 | | -const __filename = fileURLToPath(import.meta.url); |
7 | | -const __dirname = path.dirname(__filename); |
| 7 | +const __filename = fileURLToPath(import.meta.url) |
| 8 | +const __dirname = path.dirname(__filename) |
8 | 9 |
|
9 | | -const plugins: any[] = []; |
10 | | -let pluginsLoaded = false; |
| 10 | +const plugins: any[] = [] |
| 11 | +let pluginsLoaded = false |
11 | 12 |
|
12 | 13 |
|
13 | 14 | export async function loadPlugins() { |
| 15 | + console.log('loading plugins') |
| 16 | + |
14 | 17 | try { |
15 | 18 | if (!pluginsLoaded) { |
16 | | - await loadBuiltInPlugins(); |
17 | | - await loadCustomPlugins(); |
| 19 | + await loadBuiltInPlugins() |
| 20 | + await loadCustomPlugins() |
18 | 21 | } |
19 | 22 | } catch (e) { |
20 | | - plugins.length = 0; |
21 | | - console.log('There was an error while loading plugins.'); |
22 | | - console.log('Clearing all plugins and aborting custom plugin scans.'); |
23 | | - console.log('Please check the logs for hints as to what may have gone wrong.'); |
| 23 | + plugins.length = 0 |
| 24 | + console.log(abortError) |
24 | 25 | } finally { |
25 | | - pluginsLoaded = true; |
26 | | - return plugins; |
| 26 | + pluginsLoaded = true |
| 27 | + return plugins |
27 | 28 | } |
28 | 29 | } |
29 | 30 |
|
30 | | -async function loadBuiltInPlugins() { |
31 | | - console.log('Loading built-in plugins'); |
| 31 | +export const abortError = [ |
| 32 | + 'There was an error while loading plugins.', |
| 33 | + 'Clearing all plugins and aborting custom plugin scans.', |
| 34 | + 'Please check the logs for hints as to what may have gone wrong.' |
| 35 | +].join('\n') |
| 36 | + |
| 37 | + |
| 38 | +export function clearCache() { |
| 39 | + console.log('clearing plugin cache') |
| 40 | + pluginsLoaded = false |
| 41 | + plugins.length = 0 |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// exported for mocking/testing. not for actual use |
| 46 | +export async function loadBuiltInPlugins() { |
| 47 | + console.log('Loading built-in plugins') |
32 | 48 |
|
33 | | - const pluginsPath = '../../../scanner-plugins/'; |
| 49 | + const pluginsPath = '../../../scanner-plugins/' |
34 | 50 | await loadPluginsFromPath({ |
35 | 51 | readPath: path.join(__dirname, pluginsPath), |
36 | 52 | importPath: pluginsPath, |
37 | | - }); |
| 53 | + }) |
38 | 54 | } |
39 | 55 |
|
40 | | -async function loadCustomPlugins() { |
41 | | - console.log('Loading custom plugins'); |
| 56 | +// exported for mocking/testing. not for actual use |
| 57 | +export async function loadCustomPlugins() { |
| 58 | + console.log('Loading custom plugins') |
42 | 59 |
|
43 | | - const pluginsPath = process.cwd() + '/.github/scanner-plugins/'; |
| 60 | + const pluginsPath = process.cwd() + '/.github/scanner-plugins/' |
44 | 61 | await loadPluginsFromPath({ |
45 | 62 | readPath: pluginsPath, |
46 | 63 | importPath: pluginsPath |
47 | | - }); |
| 64 | + }) |
48 | 65 | } |
49 | 66 |
|
50 | | -async function loadPluginsFromPath({ readPath, importPath }: { readPath: string, importPath: string }) { |
| 67 | +// exported for mocking/testing. not for actual use |
| 68 | +export async function loadPluginsFromPath({ readPath, importPath }: { readPath: string, importPath: string }) { |
51 | 69 | try { |
52 | | - const res = fs.readdirSync(readPath); |
| 70 | + const res = fs.readdirSync(readPath) |
53 | 71 | for (const pluginFolder of res) { |
54 | | - console.log('Found plugin: ', pluginFolder); |
| 72 | + console.log('Found plugin: ', pluginFolder) |
55 | 73 | // @ts-ignore |
56 | | - plugins.push(await import(path.join(importPath, pluginFolder, '/index.js'))); |
| 74 | + plugins.push(await dynamicImport(path.join(importPath, pluginFolder, '/index.js'))) |
57 | 75 | } |
58 | 76 | } catch (e) { |
59 | 77 | // - log errors here for granular info |
60 | | - console.log('error: '); |
61 | | - console.log(e); |
| 78 | + console.log('error: ') |
| 79 | + console.log(e) |
62 | 80 | // - throw error to handle aborting the plugin scans |
63 | | - throw(e); |
| 81 | + throw(e) |
64 | 82 | } |
65 | 83 | } |
0 commit comments