chore: Change back to the function-style operation

We lose the more normal looking export in favor of the `return` statement already traditional to this action, and thus can handle await statements without an ESM conversion.

This is a separate commit from the former because I think the next major version of this action should switch to ESM, revert this commit, and use the more standard export notation in all supported languages.
This commit is contained in:
Ricky C 2024-07-05 19:03:21 -07:00
parent d3f9a3b3fb
commit f27b40e6b7
4 changed files with 28 additions and 51 deletions

View file

@ -11,12 +11,12 @@ const scripts: Record<SupportedLanguage, string> = {
[SupportedLanguage.cts]: `
const FS = require('node:fs') // Proof that we are in CommonJS.
let a: string // Proof that we are in TypeScript.
exports = foo // Proof that we executed correctly.
return foo // Proof that we executed correctly.
`,
[SupportedLanguage.mts]: `
import FS from 'node:fs' // Proof that we are in an ES Module.
let a: string // Proof that we are in TypeScript.
export default foo // Proof that we executed correctly.
return foo // Proof that we executed correctly.
`
}
@ -108,7 +108,7 @@ describe(interpretScript.name, () => {
{require} as any,
`
const {test} = require('../test/requireable')
exports = test()
return test()
`
)
return expect(result()).resolves.toEqual('hello')
@ -165,13 +165,12 @@ describe(interpretScript.name, () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`export default {a: 'b'}`
`return {a: 'b'}`
)
return expect(result()).resolves.toEqual({a: 'b'})
})
test.skip(`a script that uses a root level await`, async () => {
// Will not work until we can actually run in ESM. Current code is transpiling the mts to cjs, so we don't get root level awaits yet.
test(`a script that uses a root level await`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
@ -180,14 +179,13 @@ describe(interpretScript.name, () => {
return expect(result()).resolves
})
test.skip(`a script imports a script from disk`, async () => {
// Will not work until we can actually run in ESM. Current code is transpiling the mts to cjs, so we don't get root level awaits yet.
test(`a script imports a script from disk`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{require} as any,
`
const {test} = await import('../test/importable')
export default test()
return test()
`
)
return expect(result()).resolves.toEqual('hello')