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
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions __test__/async-function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import {callAsyncFunction} from '../src/async-function'

describe('callAsyncFunction', () => {
describe(callAsyncFunction.name, () => {
test('calls the function with its arguments', async () => {
const result = await callAsyncFunction({foo: 'bar'} as any, 'return foo')
expect(result).toEqual('bar')
})

test('throws on ReferenceError', async () => {
test('can await a Promise', async () => {
const result = await callAsyncFunction(
{} as any,
'return await new Promise(resolve => resolve("bar"))'
)
expect(result).toEqual('bar')
})

test(`throws an ${ReferenceError.name}`, async () => {
expect.assertions(1)

try {
Expand Down
2 changes: 1 addition & 1 deletion __test__/get-retry-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {getRetryOptions} from '../src/retry-options'

describe('getRequestOptions', () => {
describe(getRetryOptions.name, () => {
test('retries disabled if retries == 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(
0,
Expand Down
194 changes: 194 additions & 0 deletions __test__/interpret-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/* 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.
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.
return 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')
return 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,
`return {a: 'b'}`
)
return expect(result()).resolves.toEqual({a: 'b'})
})

test(`a script that uses a root level await`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{} as any,
`await Promise.resolve()`
)
return expect(result()).resolves
})

test(`a script imports a script from disk`, async () => {
const result = await interpretScript(
SupportedLanguage.mts,
{require} as any,
`
const {test} = await import('../test/importable')
return 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
Expand Up @@ -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`
Expand Down
Loading