@cloudpss/lint-staged-config 1.3.2 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -22
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  /* eslint-disable no-console */
2
2
 
3
- const path = require('path');
3
+ const path = require('node:path');
4
4
 
5
5
  const prettierSupportInfo = require('prettier').getSupportInfo();
6
6
  const eslintCli = new (require('eslint').ESLint)({});
7
7
 
8
8
  const DEBUG = (process.env['DEBUG'] ?? '').includes('lint-staged-config');
9
9
 
10
- /** @type {readonly string[]} */
10
+ /** @type {ReadonlyArray<string>} */
11
11
  const prettierSupportedExt = prettierSupportInfo.languages.flatMap((l) => l.extensions ?? []);
12
- /** @type {readonly string[]} */
12
+ /** @type {ReadonlyArray<string>} */
13
13
  const prettierSupportedFilename = prettierSupportInfo.languages.flatMap((l) => l.filenames ?? []);
14
14
 
15
15
  /**
16
- * @param {string} f
16
+ * 检查文件是否是 eslint 支持的文件
17
+ * @param {string} f 文件路径
17
18
  */
18
19
  const isEslintFile = async (f) => {
19
20
  const ext = path.extname(f);
@@ -21,13 +22,14 @@ const isEslintFile = async (f) => {
21
22
  return !(await eslintCli.isPathIgnored(f));
22
23
  };
23
24
 
25
+ /**
26
+ * 检查文件是否是 stylelint 支持的文件
27
+ * @param {string} f 文件路径
28
+ */
24
29
  const isStylelintFile = (() => {
25
30
  try {
26
31
  require.resolve('stylelint');
27
- /**
28
- * @param {string} f
29
- */
30
- return (f) => {
32
+ return (/** @type {string} */ f) => {
31
33
  const ext = path.extname(f);
32
34
  if (!ext || !['.css', '.scss', '.sass'].includes(ext)) return false;
33
35
  return true;
@@ -38,17 +40,20 @@ const isStylelintFile = (() => {
38
40
  })();
39
41
 
40
42
  /**
41
- * @param {string[]} files
42
- * @param {string[]} commands
43
+ * 生成命令
44
+ * @param {string[]} files 文件列表
45
+ * @param {string[]} commands 命令列表
43
46
  */
44
47
  function makeCommands(files, commands) {
45
48
  if (files.length === 0) return [];
46
49
  const fileLine = ` -- '${files.join(`' '`)}'`;
47
50
  return commands.map((c) => c + fileLine);
48
51
  }
52
+
49
53
  /**
50
- * @param {(files: string[]) => (string[] | Promise<string[]>)} impl
51
- * @returns {(files: string[]) => Promise<string[]>}
54
+ * 包装处理函数
55
+ * @param {(files: string[]) => (string[] | Promise<string[]>)} impl 处理函数
56
+ * @returns {(files: string[]) => Promise<string[]>} 包装后的处理函数
52
57
  */
53
58
  function handler(impl) {
54
59
  return async (files) => {
@@ -65,9 +70,6 @@ function handler(impl) {
65
70
  }
66
71
 
67
72
  module.exports = {
68
- /**
69
- * @param {string[]} files
70
- */
71
73
  '*': handler(async (files) => {
72
74
  const commands = [];
73
75
  const eslintFiles = [];
@@ -80,18 +82,20 @@ module.exports = {
80
82
  stylelintFiles.push(file);
81
83
  } else {
82
84
  const filename = path.basename(file);
83
- if (
84
- prettierSupportedFilename.includes(filename) ||
85
- prettierSupportedExt.some((v) => filename.endsWith(v))
86
- ) {
85
+ if (prettierSupportedFilename.includes(filename) || prettierSupportedExt.some((v) => filename.endsWith(v))) {
87
86
  prettierFiles.push(file);
88
87
  }
89
88
  }
90
89
  }
90
+
91
+ const cacheOptions = `--cache --cache-strategy content`;
91
92
  commands.push(
92
- ...makeCommands(eslintFiles, [`eslint --fix --max-warnings 0 --no-error-on-unmatched-pattern`, `git add`]),
93
- ...makeCommands(stylelintFiles, [`stylelint --fix`, `git add`]),
94
- ...makeCommands(prettierFiles, [`prettier --write`, `git add`]),
93
+ ...makeCommands(eslintFiles, [
94
+ `eslint ${cacheOptions} --cache-location node_modules/.cache/eslint/ --fix --max-warnings 0 --no-error-on-unmatched-pattern`,
95
+ `git add`,
96
+ ]),
97
+ ...makeCommands(stylelintFiles, [`stylelint ${cacheOptions} --cache-location node_modules/.cache/stylelint/ --fix`, `git add`]),
98
+ ...makeCommands(prettierFiles, [`prettier ${cacheOptions} --write`, `git add`]),
95
99
  );
96
100
  return commands;
97
101
  }),
package/package.json CHANGED
@@ -3,15 +3,15 @@
3
3
  "license": "MIT",
4
4
  "homepage": "https://cloudpss.net/",
5
5
  "repository": "https://codeup.aliyun.com/cloudpss/code-quality.git",
6
- "version": "1.3.2",
6
+ "version": "1.4.1",
7
7
  "main": "./index.js",
8
8
  "devDependencies": {
9
9
  "@types/eslint": "^8.37.0",
10
10
  "@types/prettier": "^2.7.2"
11
11
  },
12
12
  "dependencies": {
13
- "eslint": "^8.38.0",
14
- "lint-staged": "^13.2.1",
15
- "prettier": "^2.8.7"
13
+ "eslint": "^8.39.0",
14
+ "lint-staged": "^13.2.2",
15
+ "prettier": "^2.8.8"
16
16
  }
17
17
  }