Add ESLint and Prettier

This commit is contained in:
Jonathan Clem
2020-05-18 11:26:47 -04:00
parent b945d091bf
commit 3037861304
8 changed files with 987 additions and 71 deletions

13
.eslintrc.yml Normal file
View 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
View File

@@ -0,0 +1,5 @@
arrowParens: avoid
bracketSpacing: false
semi: false
singleQuote: true
trailingComma: none

View File

@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {callAsyncFunction} from '../src/async-function' import {callAsyncFunction} from '../src/async-function'
describe('callAsyncFunction', () => { describe('callAsyncFunction', () => {
test('calls the function with its arguments', async () => { 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') expect(result).toEqual('bar')
}) })
@@ -10,17 +12,17 @@ describe('callAsyncFunction', () => {
expect.assertions(1) expect.assertions(1)
try { try {
await callAsyncFunction({}, 'proces') await callAsyncFunction({} as any, 'proces')
} catch (err) { } catch (err) {
expect(err).toBeInstanceOf(ReferenceError) expect(err).toBeInstanceOf(ReferenceError)
} }
}) })
test('can access process', async () => { test('can access process', async () => {
await callAsyncFunction({}, 'process') await callAsyncFunction({} as any, 'process')
}) })
test('can access console', async () => { test('can access console', async () => {
await callAsyncFunction({}, 'console') await callAsyncFunction({} as any, 'console')
}) })
}) })

10
dist/index.js vendored
View File

@@ -9274,7 +9274,7 @@ var core = __webpack_require__(470);
var lib_github = __webpack_require__(469); var lib_github = __webpack_require__(469);
// CONCATENATED MODULE: ./src/async-function.ts // CONCATENATED MODULE: ./src/async-function.ts
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor; const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
function callAsyncFunction(args, source) { function callAsyncFunction(args, source) {
const fn = new AsyncFunction(...Object.keys(args), source); const fn = new AsyncFunction(...Object.keys(args), source);
return fn(...Object.values(args)); return fn(...Object.values(args));
@@ -9300,7 +9300,7 @@ async function main() {
opts.previews = previews.split(','); opts.previews = previews.split(',');
const github = new lib_github.GitHub(token, opts); const github = new lib_github.GitHub(token, opts);
const script = Object(core.getInput)('script', { required: true }); 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); const result = await callAsyncFunction({ require: __webpack_require__(875), github, context: lib_github.context, core: core }, script);
let encoding = Object(core.getInput)('result-encoding'); let encoding = Object(core.getInput)('result-encoding');
encoding = encoding ? encoding : 'json'; encoding = encoding ? encoding : 'json';
@@ -9317,14 +9317,10 @@ async function main() {
} }
Object(core.setOutput)('result', output); Object(core.setOutput)('result', output);
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleError(err) { function handleError(err) {
console.error(err); console.error(err);
if (err && err.message) {
Object(core.setFailed)(err.message);
}
else {
Object(core.setFailed)(`Unhandled error: ${err}`); Object(core.setFailed)(`Unhandled error: ${err}`);
}
} }

931
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,21 +3,20 @@
"description": "A GitHub action for executing a simple script", "description": "A GitHub action for executing a simple script",
"version": "0.9.0", "version": "0.9.0",
"author": "GitHub", "author": "GitHub",
"dependencies": { "license": "MIT",
"@actions/core": "^1.2.4", "main": "dist/index.js",
"@actions/github": "^2.2.0" "private": true,
}, "scripts": {
"devDependencies": { "build": "ncc build src/main.ts",
"@types/jest": "^25.1.4", "format": "prettier --write src __test__",
"@zeit/ncc": "^0.22.0", "lint": "eslint src __test__",
"husky": "^4.2.3", "check": "run-p --continue-on-error --aggregate-output format lint",
"jest": "^25.1.0", "pre-commit": "run-s check test build",
"ts-jest": "^25.2.1", "test": "jest"
"typescript": "^3.8.3"
}, },
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "npm run build && git add dist/" "pre-commit": "npm run pre-commit && git add dist/"
} }
}, },
"jest": { "jest": {
@@ -33,11 +32,22 @@
} }
} }
}, },
"license": "MIT", "dependencies": {
"main": "dist/index.js", "@actions/core": "^1.2.4",
"private": true, "@actions/github": "^2.2.0"
"scripts": { },
"build": "ncc build src/main.ts", "devDependencies": {
"test": "jest" "@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"
} }
} }

View File

@@ -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, args: AsyncFunctionArguments,
source: string source: string
): Promise<any> { ): Promise<T> {
const fn = new AsyncFunction(...Object.keys(args), source) const fn = new AsyncFunction(...Object.keys(args), source)
return fn(...Object.values(args)) return fn(...Object.values(args))
} }

View File

@@ -5,19 +5,27 @@ import {callAsyncFunction} from './async-function'
process.on('unhandledRejection', handleError) process.on('unhandledRejection', handleError)
main().catch(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 token = core.getInput('github-token', {required: true})
const debug = core.getInput('debug') const debug = core.getInput('debug')
const userAgent = core.getInput('user-agent') const userAgent = core.getInput('user-agent')
const previews = core.getInput('previews') const previews = core.getInput('previews')
const opts: {[key: string]: any} = {}
const opts: Options = {}
if (debug === 'true') opts.log = console if (debug === 'true') opts.log = console
if (userAgent != null) opts.userAgent = userAgent if (userAgent != null) opts.userAgent = userAgent
if (previews != null) opts.previews = previews.split(',') if (previews != null) opts.previews = previews.split(',')
const github = new GitHub(token, opts) const github = new GitHub(token, opts)
const script = core.getInput('script', {required: true}) 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( const result = await callAsyncFunction(
{require: require, github, context, core}, {require: require, github, context, core},
script script
@@ -42,12 +50,8 @@ async function main() {
core.setOutput('result', output) 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) console.error(err)
if (err && err.message) {
core.setFailed(err.message)
} else {
core.setFailed(`Unhandled error: ${err}`) core.setFailed(`Unhandled error: ${err}`)
}
} }