diff --git a/__test__/interpret-script.test.ts b/__test__/interpret-script.test.ts index 54d3085..3bdcc15 100644 --- a/__test__/interpret-script.test.ts +++ b/__test__/interpret-script.test.ts @@ -11,12 +11,12 @@ const scripts: Record = { [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') diff --git a/dist/index.js b/dist/index.js index 2658010..597e511 100644 --- a/dist/index.js +++ b/dist/index.js @@ -213408,21 +213408,23 @@ var io = __nccwpck_require__(7436); var dist_node = __nccwpck_require__(8883); // EXTERNAL MODULE: ./node_modules/@octokit/plugin-retry/dist-node/index.js var plugin_retry_dist_node = __nccwpck_require__(6298); -;// CONCATENATED MODULE: external "node:vm" -const external_node_vm_namespaceObject = require("node:vm"); // EXTERNAL MODULE: ./node_modules/typescript/lib/typescript.js var typescript = __nccwpck_require__(7414); ;// CONCATENATED MODULE: ./src/async-function.ts const AsyncFunction = Object.getPrototypeOf(async () => null).constructor; function callAsyncFunction(args, source) { - const fn = new AsyncFunction(...Object.keys(args), source); - return fn(...Object.values(args)); + const commonJsArgs = { + ...args, + module: { exports: {} }, + exports: {} + }; + const fn = new AsyncFunction(...Object.keys(commonJsArgs), source); + return fn(...Object.values(commonJsArgs)); } ;// CONCATENATED MODULE: ./src/interpret-script.ts - var SupportedLanguage; (function (SupportedLanguage) { SupportedLanguage["cjs"] = "cjs"; @@ -213431,31 +213433,20 @@ var SupportedLanguage; })(SupportedLanguage || (SupportedLanguage = {})); async function interpretScript(language, context, script) { switch (language) { - case SupportedLanguage.cjs: - return async () => callAsyncFunction(context, script); case SupportedLanguage.cts: case SupportedLanguage.mts: { const fileName = `github-script.${language}`; - const compilerResult = (0,typescript.transpileModule)(script, { + script = (0,typescript.transpileModule)(script, { compilerOptions: { module: typescript.ModuleKind.CommonJS, target: typescript.ScriptTarget.Latest, strict: true }, fileName - }); - return async () => { - const runContext = { - module: { exports: {} }, - exports: {}, - process, - ...context - }; - const runResult = external_node_vm_namespaceObject.runInNewContext(compilerResult.outputText, runContext); - return runResult; - }; + }).outputText; } } + return async () => callAsyncFunction(context, script); } ;// CONCATENATED MODULE: ./src/retry-options.ts diff --git a/src/async-function.ts b/src/async-function.ts index a60af87..f5649c4 100644 --- a/src/async-function.ts +++ b/src/async-function.ts @@ -22,6 +22,11 @@ export function callAsyncFunction( args: AsyncFunctionArguments, source: string ): Promise { - const fn = new AsyncFunction(...Object.keys(args), source) - return fn(...Object.values(args)) + const commonJsArgs = { + ...args, + module: {exports: {}}, + exports: {} + } + const fn = new AsyncFunction(...Object.keys(commonJsArgs), source) + return fn(...Object.values(commonJsArgs)) } diff --git a/src/interpret-script.ts b/src/interpret-script.ts index 7500f41..7469147 100644 --- a/src/interpret-script.ts +++ b/src/interpret-script.ts @@ -1,5 +1,3 @@ -import * as VM from 'node:vm' - import * as core from '@actions/core' import * as exec from '@actions/exec' import {Context} from '@actions/github/lib/context' @@ -36,35 +34,20 @@ export async function interpretScript( script: string ): Promise<() => Promise> { switch (language) { - case SupportedLanguage.cjs: - return async () => callAsyncFunction(context, script) case SupportedLanguage.cts: case SupportedLanguage.mts: { const fileName = `github-script.${language}` - const compilerResult = transpileModule(script, { + script = transpileModule(script, { compilerOptions: { module: ModuleKind.CommonJS, // Take the incoming TypeScript and compile it to CommonJS to run in the CommonJS environment of this action. target: ScriptTarget.Latest, strict: true }, fileName - }) - - return async () => { - const runContext: CjsContext & Record = { - module: {exports: {}}, - exports: {}, - process, - ...context - } - const runResult = VM.runInNewContext( - compilerResult.outputText, - runContext - ) - - return runResult - } + }).outputText } } + + return async () => callAsyncFunction(context, script) }