Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
.dumi
server
pnpm-lock.yaml
.idea
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ npm run build:all
已有 `examples/webtools` 进行调试

```sh
cd examples/webtools
cd examples/web-tools
npm run doctor:webtools
```

Expand All @@ -22,10 +22,10 @@ npm run doctor:webtools
因为 monorepo 的缘故 无需手动 `link`,修改源码后 `examples` 中即可查看效果

```sh
cd packages/doctor-webtools
cd packages/web-tools
npm run dev

npm run doctor:webtools //记得回到 examples/webtools
npm run doctor:webtools //记得回到 examples/web-tools
```

#### 文档贡献指南:
Expand Down
8 changes: 8 additions & 0 deletions examples/eslint/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "standard-with-typescript",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {}
}
18 changes: 18 additions & 0 deletions examples/eslint/package.json
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"
}
5 changes: 5 additions & 0 deletions packages/eslint/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from "father";

export default defineConfig({
extends: "../../.fatherrc.base",
});
1 change: 1 addition & 0 deletions packages/eslint/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org
34 changes: 34 additions & 0 deletions packages/eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@doctors/eslint",
"version": "0.0.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

版本可以同步一下其他包

"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"
Copy link
Member

Choose a reason for hiding this comment

The 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"
}
}
22 changes: 22 additions & 0 deletions packages/eslint/src/commands/eslint.ts
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,
});
};
1 change: 1 addition & 0 deletions packages/eslint/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PRESET_NAME = "eslint";
6 changes: 6 additions & 0 deletions packages/eslint/src/defineConfig.ts
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;
53 changes: 53 additions & 0 deletions packages/eslint/src/features/checkConfigured.ts
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"];
Copy link
Member

@BoyYangzai BoyYangzai Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于 esolint 预设来说 这四个缺一不可吗?

Copy link
Member Author

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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中
}
1 change: 1 addition & 0 deletions packages/eslint/src/features/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [require.resolve("./checkConfigured")];
23 changes: 23 additions & 0 deletions packages/eslint/src/index.ts
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,
],
};
};
24 changes: 24 additions & 0 deletions packages/eslint/src/type.ts
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;
};