- Notifications
You must be signed in to change notification settings - Fork455
feat: Add support for TypeScript scripts#477
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
kf6kjg wants to merge3 commits intoactions:mainChoose a base branch fromkf6kjg:feature/typescript
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.