Skip to content

Commit

Permalink
Add Support for ESM Actions (#90)
Browse files Browse the repository at this point in the history
This PR converts `local-action` to ESM, as well as adds support for
JavaScript/TypeScript actions written in ESM syntax.

This is accomplished by replacing proxyquire with quibble, which allows
for stubbing out both CommonJS and ESM packages with less issues.
  • Loading branch information
ncalteen authored Aug 22, 2024
1 parent f23aae5 commit 5d37e4c
Show file tree
Hide file tree
Showing 87 changed files with 1,202 additions and 896 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__fixtures__/javascript
__fixtures__/javascript-esm
__fixtures__/typescript
__fixtures__/typescript-esm
.github/
.vscode/
badges/
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rules:
'camelcase': 'off',
'eslint-comments/no-use': 'off',
'i18n-text/no-en': 'off',
'import/no-namespace': 'off',
'no-console': 'off',
'no-unused-vars': 'off'
}
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21.6.2
20.5.1
13 changes: 13 additions & 0 deletions __fixtures__/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { jest } from '@jest/globals'

export const debug = jest.fn().mockImplementation(() => {})
export const error = jest.fn().mockImplementation(() => {})
export const info = jest.fn().mockImplementation(() => {})
export const getInput = jest.fn().mockImplementation(() => {})
export const setOutput = jest.fn().mockImplementation(() => {})
export const setFailed = jest.fn().mockImplementation(() => {})
export const warning = jest.fn().mockImplementation(() => {})
export const summary = {
addRaw: jest.fn().mockImplementation(() => {}),
write: jest.fn().mockImplementation(() => {})
}
2 changes: 2 additions & 0 deletions __fixtures__/javascript-esm/no-import/.env.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ACTIONS_STEP_DEBUG=false
INPUT_MILLISECONDS=2400
16 changes: 16 additions & 0 deletions __fixtures__/javascript-esm/no-import/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: JavaScript (No Import)
description: This action doesn't import any dependencies

inputs:
myInput:
description: An input
required: true
default: value

outputs:
myOutput:
description: An output

runs:
using: node20
main: dist/index.js
13 changes: 13 additions & 0 deletions __fixtures__/javascript-esm/no-import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "javascript-action",
"description": "GitHub Actions JavaScript template",
"version": "0.0.0",
"type": "module",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {}
}
3 changes: 3 additions & 0 deletions __fixtures__/javascript-esm/no-import/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { run } = require('./main')

run()
7 changes: 7 additions & 0 deletions __fixtures__/javascript-esm/no-import/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async function run() {
return Promise.resolve()
}

module.exports = {
run
}
2 changes: 2 additions & 0 deletions __fixtures__/javascript-esm/success/.env.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ACTIONS_STEP_DEBUG=false
INPUT_MILLISECONDS=2400
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: TypeScript (Success)
name: JavaScript (Success)
description: This action returns without error

inputs:
myInput:
description: An input
required: true
default: 'default value'
default: value

outputs:
myOutput:
Expand Down
13 changes: 13 additions & 0 deletions __fixtures__/javascript-esm/success/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "javascript-action",
"description": "GitHub Actions JavaScript template",
"version": "0.0.0",
"type": "module",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {}
}
3 changes: 3 additions & 0 deletions __fixtures__/javascript-esm/success/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { run } = require('./main')

run()
13 changes: 13 additions & 0 deletions __fixtures__/javascript-esm/success/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { getInput, info, setOutput } = require('@actions/core')

async function run() {
const myInput = getInput('myInput')

setOutput('myOutput', myInput)

info('JavaScript Action Succeeded!')
}

module.exports = {
run
}
8 changes: 0 additions & 8 deletions __fixtures__/javascript/failure/.env.fixture

This file was deleted.

6 changes: 0 additions & 6 deletions __fixtures__/javascript/failure/action.yml

This file was deleted.

7 changes: 0 additions & 7 deletions __fixtures__/javascript/failure/src/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions __fixtures__/javascript/failure/src/main.js

This file was deleted.

10 changes: 2 additions & 8 deletions __fixtures__/javascript/no-import/.env.fixture
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
# Do not commit your actual .env file to Git! This may contain secrets or other
# private information.

# GitHub Actions inputs should follow `INPUT_<name>` format (case-insensitive).
INPUT_milliseconds=2400

# Enable/disable step debug logs
ACTIONS_STEP_DEBUG=false
ACTIONS_STEP_DEBUG=false
INPUT_MILLISECONDS=2400
2 changes: 1 addition & 1 deletion __fixtures__/javascript/no-import/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
myInput:
description: An input
required: true
default: 'default value'
default: value

outputs:
myOutput:
Expand Down
12 changes: 12 additions & 0 deletions __fixtures__/javascript/no-import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "javascript-action",
"description": "GitHub Actions JavaScript template",
"version": "0.0.0",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {}
}
4 changes: 0 additions & 4 deletions __fixtures__/javascript/no-import/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */

const { run } = require('./main')

run()
2 changes: 0 additions & 2 deletions __fixtures__/javascript/no-import/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable import/no-commonjs */

async function run() {
return Promise.resolve()
}
Expand Down
11 changes: 1 addition & 10 deletions __fixtures__/javascript/success/.env.fixture
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
# Do not commit your actual .env file to Git! This may contain secrets or other
# private information.

# GitHub Actions inputs should follow `INPUT_<name>` format (case-insensitive).
INPUT_milliseconds=2400

# Enable/disable step debug logs
ACTIONS_STEP_DEBUG=false

# Step summary output location
GITHUB_STEP_SUMMARY='summary.md'
INPUT_MILLISECONDS=2400
2 changes: 1 addition & 1 deletion __fixtures__/javascript/success/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
myInput:
description: An input
required: true
default: 'default value'
default: value

outputs:
myOutput:
Expand Down
12 changes: 12 additions & 0 deletions __fixtures__/javascript/success/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "javascript-action",
"description": "GitHub Actions JavaScript template",
"version": "0.0.0",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {}
}
4 changes: 0 additions & 4 deletions __fixtures__/javascript/success/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */

const { run } = require('./main')

run()
14 changes: 4 additions & 10 deletions __fixtures__/javascript/success/src/main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-commonjs */

const core = require('@actions/core')
const { getInput, info, setOutput } = require('@actions/core')

async function run() {
const myInput = core.getInput('myInput')

core.setOutput('myOutput', myInput)
const myInput = getInput('myInput')

core.summary.addRaw('JavaScript Action Succeeded!')
await core.summary.write()
setOutput('myOutput', myInput)

core.info('JavaScript Action Succeeded!')
info('JavaScript Action Succeeded!')
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions __fixtures__/tsconfig-paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { jest } from '@jest/globals'

export const loadConfig = jest.fn().mockImplementation(() => {})
export const register = jest.fn().mockImplementation(() => {})
2 changes: 2 additions & 0 deletions __fixtures__/typescript-esm/no-import/.env.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ACTIONS_STEP_DEBUG=false
INPUT_MILLISECONDS=2400
17 changes: 17 additions & 0 deletions __fixtures__/typescript-esm/no-import/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

inputs:
milliseconds:
description: Your input description here
required: true
default: '1000'

outputs:
time:
description: Your output description here

runs:
using: node20
main: dist/index.js
17 changes: 17 additions & 0 deletions __fixtures__/typescript-esm/no-import/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "typescript-action",
"description": "GitHub Actions TypeScript template",
"version": "0.0.0",
"type": "module",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {
"@types/node": "^20.14.7",
"ts-node": "^10.9.2",
"typescript": "^5.5.2"
}
}
3 changes: 3 additions & 0 deletions __fixtures__/typescript-esm/no-import/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { run } from './main.js'

run()
3 changes: 3 additions & 0 deletions __fixtures__/typescript-esm/no-import/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function run(): Promise<void> {
console.log('TypeScript ESM Action (no-import)!')
}
25 changes: 25 additions & 0 deletions __fixtures__/typescript-esm/no-import/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationMap": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2022"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"newLine": "lf",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"pretty": true,
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"target": "ES2022"
},
"exclude": ["node_modules"],
"include": ["src"]
}
2 changes: 2 additions & 0 deletions __fixtures__/typescript-esm/success/.env.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ACTIONS_STEP_DEBUG=false
INPUT_MILLISECONDS=2400
17 changes: 17 additions & 0 deletions __fixtures__/typescript-esm/success/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: The name of your action here
description: Provide a description here
author: Your name or organization here

inputs:
milliseconds:
description: Your input description here
required: true
default: '1000'

outputs:
time:
description: Your output description here

runs:
using: node20
main: dist/index.js
17 changes: 17 additions & 0 deletions __fixtures__/typescript-esm/success/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "typescript-action",
"description": "GitHub Actions TypeScript template",
"version": "0.0.0",
"type": "module",
"engines": {
"node": ">=20"
},
"dependencies": {
"@actions/core": "^1.10.1"
},
"devDependencies": {
"@types/node": "^20.14.7",
"ts-node": "^10.9.2",
"typescript": "^5.5.2"
}
}
Loading

0 comments on commit 5d37e4c

Please sign in to comment.