11import * as fs from 'fs'
22import * as path from 'path'
3- import { fileURLToPath } from 'url'
3+ import { fileURLToPath } from 'url'
44import * as esbuild from 'esbuild'
5- import { dynamicImport } from './dynamicImport.js'
6- import type { Finding } from './types.d.js'
5+ import { dynamicImport } from './dynamicImport.js'
6+ import type { Finding } from './types.d.js'
77import playwright from 'playwright'
88import * as core from '@actions/core'
99
@@ -60,7 +60,7 @@ export async function loadBuiltInPlugins() {
6060 core . info ( 'Loading built-in plugins' )
6161
6262 const pluginsPath = path . join ( __dirname , '../../../scanner-plugins/' )
63- await loadPluginsFromPath ( { pluginsPath} )
63+ await loadPluginsFromPath ( { pluginsPath } )
6464}
6565
6666// exported for mocking/testing. not for actual use
@@ -80,7 +80,7 @@ export async function loadCustomPlugins() {
8080 return
8181 }
8282
83- await loadPluginsFromPath ( { pluginsPath, skipBuiltInPlugins : BUILT_IN_PLUGINS } )
83+ await loadPluginsFromPath ( { pluginsPath, skipBuiltInPlugins : BUILT_IN_PLUGINS } )
8484}
8585
8686// exported for mocking/testing. not for actual use
@@ -97,15 +97,17 @@ export async function loadPluginsFromPath({
9797 const pluginFolderPath = path . join ( pluginsPath , pluginFolder )
9898
9999 if ( fs . existsSync ( pluginFolderPath ) && fs . lstatSync ( pluginFolderPath ) . isDirectory ( ) ) {
100- const pluginEntryPath = resolvePluginEntryPath ( pluginFolderPath )
100+ let plugin = await loadPluginViaTsFile ( pluginFolderPath )
101101
102- if ( ! pluginEntryPath ) {
102+ if ( ! plugin ) {
103+ plugin = await loadPluginViaJsFile ( pluginFolderPath )
104+ }
105+
106+ if ( ! plugin ) {
103107 core . info ( `Skipping plugin folder without index.ts or index.js: ${ pluginFolder } ` )
104108 continue
105109 }
106110
107- const plugin = await loadPluginModule ( pluginEntryPath )
108-
109111 if ( skipBuiltInPlugins ?. includes ( plugin . name ) ) {
110112 core . info ( `Skipping built-in plugin: ${ plugin . name } ` )
111113 continue
@@ -124,23 +126,11 @@ export async function loadPluginsFromPath({
124126 }
125127}
126128
127- function resolvePluginEntryPath ( pluginFolderPath : string ) {
128- const typescriptPluginPath = path . join ( pluginFolderPath , 'index.ts' )
129- if ( fs . existsSync ( typescriptPluginPath ) ) {
130- return typescriptPluginPath
131- }
132-
133- const javascriptPluginPath = path . join ( pluginFolderPath , 'index.js' )
134- if ( fs . existsSync ( javascriptPluginPath ) ) {
135- return javascriptPluginPath
136- }
137-
138- return null
139- }
140-
141- async function loadPluginModule ( pluginEntryPath : string ) {
142- if ( pluginEntryPath . endsWith ( '.js' ) ) {
143- return dynamicImport ( pluginEntryPath )
129+ async function loadPluginViaTsFile ( pluginFolderPath : string ) {
130+ const pluginEntryPath = path . join ( pluginFolderPath , 'index.ts' )
131+ if ( ! fs . existsSync ( pluginEntryPath ) ) {
132+ core . info ( `No index.ts found for plugin at path: ${ pluginFolderPath } ` )
133+ return
144134 }
145135
146136 const esbuildResult = await esbuild . build ( {
@@ -155,16 +145,27 @@ async function loadPluginModule(pluginEntryPath: string) {
155145
156146 const outputFileContents = esbuildResult . outputFiles [ 0 ] ?. text
157147 if ( ! outputFileContents ) {
158- throw new Error ( `esbuild produced no output for plugin: ${ pluginEntryPath } ` )
148+ core . info ( `esbuild produced no output for plugin: ${ pluginEntryPath } ` )
149+ return
159150 }
160151
161152 const base64CompiledPlugin = Buffer . from ( outputFileContents ) . toString ( 'base64' )
162153 return dynamicImport ( `data:text/javascript;base64,${ base64CompiledPlugin } ` )
163154}
164155
156+ async function loadPluginViaJsFile ( pluginFolderPath : string ) {
157+ const pluginEntryPath = path . join ( pluginFolderPath , 'index.js' )
158+ if ( ! fs . existsSync ( pluginEntryPath ) ) {
159+ core . info ( `No index.js found for plugin at path: ${ pluginFolderPath } ` )
160+ return
161+ }
162+
163+ return dynamicImport ( pluginEntryPath )
164+ }
165+
165166type InvokePluginParams = PluginDefaultParams & {
166167 plugin : Plugin
167168}
168- export function invokePlugin ( { plugin, page, addFinding} : InvokePluginParams ) {
169- return plugin . default ( { page, addFinding} )
169+ export function invokePlugin ( { plugin, page, addFinding } : InvokePluginParams ) {
170+ return plugin . default ( { page, addFinding } )
170171}
0 commit comments