Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for TypeScript scripts #477

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Add support for TypeScript scripts
Both of the .cts and .mts flavors. Because this action is written in CommonJS both have to compile to CommonJS in order to execute.

As it is TypeScript there's already an expectation of some slowness, so I went with the approach of running the script via the node VM module. While a cleaner approach, it has the caveat that root level await in the script doesn't work. That should become available if #457 is completed.
  • Loading branch information
Ricky C committed Jul 6, 2024
commit d3f9a3b3fbc416372f6e80816f5b710a386750b1
196 changes: 196 additions & 0 deletions __test__/interpret-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import {SupportedLanguage, interpretScript} from '../src/interpret-script'

const scripts: Record<SupportedLanguage, string> = {
[SupportedLanguage.cjs]: `
const FS = require('node:fs') // Proof that we are in CommonJS.
var a // Proof that we are NOT in TypeScript.
return foo // Proof that we executed correctly. Also, this is the pre-existing function-style syntax.
`,
[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.
`,
[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.
`
}

describe(interpretScript.name, () => {
describe(`language set to ${SupportedLanguage.cjs}`, () => {
test(`throws when given a ${SupportedLanguage.cts} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cjs,
{foo: 'bar', require} as any,
scripts.cts
)
).rejects
})

test(`throws when given an ${SupportedLanguage.mts} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cjs,
{foo: 'bar', require} as any,
scripts.mts
)
).rejects
})

test(`interprets a ${SupportedLanguage.cjs} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cjs,
{foo: 'bar', require} as any,
scripts.cjs
)
).resolves
})

test(`when given a ${SupportedLanguage.cjs} script returns a function that can run it correctly`, async () => {
const result = await interpretScript(
SupportedLanguage.cjs,
{foo: 'bar', require} as any,
scripts.cjs
)
return expect(result()).resolves.toEqual('bar')
})
})

describe(`language set to ${SupportedLanguage.cts}`, () => {
test(`throws when given a ${SupportedLanguage.cjs} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cts,
{foo: 'bar', require} as any,
scripts.cjs
)
).rejects
})

test(`throws when given an ${SupportedLanguage.mts} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cts,
{foo: 'bar', require} as any,
scripts.mts
)
).rejects
})

test(`interprets a ${SupportedLanguage.cts} script`, async () => {
return expect(
interpretScript(
SupportedLanguage.cts,
{foo: 'bar', require} as any,
scripts.cts
)
).resolves
})

test(`when given a ${SupportedLanguage.cts} script returns a function that can run it correctly`, async () => {
const result = await interpretScript(
SupportedLanguage.cts,
{foo: 'bar', require} as any,
scripts.cts
)
return expect(result()).resolves.toEqual('bar')
})

test(`a script imports a script from disk`, async () => {
const result = await interpretScript(
SupportedLanguage.cts,
{require} as any,
`
const {test} = require('../test/requireable')
exports = test()
`
)
return expect(result()).resolves.toEqual('hello')
})
})

describe(`language set to ${SupportedLanguage.mts}`, () => {
test(`throws when given a ${SupportedLanguage.cjs} script`, async () => {
return expect(
interpretScript(SupportedLanguage.mts, {foo: 'bar'} as any, scripts.cjs)
).rejects
})

test(`throws when given a ${SupportedLanguage.cts} script`, async () => {
return expect(
interpretScript(SupportedLanguage.mts, {foo: 'bar'} as any, scripts.cts)
).rejects
})

test(`interprets an ${SupportedLanguage.mts} script`, async () => {
return expect(
interpretScript(SupportedLanguage.mts, {foo: 'bar'} as any, scripts.mts)
).resolves
})

test(`when given an ${SupportedLanguage.mts} script returns a function that can run it correctly`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{foo: 'bar'} as any,
scripts.mts
)
return expect(result()).resolves.toEqual('bar')
})

test(`can access console`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`console`
)
return expect(result()).resolves
})

test(`can access process`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`process`
)
return expect(result()).resolves
})

test(`a script that returns an object`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`export default {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.
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`await Promise.resolve()`
)
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.
const result = await interpretScript(
SupportedLanguage.mts,
{require} as any,
`
const {test} = await import('../test/importable')
export default test()
`
)
return expect(result()).resolves.toEqual('hello')
})
})
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@ inputs:
base-url:
description: An optional GitHub REST API URL to connect to a different GitHub instance. For example, https://my.github-enterprise-server.com/api/v3
required: false
language:
description: The language to interpret the script as. Pick from "cjs", "cts", "mts".
default: "cjs"
outputs:
result:
description: The return value of the script, stringified with `JSON.stringify`
Loading
Oops, something went wrong.