Merge pull request #48 from actions/code-quality

Code quality improvements
This commit is contained in:
Jonathan Clem
2020-05-18 11:34:07 -04:00
committed by GitHub
12 changed files with 1004 additions and 73 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

View File

@@ -15,4 +15,5 @@ jobs:
key: ${{runner.os}}-npm-${{hashFiles('**/package-lock.json')}}
restore-keys: ${{runner.os}}-npm-
- run: npm ci
- run: npm run style:check
- run: npm test

3
.gitignore vendored
View File

@@ -1 +1,2 @@
node_modules
/node_modules/
!/.vscode/

5
.prettierrc.yml Normal file
View File

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

10
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"files.exclude": {
"**/dist": true,
"**/node_modules": true
}
}

View File

@@ -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')
})
})

12
dist/index.js vendored
View File

@@ -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,14 +9317,10 @@ 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}`);
}
Object(core.setFailed)(`Unhandled error: ${err}`);
}

View File

@@ -15,7 +15,8 @@ bash> npm run build
It also has a pre-commit hook configured via
[husky](https://www.npmjs.com/package/husky) that should run the build script
before each commit.
before each commit. Additionally, this hook formats code and lints it, as
well.
## Releasing

931
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -3,21 +3,22 @@
"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:check": "prettier --check src __test__",
"format:write": "prettier --write src __test__",
"lint": "eslint src __test__",
"style:check": "run-p --continue-on-error --aggregate-output format:check lint",
"style:write": "run-p --continue-on-error --aggregate-output format:write lint",
"pre-commit": "run-s style:write 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 +34,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"
}
}

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

View File

@@ -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}`)
}
core.setFailed(`Unhandled error: ${err}`)
}