-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathpluginManager.ts
More file actions
88 lines (76 loc) · 2.41 KB
/
pluginManager.ts
File metadata and controls
88 lines (76 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import * as fs from 'fs'
import * as path from 'path'
import {fileURLToPath} from 'url'
import {dynamicImport} from './dynamicImport.js'
import type {Finding} from './types.d.js'
import playwright from 'playwright'
// Helper to get __dirname equivalent in ES Modules
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
export type Plugin = {
name: string
default: (options: {page: playwright.Page; addFinding: (findingData: Finding) => void; url: string}) => Promise<void>
}
const plugins: Plugin[] = []
let pluginsLoaded = false
export async function loadPlugins() {
console.log('loading plugins')
try {
if (!pluginsLoaded) {
await loadBuiltInPlugins()
await loadCustomPlugins()
}
} catch {
plugins.length = 0
console.log(abortError)
} finally {
pluginsLoaded = true
return plugins
}
}
export const abortError = `
There was an error while loading plugins.
Clearing all plugins and aborting custom plugin scans.
Please check the logs for hints as to what may have gone wrong.
`
export function clearCache() {
pluginsLoaded = false
plugins.length = 0
}
// exported for mocking/testing. not for actual use
export async function loadBuiltInPlugins() {
console.log('Loading built-in plugins')
const pluginsPath = '../../../scanner-plugins/'
await loadPluginsFromPath({
readPath: path.join(__dirname, pluginsPath),
importPath: pluginsPath,
})
}
// exported for mocking/testing. not for actual use
export async function loadCustomPlugins() {
console.log('Loading custom plugins')
const pluginsPath = process.cwd() + '/.github/scanner-plugins/'
await loadPluginsFromPath({
readPath: pluginsPath,
importPath: pluginsPath,
})
}
// exported for mocking/testing. not for actual use
export async function loadPluginsFromPath({readPath, importPath}: {readPath: string; importPath: string}) {
try {
const res = fs.readdirSync(readPath)
for (const pluginFolder of res) {
const pluginFolderPath = path.join(importPath, pluginFolder)
if (fs.lstatSync(pluginFolderPath).isDirectory()) {
console.log('Found plugin: ', pluginFolder)
plugins.push(await dynamicImport(path.join(importPath, pluginFolder, '/index.js')))
}
}
} catch (e) {
// - log errors here for granular info
console.log('error: ')
console.log(e)
// - throw error to handle aborting the plugin scans
throw e
}
}