mirror of
https://github.com/actions/github-script.git
synced 2025-12-08 08:06:23 +00:00
Add ESLint and Prettier
This commit is contained in:
13
.eslintrc.yml
Normal file
13
.eslintrc.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
root: true
|
||||
parser: '@typescript-eslint/parser'
|
||||
plugins: ['@typescript-eslint']
|
||||
extends:
|
||||
- eslint:recommended
|
||||
- plugin:@typescript-eslint/eslint-recommended
|
||||
- plugin:@typescript-eslint/recommended
|
||||
- prettier/@typescript-eslint
|
||||
rules:
|
||||
# '@typescript-eslint/explicit-function-return-type': 0
|
||||
'@typescript-eslint/no-use-before-define':
|
||||
- 2
|
||||
- functions: false
|
||||
5
.prettierrc.yml
Normal file
5
.prettierrc.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
arrowParens: avoid
|
||||
bracketSpacing: false
|
||||
semi: false
|
||||
singleQuote: true
|
||||
trailingComma: none
|
||||
@@ -1,8 +1,10 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import {callAsyncFunction} from '../src/async-function'
|
||||
|
||||
describe('callAsyncFunction', () => {
|
||||
test('calls the function with its arguments', async () => {
|
||||
const result = await callAsyncFunction({foo: 'bar'}, 'return foo')
|
||||
const result = await callAsyncFunction({foo: 'bar'} as any, 'return foo')
|
||||
expect(result).toEqual('bar')
|
||||
})
|
||||
|
||||
@@ -10,17 +12,17 @@ describe('callAsyncFunction', () => {
|
||||
expect.assertions(1)
|
||||
|
||||
try {
|
||||
await callAsyncFunction({}, 'proces')
|
||||
await callAsyncFunction({} as any, 'proces')
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(ReferenceError)
|
||||
}
|
||||
})
|
||||
|
||||
test('can access process', async () => {
|
||||
await callAsyncFunction({}, 'process')
|
||||
await callAsyncFunction({} as any, 'process')
|
||||
})
|
||||
|
||||
test('can access console', async () => {
|
||||
await callAsyncFunction({}, 'console')
|
||||
await callAsyncFunction({} as any, 'console')
|
||||
})
|
||||
})
|
||||
|
||||
10
dist/index.js
vendored
10
dist/index.js
vendored
@@ -9274,7 +9274,7 @@ var core = __webpack_require__(470);
|
||||
var lib_github = __webpack_require__(469);
|
||||
|
||||
// CONCATENATED MODULE: ./src/async-function.ts
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
|
||||
function callAsyncFunction(args, source) {
|
||||
const fn = new AsyncFunction(...Object.keys(args), source);
|
||||
return fn(...Object.values(args));
|
||||
@@ -9300,7 +9300,7 @@ async function main() {
|
||||
opts.previews = previews.split(',');
|
||||
const github = new lib_github.GitHub(token, opts);
|
||||
const script = Object(core.getInput)('script', { required: true });
|
||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
|
||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
|
||||
const result = await callAsyncFunction({ require: __webpack_require__(875), github, context: lib_github.context, core: core }, script);
|
||||
let encoding = Object(core.getInput)('result-encoding');
|
||||
encoding = encoding ? encoding : 'json';
|
||||
@@ -9317,15 +9317,11 @@ async function main() {
|
||||
}
|
||||
Object(core.setOutput)('result', output);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function handleError(err) {
|
||||
console.error(err);
|
||||
if (err && err.message) {
|
||||
Object(core.setFailed)(err.message);
|
||||
}
|
||||
else {
|
||||
Object(core.setFailed)(`Unhandled error: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
931
package-lock.json
generated
931
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
46
package.json
46
package.json
@@ -3,21 +3,20 @@
|
||||
"description": "A GitHub action for executing a simple script",
|
||||
"version": "0.9.0",
|
||||
"author": "GitHub",
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.2.4",
|
||||
"@actions/github": "^2.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^25.1.4",
|
||||
"@zeit/ncc": "^0.22.0",
|
||||
"husky": "^4.2.3",
|
||||
"jest": "^25.1.0",
|
||||
"ts-jest": "^25.2.1",
|
||||
"typescript": "^3.8.3"
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts",
|
||||
"format": "prettier --write src __test__",
|
||||
"lint": "eslint src __test__",
|
||||
"check": "run-p --continue-on-error --aggregate-output format lint",
|
||||
"pre-commit": "run-s check test build",
|
||||
"test": "jest"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "npm run build && git add dist/"
|
||||
"pre-commit": "npm run pre-commit && git add dist/"
|
||||
}
|
||||
},
|
||||
"jest": {
|
||||
@@ -33,11 +32,22 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts",
|
||||
"test": "jest"
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.2.4",
|
||||
"@actions/github": "^2.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^25.1.4",
|
||||
"@typescript-eslint/eslint-plugin": "^2.33.0",
|
||||
"@typescript-eslint/parser": "^2.33.0",
|
||||
"@zeit/ncc": "^0.22.0",
|
||||
"eslint": "^7.0.0",
|
||||
"eslint-config-prettier": "^6.11.0",
|
||||
"husky": "^4.2.5",
|
||||
"jest": "^25.1.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.0.5",
|
||||
"ts-jest": "^25.2.1",
|
||||
"typescript": "^3.8.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
||||
import * as core from '@actions/core'
|
||||
import {GitHub} from '@actions/github'
|
||||
import {Context} from '@actions/github/lib/context'
|
||||
|
||||
type AsyncFunctionArguments = {[key: string]: any}
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor
|
||||
|
||||
export function callAsyncFunction(
|
||||
type AsyncFunctionArguments = {
|
||||
context: Context
|
||||
core: typeof core
|
||||
github: GitHub
|
||||
require: NodeRequire
|
||||
}
|
||||
|
||||
export function callAsyncFunction<T>(
|
||||
args: AsyncFunctionArguments,
|
||||
source: string
|
||||
): Promise<any> {
|
||||
): Promise<T> {
|
||||
const fn = new AsyncFunction(...Object.keys(args), source)
|
||||
return fn(...Object.values(args))
|
||||
}
|
||||
|
||||
22
src/main.ts
22
src/main.ts
@@ -5,19 +5,27 @@ import {callAsyncFunction} from './async-function'
|
||||
process.on('unhandledRejection', handleError)
|
||||
main().catch(handleError)
|
||||
|
||||
async function main() {
|
||||
type Options = {
|
||||
log?: Console
|
||||
userAgent?: string
|
||||
previews?: string[]
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const token = core.getInput('github-token', {required: true})
|
||||
const debug = core.getInput('debug')
|
||||
const userAgent = core.getInput('user-agent')
|
||||
const previews = core.getInput('previews')
|
||||
const opts: {[key: string]: any} = {}
|
||||
|
||||
const opts: Options = {}
|
||||
if (debug === 'true') opts.log = console
|
||||
if (userAgent != null) opts.userAgent = userAgent
|
||||
if (previews != null) opts.previews = previews.split(',')
|
||||
|
||||
const github = new GitHub(token, opts)
|
||||
const script = core.getInput('script', {required: true})
|
||||
|
||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
|
||||
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
|
||||
const result = await callAsyncFunction(
|
||||
{require: require, github, context, core},
|
||||
script
|
||||
@@ -42,12 +50,8 @@ async function main() {
|
||||
core.setOutput('result', output)
|
||||
}
|
||||
|
||||
function handleError(err: any) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function handleError(err: any): void {
|
||||
console.error(err)
|
||||
|
||||
if (err && err.message) {
|
||||
core.setFailed(err.message)
|
||||
} else {
|
||||
core.setFailed(`Unhandled error: ${err}`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user