-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpluginFileLoaders.ts
More file actions
49 lines (43 loc) · 1.77 KB
/
pluginFileLoaders.ts
File metadata and controls
49 lines (43 loc) · 1.77 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
import * as fs from 'fs'
import * as path from 'path'
import * as esbuild from 'esbuild'
import {dynamicImport} from '../dynamicImport.js'
import * as core from '@actions/core'
import { Plugin } from './types.js'
// - these functions had to be moved into a separate file
// because vitest will not mock the implementation of functions
// that are in the same file as the function under test.
// - https://vitest.dev/guide/mocking/modules.html#mocking-modules-pitfalls
export async function loadPluginViaTsFile(pluginFolderPath: string): Promise<Plugin | undefined> {
const pluginEntryPath = path.join(pluginFolderPath, 'index.ts')
if (!fs.existsSync(pluginEntryPath)) {
core.info(`No index.ts found for plugin at path: ${pluginFolderPath}`)
return
}
core.info(`index.ts found for plugin at path: ${pluginFolderPath}`)
const esbuildResult = await esbuild.build({
entryPoints: [pluginEntryPath],
write: false,
bundle: true,
format: 'esm',
platform: 'node',
target: 'node24',
sourcemap: 'inline',
})
const outputFileContents = esbuildResult.outputFiles[0]?.text
if (!outputFileContents) {
core.info(`esbuild produced no output for plugin: ${pluginEntryPath}`)
return
}
const base64CompiledPlugin = Buffer.from(outputFileContents).toString('base64')
return dynamicImport(`data:text/javascript;base64,${base64CompiledPlugin}`)
}
export async function loadPluginViaJsFile(pluginFolderPath: string): Promise<Plugin | undefined> {
const pluginEntryPath = path.join(pluginFolderPath, 'index.js')
if (!fs.existsSync(pluginEntryPath)) {
core.info(`No index.js found for plugin at path: ${pluginFolderPath}`)
return
}
core.info(`index.js found for plugin at path: ${pluginFolderPath}`)
return dynamicImport(pluginEntryPath)
}