周报@2023-05-23

husky hooks 不生效,不起作用的解决方法。 解决 TS2451: Cannot redeclare block-scoped variable 'self' 错误。 Java 中使用 swagger 如何设置 operationId。 解决 nginx: [emerg] "add_header" directive is not allowed here 错误。 Git 排除某个文件夹,但是要将其中的某个或某些文件包含进来。

husky hooks 不生效,不起作用的解决方法。

具体办法看文档

  • Ensure that you don't have a typo in your filename. For example, precommit or pre-commit.sh are invalid names. See Git hooks documentation for valid names.

  • Check that git config core.hooksPath returns .husky (or your custom hooks directory).

  • Verify that hook files are executable. This is automatically set when using husky add command but you can run chmod +x .husky/<hookname> to fix that.

  • Check that your version of Git is greater than 2.9.

一般以前生效,新建的项目不生效的原因:大概是因为项目是复制的,文件没有设置可执行,run chmod +x .husky/<hookname> to fix that.

TS2451: Cannot redeclare block-scoped variable 'self'.

https://stackoverflow.com/questions/35758584/cannot-redeclare-block-scoped-variable

如何解决在 service-worker 中 self 类型不正确?可以重新定义 self 类型,重新定义 self 类型的时候报错,加个 export 就好了

ts
1export declare const self: ServiceWorkerGlobalScope & typeof window;

Java 中使用 swagger 如何设置 operationId

Java 中可以使用 @ApiOperation() 注解中的 nickname 设置 operationId

nginx: [emerg] "add_header" directive is not allowed here

解决 nginx 报错 nginx: [emerg] "add_header" directive is not allowed here

"add_header" 指令在 Nginx 配置文件中不允许在所有位置使用。如果你在一个 if 语句中尝试使用 "add_header" 指令,会引发 "add_header directive is not allowed here" 的错误。

一种替代方案是使用 "map" 指令来设置一个变量,然后在 server 或 location 上下文中使用 "add_header" 指令:

nginx
1map $request_method $cors_header {
2    OPTIONS "Value-of-your-header";
3    default "";
4}
5
6server {
7    ...
8
9    add_header 'Name-of-your-header' $cors_header;
10
11    if ($request_method = 'OPTIONS') {
12        return 204;
13    }
14
15    ...
16}

在这个例子中,当请求方法为 OPTIONS 时,$cors_header 变量的值被设置为 "Value-of-your-header"。对于其他请求方法,这个变量的值为空。然后,add_header 指令在 server 上下文中使用,并将 $cors_header 作为头部值。所以,只有当请求方法为 OPTIONS 时,才会添加此头部。

请根据你的需要修改 "Name-of-your-header" 和 "Value-of-your-header"。

Git 排除某个文件夹,但是要将其中的某个或某些文件包含进来

如果你想在 Git 中排除某个文件夹,但是要将其中的某个或某些文件包含进来,你可以在 .gitignore 文件中使用特殊的语法。

以下面的例子为例,假设你想要排除 my_folder 中的所有文件,但是想要包含 my_folder/subfolder/my_file.txt。首先,你需要在 .gitignore 文件中添加以下内容:

bash
1# Ignore everything in the directory
2my_folder/*
3
4# Exclude specific subdirectories or files
5!my_folder/subfolder/
6!my_folder/subfolder/my_file.txt

第一行命令会让 Git 忽略 my_folder 中的所有文件。然后,下面两行的 ! 符号将特定的子目录和文件从忽略列表中排除。注意,我们需要显式地将整个子目录添加到例外中,因为我们之前已经忽略了整个 my_folder 文件夹。

你需要根据你的具体情况调整上述的路径和文件名,以满足你的需求。