nodejs 项目自动格式化方案

之前一直遇到一个问题,多个人合作同一个项目时,提交的代码样式各式各样,commit 也随便写,这让 git 记录反而成为一种负担。

大概一年前,因为工作需要,我自己构建了一个 nodejs 项目脚手架,实现自动格式化代码,规范化提交。

ESlint

ESLint 是一款语法检测工具。它可以根据人们规定的规则,提示用户代码是否符合规定的代码规则。

在 vscode 中,我们很容易可以找到插件Eslint

需要在项目里安装 eslint

1
yarn add eslint eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard --dev

编写.eslintrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
"root": true,
"env": {
// 一个环境定义了一组预定义的全局变量。
"es6": true, // 启用 es6 语法
"node": true, // - Node.js 全局变量和 Node.js 作用域。
"browser": true,
"Fluid": true
},
"extends": [
"standard",
"plugin:node/recommended"
],
"plugins": [
"standard",
"node"
],
"rules": {
"semi": [
"error",
"always"
],
"quotes": [
"error",
"double"
],
"no-multiple-empty-lines": "error",
"no-var": "error",
"no-template-curly-in-string": "off",
"node/no-deprecated-api": "off",
"camelcase": "off",
"no-bitwise": "off",
"no-case-declarations": "off",
"no-new": "off",
"new-cap": "off",
"no-unmodified-loop-condition": "off",
"no-loop-func": "off",
"prefer-promise-reject-errors": "off",
"node/no-unsupported-features/es-syntax": "off",
"standard/no-callback-literal": "off",
"no-tabs": "off",
"indent": [
"error",
"tab"
],
"space-before-function-paren": [
"error",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
],
"arrow-parens": [
"error",
"as-needed"
]
}
}

并在 vscode 的 settings 中设置 eslint

1
2
3
4
5
6
7
{
"eslint.enable": true,
"eslint.alwaysShowStatus": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
},
}

代码格式化已完成,接下来就要处理 git commit 了

commitizen

安装

1
yarn add @commitlint/cli @commitlint/config-conventional commitizen cz-customizable husky --dev

配置

1
2
3
4
5
6
7
8
9
// commitlint.config.js
module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
"subject-empty": [0, "always"],
"type-empty": [0, "always"]
}
};

在 package 中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"husky": {
"hooks": {
"pre-commit": "eslint --fix .",
"pre-push": "eslint --fix .",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"config": {
"commitizen": {
"path": "node_modules/cz-customizable"
},
"cz-customizable": {
"config": "config/cz-config.js"
}
},
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//config/cz-config.js

module.exports = {
types: [
{ value: "特性✨", name: "特性:一个新的特性" },
{ value: "修复🐛", name: "修复:修复一个 Bug" },
{ value: "文档📚", name: "文档:变更的只有文档" },
{ value: "格式🌈", name: "格式:空格,分号等格式修复" },
{ value: "重构⛑", name: "重构:代码重构,注意和特性、修复区分开" },
{ value: "性能🕑", name: "性能:提升性能" },
{ value: "测试☕", name: "测试:添加一个测试" },
{ value: "工具⛏", name: "工具:开发工具变动(构建、脚手架工具等)" },
{ value: "回滚🔙", name: "回滚:代码回退" }
],

scopes: [
{ name: "command" },
{ name: "config" },
{ name: "modules" },
{ name: "tools" }
],

// it needs to match the value for field type. Eg.: 'fix'
/*
scopeOverrides: {
fix: [
{name: 'merge'},
{name: 'style'},
{name: 'e2eTest'},
{name: 'unitTest'}
]
},
*/
// override the messages, defaults are as follows
messages: {
type: "选择一种你的提交类型:",
scope: "选择一个 scope (可选):",
// used if allowCustomScopes is true
customScope: "Denote the SCOPE of this change:",
subject: "短说明:\n",
body: "长说明,使用|换行(可选):\n",
breaking: "非兼容性说明 (可选):\n",
footer: "关联关闭的 issue,例如:#31, #34(可选):\n",
confirmCommit: "确定提交说明?"
},

allowCustomScopes: true,
allowBreakingChanges: ["特性", "修复"],

// limit subject length
subjectLimit: 100
};

使用

1
npx cz

nodejs 项目自动格式化方案
https://bubao.github.io/posts/fe88e6b7.html
作者
一念
发布于
2020年9月16日
许可协议