-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ feat: add eslint preset #20
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ dist | |
.dumi | ||
server | ||
pnpm-lock.yaml | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "standard-with-typescript", | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"rules": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "eslint-example", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"doctor:eslint": "doctor eslint", | ||
"test": "npm run doctor:eslint" | ||
}, | ||
"dependencies": { | ||
"@doctors/core": "workspace:^", | ||
"@doctors/eslint": "workspace:^", | ||
"eslint": "^8.43.0" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineConfig } from "father"; | ||
|
||
export default defineConfig({ | ||
extends: "../../.fatherrc.base", | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
registry=https://registry.npmjs.org |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@doctors/eslint", | ||
"version": "0.0.3", | ||
"description": "", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"dev": "father dev", | ||
"build": "father build", | ||
"build:deps": "father prebundle", | ||
"prepublishOnly": "father doctor && npm run build" | ||
}, | ||
"repository": "https://github.com/BoyYangzai/doctor", | ||
"keywords": [], | ||
"authors": [ | ||
"洋" | ||
], | ||
"license": "MIT", | ||
"files": [ | ||
"./dist", | ||
"./bin" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 除了 @doctors/core 之外不需要 bin 字段 可以删除一下 |
||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": { | ||
"@doctors/core": "workspace:^" | ||
}, | ||
"dependencies": { | ||
"@doctors/utils": "workspace:^", | ||
"@umijs/core": "^4.0.71", | ||
"@umijs/utils": "^4.0.71" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { generatePreset } from "@doctors/core"; | ||
import { IApi } from "../type"; | ||
import { ConfigSchema } from "../type"; | ||
import { Nullify } from "@doctors/core"; | ||
import { getConfigFile } from "../features/checkConfigured"; | ||
import { PRESET_NAME } from "../constants"; | ||
|
||
const schema: Nullify<ConfigSchema> = { | ||
eslint: {}, | ||
}; | ||
|
||
export default (api: IApi) => { | ||
const meta = { | ||
eslintConfig: getConfigFile(api), // 从 getConfigFile 获取 | ||
}; | ||
generatePreset({ | ||
api, | ||
command: PRESET_NAME, | ||
schema, | ||
meta, | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const PRESET_NAME = "eslint"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ConfigSchema } from "./type"; | ||
|
||
const defineConfig = (config: ConfigSchema) => { | ||
return config; | ||
}; | ||
export default defineConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { IApi } from "../type"; | ||
import { DoctorLevel } from "@doctors/core"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
export default (api: IApi) => { | ||
// 解构 meta | ||
api.addDoctorEslintCheck((meta) => { | ||
const { eslintConfig } = meta; | ||
let isEsLintOk: boolean = true; | ||
const prerequisites = ["env", "extends", "parserOptions", "rules"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 对于 esolint 预设来说 这四个缺一不可吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应该有哪些必备预设呢 |
||
const configKeys = Object.keys(eslintConfig); | ||
prerequisites.forEach((item) => { | ||
if (!configKeys.includes(item)) { | ||
isEsLintOk = false; | ||
} | ||
}); | ||
const doctorLevel = isEsLintOk ? DoctorLevel.SUCCESS : DoctorLevel.WARN; | ||
return isEsLintOk | ||
? { | ||
label: "ESLint Check", | ||
description: "ESLint prerequisites have been configured", | ||
doctorLevel, | ||
} | ||
: { | ||
label: "ESLint Check", | ||
description: "ESLint prerequisites have not been configured", | ||
doctorLevel, | ||
}; | ||
}); | ||
}; | ||
/** | ||
* @description:获取eslint配置 | ||
* @return {*} | ||
* @param api | ||
*/ | ||
export function getConfigFile(api: IApi) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个函数可以单独放一个文件 // getEslintConfigFile.ts
export default getEslntConfigFile(){} |
||
const cwd = api.service.paths.cwd; | ||
let config = ""; | ||
let files = fs.readdirSync(cwd); | ||
for (let i = 0; i < files.length; i++) { | ||
if (files[i].includes("eslintrc")) { | ||
const filedir = path.join(cwd, files[i]); | ||
const file = fs.statSync(filedir); | ||
if (file.isFile()) { | ||
//判断是否为文件 | ||
config = fs.readFileSync(filedir, "utf-8"); | ||
return JSON.parse(config); | ||
} | ||
} | ||
} | ||
//也可能在package.json中 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default [require.resolve("./checkConfigured")]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import features from "./features"; | ||
import { IApi } from "./type"; | ||
import { PRESET_NAME } from "./constants"; | ||
|
||
// 为用户导出需要的 类型 和 工具函数 | ||
export * from "./type"; | ||
export { default as defineConfig } from "./defineConfig"; | ||
|
||
export default (api: IApi) => { | ||
// key 用来识别插件 否则会使用默认的小驼峰包名 容易和其他插件重复 | ||
api.describe({ | ||
key: `doctor-preset-${PRESET_NAME}`, | ||
}); | ||
|
||
return { | ||
plugins: [ | ||
//commands | ||
require.resolve("./commands/eslint"), | ||
//features | ||
...features, | ||
], | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { IApi as DoctorApi, DoctorMeta, DoctorLevel } from "@doctors/core"; | ||
|
||
// 校验配置文件以及类型提示的 Schema 列表 | ||
/** | ||
* 用每一个 Preset 来单独维护 Schema 以便获得更全面的类型提示 | ||
*/ | ||
export interface ConfigSchema { | ||
eslint: {}; | ||
} | ||
|
||
// 元数据 | ||
interface Meta { | ||
eslintConfig: object; | ||
} | ||
|
||
export type IApi = DoctorApi & { | ||
addDoctorEslintCheckBefore: (fn: () => void) => void; | ||
|
||
addDoctorEslintCheck: { | ||
(fn: (meta: Meta) => DoctorMeta | undefined): void; | ||
}; | ||
|
||
addDoctorEslintCheckEnd: (fn: () => void) => void; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
版本可以同步一下其他包