0%

hexo-g 报错 Cannot set property ‘lastIndex’ of undefined

作者:草梅友仁

http://huanyouchen.github.io/2018/05/30/hexo-g-error-TypeError-cannot-set-property-lastIndex-of-undefined/

使用 hexo -g 时候报错:TypeError: Cannot set property ‘lastIndex’ of undefined

解决方法:

在配置文件_config.yml 中将 highlight 选项的 auto_detect 设为 false

方法来自:https://github.com/hexojs/hexo/issues/1627

本文作者:草梅友仁
本文地址: https://blog.cmyr.ltd/archives/4b3a011e.html
版权声明:转载请注明出处!

用 typescript 生成 Swagger 文档

参考地址:https://wz2cool.github.io/2018/04/14/swagger-ts-doc-start/

apiModelProperty 装饰器

这个装饰器主要是为了生成 definitions 中的 model,我们看代码可看到如何描述一个 typescript 中的一个类。

1
2
3
4
5
6
7
8
9
10
11
12
import { apiModelProperty, DataType } from "swagger-ts-doc";

export class UpdateStudentDto {
@apiModelProperty(
DataType.STRING, // 类型
false, // 是否必填
"学生姓名" // 描述
)
public name: string;
@apiModelProperty(DataType.INTEGER, false, "学生年龄")
public age: number;
}

最后会生成与之对应的 swagger json 描述(这里我们不使用 yaml 语法,使用的 json 语法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"UpdateStudentDto": {
"type": "object",
"required": ["name", "age"],
"properties": {
"name": {
"type": "string",
"description": "学生姓名"
},
"age": {
"type": "integer",
"description": "学生年龄"
}
}
},

Request 参数

参考 swagger 文档:
https://swagger.io/docs/specification/describing-parameters/
https://swagger.io/docs/specification/describing-request-body/

  • RequestBody 类对应文档 requestBody
  • PathVariable 类对应文档 path parameters (in: path)
  • RequestParam 类对弈文档 query parameters (in: query)

Reponse

参考 swagger 文档:
https://swagger.io/docs/specification/describing-responses/
我们看一下定义多个返回相应

1
2
3
4
5
[
new Response(HttpStatusCode.OK, DataType.STRING, "ok"),
new Response(HttpStatusCode.INTERNAL_SERVER_ERROR, DataType.STRING, "内部错误"),
new Response(HttpStatusCode.NOT_FOUND, DataType.STRING, "学生未找到"),
],

registerRequestMapping 方法

这里就是我们要去生成 swagger 中 paths 节点调用的方法,这里我们举一个修改学生的一个例子。

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
 registerRequestMapping(
StudentApi, // tags 类似于把一些路由放到一个组里面
"/students/{id}", // 路由
RequestMethod.PUT,
[
new PathVariable("id", DataType.STRING, "学生ID"),
new RequestBody("student", DataType.OBJECT, UpdateStudentDto, "学生"),
],
[
new Response(HttpStatusCode.OK, DataType.STRING, "ok"),
new Response(HttpStatusCode.INTERNAL_SERVER_ERROR, DataType.STRING, "内部错误"),
new Response(HttpStatusCode.NOT_FOUND, DataType.STRING, "学生未找到"),
],
"修改学生"); // 对这个路由的描述
route.put("/:id", (req, res, next) => {
const input = req.body;
const id = req.params.id;
if (!id) {
res.status(HttpStatusCode.INTERNAL_SERVER_ERROR);
res.json("学生ID不能为空");
return;
}

if (lodash.findIndex(this.students, (x) => x.uuid === id) < 0) {
res.status(HttpStatusCode.NOT_FOUND);
res.json(`未能找到学生`);
return;
}

const student = new Student();
student.uuid = id;
student.name = input.name;
student.age = input.age;
this.modifyStudent(student);
res.json("ok");
});

本文作者:草梅友仁
本文地址: https://blog.cmyr.ltd/archives/bbc1fd72.html
版权声明:转载请注明出处!

自动生成日志

参考链接:https://juejin.im/post/5bd2debfe51d457abc710b57

Commitizen – 自动生成合格的 commit message

根据上述的描述,你是不是在感慨写个 commit message 好麻烦,这里介绍下 Commitizen – 能够根据提示自动生成符合规范的 commit message

安装

1
2
$ npm install -g commitizen
复制代码

在项目中使用

然后,在项目目录里,运行下面的命令,使其支持 AngularCommit message 格式。

1
2
$ commitizen init cz-conventional-changelog --save --save-exact
复制代码

commit

在提交的时候就可以使用 git cz 就可以根据提示,生成自动化的 commit message

validate-commit-msg 检查你的 commit-message 规范

Commitizen 可以帮助我们规范自己的 commit-message,但是在团队合作中,如何规范其他成员的 commit 规范呢?

可以使用 validate-commit-msg 来检查你的项目的 commit-message 是否符合格式

validate-commit-msg 安装

1
2
npm install --save-dev validate-commit-msg
复制代码

husky 安装

按照 validate-commit-msg 中 README 中写的,可以用 validate-commit-msg 作为一个 githook 来验证提交消息,并且推荐了 husky

This provides you a binary that you can use as a githook to validate the commit message. I recommend husky. You’ll want to make this part of the commit-msg githook, e.g. when using husky, add “commitmsg”: “validate-commit-msg” to your npm scripts in package.json.

执行

1
2
npm install husky --save-dev
复制代码

并且在 package.json 中的 scripts 字段中加入

1
2
"commitmsg": "validate-commit-msg"
复制代码

然后每次 git commit 之后,就会自动检查 commit message 是否合格。如果不合格,就会报错

1
2
3
4
husky > commit-msg (node v9.2.1)
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
change
husky > commit-msg hook failed (add --no-verify to bypass)

生成 Change log

如果你的所有 commit 都符合 Angular 格式,那么发布新版本时, Change log 就可以用脚本自动生成。

生成的文档包括以下三个部分。

  • New features
  • Bug fixes
  • Breaking changes.

每个部分都会罗列相关的 commit ,并且有指向这些 commit 的链接。当然,生成的文档允许手动修改,所以发布前,你还可以添加其他内容。

conventional-changelog 自动根据 commit 生成 change log

conventional-changelog 安装

1
npm install -g conventional-changelog-cli

本文作者:草梅友仁
本文地址: https://blog.cmyr.ltd/archives/320b027.html
版权声明:转载请注明出处!