Skip to content

Commit 4d62c1a

Browse files
committed
refactor copilot code
- change reflow to .ts file - add temporary test scan for testing js files
1 parent 3646661 commit 4d62c1a

File tree

4 files changed

+41
-32
lines changed

4 files changed

+41
-32
lines changed

.github/actions/find/src/dynamicImport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {pathToFileURL} from 'url'
1+
import { pathToFileURL } from 'url'
22

33
// - this exists because it looks like there's no straight-forward
44
// way to mock the dynamic import function, so mocking this instead
@@ -19,6 +19,9 @@ import {pathToFileURL} from 'url'
1919
//
2020
// - so this looks like a reasonable approach
2121
export function dynamicImport(path: string) {
22+
// - this check is for non-file imports.
23+
// - this can be encountered when using esbuild to compile TS plugin files
24+
// at run-time
2225
if (path.startsWith('data:') || path.startsWith('file:')) {
2326
return import(path)
2427
}

.github/actions/find/src/pluginManager.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as fs from 'fs'
22
import * as path from 'path'
3-
import {fileURLToPath} from 'url'
3+
import { fileURLToPath } from 'url'
44
import * 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'
77
import playwright from 'playwright'
88
import * 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+
165166
type 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
}

.github/scanner-plugins/reflow-scan/index.js renamed to .github/scanner-plugins/reflow-scan/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export default async function reflowScan({page, addFinding} = {}) {
1+
export default async function reflowScan({ page, addFinding } = {}) {
22
const originalViewport = page.viewportSize()
33
const url = page.url()
44
// Check for horizontal scrolling at 320x256 viewport
55
try {
6-
await page.setViewportSize({width: 320, height: 256})
6+
await page.setViewportSize({ width: 320, height: 256 })
77
const scrollWidth = await page.evaluate(() => document.documentElement.scrollWidth)
88
const clientWidth = await page.evaluate(() => document.documentElement.clientWidth)
99

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default async function reflowScan({ page, addFinding } = {}) {
2+
console.log('testing js file import')
3+
}
4+
5+
export const name = 'temp-test-scan'

0 commit comments

Comments
 (0)