Skip to content

Commit c85852d

Browse files
authored
refactor: refactor project by typescript (#7)
* fix: fix dynamic require error from built file * style: beautify code style * refactor: separate webpack plugin from package * 🔥 chore: remove useless utils * ♻️ refactor: refactor project by ts * 🔧 build: update webpack config * 🐛 fix: fix error for ts project * 🐛 fix: fix ci * 🐛 fix: fix webpack build error * 🐛 fix: fix webpack build error * 🐛 fix: fix module error * 🔖 chore(release): publish v0.1.1
1 parent a9f7af7 commit c85852d

15 files changed

+201
-44577
lines changed

.eslintrc.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
'parser': '@typescript-eslint/parser',
3+
'plugins': [
4+
'@typescript-eslint'
5+
],
6+
'rules': {
7+
'no-multiple-empty-lines': 1,
8+
'import/no-unresolved': 'off',
9+
'import/order': 'warn',
10+
'@typescript-eslint/no-empty-interface': 'warn',
11+
'@typescript-eslint/no-var-requires': 'off',
12+
'@typescript-eslint/ban-ts-comment': 'off',
13+
'@typescript-eslint/explicit-module-boundary-types': 'off',
14+
'@typescript-eslint/no-explicit-any': 'off',
15+
'@typescript-eslint/no-empty-function': 'off'
16+
},
17+
'extends': [
18+
'eslint:recommended',
19+
'plugin:import/errors',
20+
'plugin:import/warnings',
21+
'plugin:import/typescript'
22+
],
23+
'root': true,
24+
'parserOptions': {
25+
'ecmaVersion': 2018,
26+
'sourceType': 'module'
27+
},
28+
'env': {
29+
'browser': true,
30+
'node': true,
31+
'jest': true
32+
}
33+
}

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ jobs:
3131

3232
您的 lint-md 配置文件的路径,支持 `json` 或者 JavaScript 模块。
3333

34-
3534
如果工作目录下没有任何配置文件,我们使用[默认的规则](https://github.com/lint-md/lint-md#rules-%E9%85%8D%E7%BD%AE)
3635

3736
### failOnWarnings
@@ -50,4 +49,4 @@ jobs:
5049

5150
## LICENSE
5251

53-
MIT
52+
MIT

__tests__/github-action.spec.js __tests__/github-action.spec.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
*/
88

99

10-
const path = require('path')
11-
const LintMdAction = require('../src/lint-md-action')
12-
const { mockAction } = require('../src/test-utils')
10+
import * as path from 'path'
11+
12+
import { LintMdAction } from '../src/lint-md-action'
13+
14+
import { mockAction } from '../src/test-utils'
1315

1416

1517
describe('lint-md GitHub action 测试', () => {
@@ -104,4 +106,4 @@ describe('lint-md GitHub action 测试', () => {
104106
const totalErrors = lintMdAction.getErrors()
105107
expect(totalErrors.length).toStrictEqual(2)
106108
})
107-
})
109+
})

__tests__/extend-linter.spec.js __tests__/linter.spec.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
77
*/
88

9-
const ExtendLinter = require('../src/extend-linter')
10-
const path = require('path')
9+
10+
import * as path from 'path'
11+
import { Lint } from '@lint-md/cli'
1112

1213
describe('lint 继承对象测试集合', () => {
1314
test('有 error 出现 (see examples/)', async () => {
14-
const newLinter = new ExtendLinter([path.resolve(process.cwd(), 'examples')], {})
15+
const newLinter = new Lint([path.resolve(process.cwd(), 'examples')], {
16+
rules: {}
17+
})
18+
1519
await newLinter.start()
1620
newLinter.printOverview()
17-
expect(newLinter.errorCount()).toStrictEqual({
21+
expect(newLinter.countError()).toStrictEqual({
1822
'error': 8,
1923
'warning': 0
2024
})
2125
})
22-
})
26+
})

dist/index.js

+1-44,357
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+38-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "@lint-md/github-action",
3-
"version": "0.1.0",
4-
"description": "",
5-
"main": "src/index.js",
3+
"version": "0.1.1",
4+
"description": "github action for lint-md",
5+
"main": "dist/index.js",
66
"scripts": {
7-
"build": "webpack",
8-
"test": "jest --no-cache"
7+
"test": "jest --no-cache",
8+
"lint": "eslint --ext .ts --ext .tsx --max-warnings 0 ./src",
9+
"clean": "rimraf dist",
10+
"build:dev": "yarn clean && webpack --mode development",
11+
"build:prod": "yarn clean && webpack --mode production"
912
},
1013
"repository": {
1114
"type": "git",
@@ -17,16 +20,40 @@
1720
"bugs": {
1821
"url": "https://github.com/yuzhanglong/hello-world-javascript-action/issues"
1922
},
23+
"jest": {
24+
"preset": "ts-jest",
25+
"testEnvironment": "node",
26+
"collectCoverage": true,
27+
"collectCoverageFrom": [
28+
"src/**/*.{js,jsx,ts,tsx}",
29+
"!**/node_modules/**",
30+
"!**/vendor/**"
31+
]
32+
},
2033
"homepage": "https://github.com/yuzhanglong/hello-world-javascript-action#readme",
2134
"devDependencies": {
2235
"@actions/core": "^1.2.6",
2336
"@actions/github": "^4.0.0",
24-
"@lint-md/cli": "^0.1.3",
25-
"babel-polyfill": "^6.26.0",
37+
"@lint-md/cli": "^0.1.5",
38+
"@types/jest": "^26.0.22",
39+
"@typescript-eslint/eslint-plugin": "^4.15.0",
40+
"@typescript-eslint/parser": "^4.15.0",
41+
"eslint": "^7.19.0",
42+
"eslint-plugin-import": "^2.22.1",
2643
"jest": "^26.6.3",
44+
"node-require-webpack-plugin": "^0.1.1",
45+
"rimraf": "^3.0.2",
2746
"terser-webpack-plugin": "^5.1.1",
28-
"webpack": "^5.25.1",
29-
"webpack-cli": "^4.5.0",
30-
"node-require-webpack-plugin": "^0.1.0"
31-
}
47+
"ts-jest": "^26.5.4",
48+
"ts-loader": "^8.1.0",
49+
"typescript": "^4.2.3",
50+
"webpack": "^5.28.0",
51+
"webpack-cli": "^4.5.0"
52+
},
53+
"type": "commonjs",
54+
"files": [
55+
"esm",
56+
"lib",
57+
"src"
58+
]
3259
}

src/extend-linter.js

-58
This file was deleted.

src/index.js src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* File: index.js
2+
* File: index.ts
33
* Description: action 入口
44
* Created: 2021-3-15 20:39:28
55
* Author: yuzhanglong
66
77
*/
88

9-
const LintMdAction = require('./lint-md-action')
9+
import { LintMdAction } from './lint-md-action'
1010

1111
const runAction = async () => {
1212
// init LintMdAction
@@ -24,4 +24,4 @@ const runAction = async () => {
2424
runAction().catch(res => {
2525
console.log(res)
2626
throw new Error('unknown error!')
27-
})
27+
})

src/lint-md-action.js src/lint-md-action.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/*
2-
* File: lint-md-action.js
2+
* File: lint-md-action.ts
33
* Description: lint-md github action 核心逻辑
44
* Created: 2021-3-15 22:23:51
55
* Author: yuzhanglong
66
77
*/
88

9-
require('babel-polyfill')
10-
const core = require('@actions/core')
11-
const path = require('path')
12-
const fs = require('fs')
13-
const ExtendLinter = require('./extend-linter')
9+
import * as fs from 'fs'
10+
import * as path from 'path'
11+
import * as core from '@actions/core'
12+
import { Lint, CliConfig } from '@lint-md/cli/lib/index'
1413

15-
class LintMdAction {
16-
constructor(basePath) {
14+
export class LintMdAction {
15+
private readonly basePath: string
16+
private readonly config: CliConfig
17+
private readonly lintFiles: string[]
18+
private linter: Lint
19+
20+
constructor(basePath?: string) {
1721
if (!basePath) {
1822
this.basePath = process.env.GITHUB_WORKSPACE
1923
}
@@ -42,15 +46,19 @@ class LintMdAction {
4246
}
4347

4448
isPass() {
45-
const result = this.linter ? this.linter.errorCount() : {}
49+
// 没有初始化直接调用 isPass, 返回 true
50+
if (!this.linter) {
51+
return true
52+
}
53+
const result = this.linter.countError()
4654
const noErrorAndWarn = result.error === 0 && result.warning === 0
4755
// 注意这里的 getInput 返回值为 string
4856
return core.getInput('failOnWarnings') === 'true' ? noErrorAndWarn : result.error === 0
4957
}
5058

5159
async lint() {
5260
// 开始 lint
53-
this.linter = new ExtendLinter(this.lintFiles, this.config)
61+
this.linter = new Lint(this.lintFiles, this.config)
5462
await this.linter.start()
5563
return this
5664
}
@@ -71,8 +79,7 @@ class LintMdAction {
7179
}
7280

7381
getErrors() {
74-
return this.linter.getErrorFiles()
82+
// @ts-ignore
83+
return this.linter.errorFiles
7584
}
7685
}
77-
78-
module.exports = LintMdAction

src/test-utils.js

-20
This file was deleted.

src/test-utils.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* File: test-utils.ts
3+
* Description: 测试工具函数集
4+
* Created: 2021-3-30 14:18:15
5+
* Author: yuzhanglong
6+
7+
*/
8+
9+
import * as core from '@actions/core'
10+
11+
// mock GitHub action 的用户参数
12+
export const mockAction = (files?: string, configFile?: string, failOnWarnings?: string) => {
13+
jest.mock('@actions/core')
14+
15+
// mock getInput method
16+
// @ts-ignore
17+
// eslint-disable-next-line no-import-assign
18+
core.getInput = (arg) => {
19+
switch (arg) {
20+
case 'files':
21+
return files || './'
22+
case 'configFile':
23+
return configFile || '.lintmdrc'
24+
case 'failOnWarnings':
25+
return failOnWarnings || 'false'
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)