@qiangzaifan/encode-fe-lint 0.0.0-stage → 1.0.0

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 (79) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +190 -2
  3. package/lib/actions/init.d.ts +3 -0
  4. package/lib/actions/init.js +250 -0
  5. package/lib/actions/scan.d.ts +3 -0
  6. package/lib/actions/scan.js +146 -0
  7. package/lib/actions/update.d.ts +2 -0
  8. package/lib/actions/update.js +112 -0
  9. package/lib/cli.d.ts +2 -0
  10. package/lib/cli.js +276 -0
  11. package/lib/config/_editorconfig.ejs +13 -0
  12. package/lib/config/_eslintignore.ejs +9 -0
  13. package/lib/config/_eslintrc.js.ejs +3 -0
  14. package/lib/config/_markdownlint.json.ejs +1 -0
  15. package/lib/config/_markdownlintignore.ejs +5 -0
  16. package/lib/config/_prettierrc.js.ejs +8 -0
  17. package/lib/config/_stylelintignore.ejs +5 -0
  18. package/lib/config/_stylelintrc.js.ejs +5 -0
  19. package/lib/config/_vscode/extensions.json.ejs +7 -0
  20. package/lib/config/_vscode/settings.json.ejs +54 -0
  21. package/lib/config/commitlint.config.js.ejs +3 -0
  22. package/lib/config/encode-fe-lint.config.js.ejs +6 -0
  23. package/lib/index.d.ts +5 -0
  24. package/lib/index.js +94 -0
  25. package/lib/lints/eslint/doESLint.d.ts +6 -0
  26. package/lib/lints/eslint/doESLint.js +72 -0
  27. package/lib/lints/eslint/formatESLintResults.d.ts +3 -0
  28. package/lib/lints/eslint/formatESLintResults.js +39 -0
  29. package/lib/lints/eslint/getESLintConfig.d.ts +3 -0
  30. package/lib/lints/eslint/getESLintConfig.js +67 -0
  31. package/lib/lints/eslint/getESLintConfigType.d.ts +2 -0
  32. package/lib/lints/eslint/getESLintConfigType.js +23 -0
  33. package/lib/lints/eslint/index.d.ts +3 -0
  34. package/lib/lints/eslint/index.js +19 -0
  35. package/lib/lints/index.d.ts +4 -0
  36. package/lib/lints/index.js +20 -0
  37. package/lib/lints/markdownlint/doMarkdownlint.d.ts +6 -0
  38. package/lib/lints/markdownlint/doMarkdownlint.js +120 -0
  39. package/lib/lints/markdownlint/formatMarkdownlintResults.d.ts +3 -0
  40. package/lib/lints/markdownlint/formatMarkdownlintResults.js +39 -0
  41. package/lib/lints/markdownlint/getMarkdownlintConfig.d.ts +7 -0
  42. package/lib/lints/markdownlint/getMarkdownlintConfig.js +31 -0
  43. package/lib/lints/markdownlint/index.d.ts +3 -0
  44. package/lib/lints/markdownlint/index.js +19 -0
  45. package/lib/lints/prettier/doPrettier.d.ts +4 -0
  46. package/lib/lints/prettier/doPrettier.js +94 -0
  47. package/lib/lints/prettier/index.d.ts +1 -0
  48. package/lib/lints/prettier/index.js +17 -0
  49. package/lib/lints/stylelint/doStylelint.d.ts +5 -0
  50. package/lib/lints/stylelint/doStylelint.js +77 -0
  51. package/lib/lints/stylelint/formatStylelintResults.d.ts +3 -0
  52. package/lib/lints/stylelint/formatStylelintResults.js +39 -0
  53. package/lib/lints/stylelint/getStylelintConfig.d.ts +3 -0
  54. package/lib/lints/stylelint/getStylelintConfig.js +36 -0
  55. package/lib/lints/stylelint/getStylelintDocUrl.d.ts +1 -0
  56. package/lib/lints/stylelint/getStylelintDocUrl.js +13 -0
  57. package/lib/lints/stylelint/index.d.ts +3 -0
  58. package/lib/lints/stylelint/index.js +19 -0
  59. package/lib/types.d.ts +73 -0
  60. package/lib/types.js +2 -0
  61. package/lib/utils/configure-hooks.d.ts +3 -0
  62. package/lib/utils/configure-hooks.js +81 -0
  63. package/lib/utils/conflict-resolve.d.ts +3 -0
  64. package/lib/utils/conflict-resolve.js +174 -0
  65. package/lib/utils/constants.d.ts +19 -0
  66. package/lib/utils/constants.js +122 -0
  67. package/lib/utils/generate-template.d.ts +2 -0
  68. package/lib/utils/generate-template.js +99 -0
  69. package/lib/utils/get-files.d.ts +3 -0
  70. package/lib/utils/get-files.js +58 -0
  71. package/lib/utils/git.d.ts +3 -0
  72. package/lib/utils/git.js +106 -0
  73. package/lib/utils/log.d.ts +8 -0
  74. package/lib/utils/log.js +25 -0
  75. package/lib/utils/npm-type.d.ts +2 -0
  76. package/lib/utils/npm-type.js +9 -0
  77. package/lib/utils/print-report.d.ts +3 -0
  78. package/lib/utils/print-report.js +109 -0
  79. package/package.json +97 -3
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 印客学院
4
+ Copyright (c) 2023 强仔 (modifications)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,191 @@
1
- # Temporary Holding Version
1
+ # encode-fe-lint
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `encode-fe-lint` 是[强仔 前端编码规范工程化](https://encode-studio-fe.github.io/fe-spec/)的配套 Lint 工具,可以为项目一键接入规范、一键扫描和修复规范问题,保障项目的编码规范和代码质量。
4
+
5
+ ## 背景
6
+
7
+ 我们引入了多个业界流行的 Linter,并根据规范内容定制了规则包,它们包括:
8
+
9
+ | 规范 | Lint 工具 | npm 包 |
10
+ | ----------------------------------------------------------------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------- |
11
+ | JavaScript 编码规范 <br/> TypeScript 编码规范 <br/> Node 编码规范 | [ESLint](https://eslint.org/) | [@qiangzaifan/eslint-config-encode](https://www.npmjs.com/package/@qiangzaifan/eslint-config-encode) |
12
+ | CSS 编码规范 | [stylelint](https://stylelint.io/) | [@qiangzaifan/stylelint-config-encode](https://www.npmjs.com/package/@qiangzaifan/stylelint-config-encode) |
13
+ | Git 规范 | [commitlint](https://commitlint.js.org/#/) | [@qiangzaifan/commitlint-config-encode](https://www.npmjs.com/package/@qiangzaifan/commitlint-config-encode) |
14
+ | 文档规范 | [markdownlint](https://github.com/DavidAnson/markdownlint) | [@qiangzaifan/markdownlint-config-encode](https://www.npmjs.com/package/@qiangzaifan/markdownlint-config-encode) |
15
+
16
+ 可以看到这些 `Linter` 和规则包众多且零散,全部安装它们会给项目增加十几个依赖,接入和升级成本都比较高。
17
+
18
+ `encode-fe-lint` 收敛屏蔽了这些依赖和配置细节,提供简单的 CLI 和 Node.js API,让项目能够一键接入、一键扫描、一键修复、一键升级,并为项目配置 git commit 卡口,降低项目接入规范的成本。
19
+
20
+ ## CLI 使用
21
+
22
+ ### 安装
23
+
24
+ 在终端执行:
25
+
26
+ ```bash
27
+ npm install @qiangzaifan/encode-fe-lint -g
28
+ ```
29
+
30
+ 安装完成后,可执行 `encode-fe-lint -h` 以验证安装成功。
31
+
32
+ ### 使用
33
+
34
+ #### `encode-fe-lint init`:一键接入
35
+
36
+ 在项目根目录执行 `encode-fe-lint init`,即可一键接入规范,为项目安装规范 `Lint` 所需的依赖和配置。
37
+
38
+ 具体会做以下事情:
39
+
40
+ - 安装各种依赖:包括 `Linter` 依赖,如 [ESLint](https://eslint.org/)、[stylelint](https://stylelint.io/)、[commitlint](https://commitlint.js.org/#/)、[markdownlint](https://github.com/DavidAnson/markdownlint) 等;配置依赖,如 [@qiangzaifan/eslint-config-encode](https://www.npmjs.com/package/@qiangzaifan/eslint-config-encode)、[@qiangzaifan/stylelint-config-encode](https://www.npmjs.com/package/@qiangzaifan/stylelint-config-encode)、[@qiangzaifan/commitlint-config-encode](https://www.npmjs.com/package/@qiangzaifan/commitlint-config-encode)、[@qiangzaifan/markdownlint-config-encode](https://www.npmjs.com/package/@qiangzaifan/markdownlint-config-encode) 等
41
+ - 写入各种配置文件,包括:
42
+ - `.eslintrc.js`、`.eslintignore`:ESLint 配置(继承 `@qiangzaifan/eslint-config-encode`)及黑名单文件
43
+ - `.stylelintrc.js`、`.stylelintignore`:stylelint 配置(继承 `@qiangzaifan/stylelint-config-encode`)及黑名单文件
44
+ - `commitlint.config.js`:commitlint 配置(继承 `@qiangzaifan/commitlint-config-encode`)
45
+ - `.markdownlint.json`、`.markdownlintignore`:`markdownlint` 配置及黑名单文件
46
+ - `.prettierrc.js`:符合规范的 [Prettier 配置](https://prettier.io/docs/en/configuration.html)
47
+ - `.editorconfig`:符合规范的 [editorconfig](https://editorconfig.org/)
48
+ - `.vscode/extensions.json`:写入规范相关的 [VSCode 插件推荐](https://code.visualstudio.com/docs/editor/extension-gallery#_workspace-recommended-extensions),包括 `ESLint`、`stylelint`、`markdownlint`、`prettier` 等
49
+ - `.vscode/settings.json`:写入规范相关的 [VSCode 设置](https://code.visualstudio.com/docs/getstarted/settings#_settings-file-locations),设置 `ESLint` 和 `stylelint` 插件的 `validate` 及**保存时自动运行 fix**,如果选择使用 `Prettier`,会同时将 `prettier-vscode` 插件设置为各前端语言的 defaultFormatter,并配置**保存时自动格式化**
50
+ - `encode-fe-lint.config.js`encode-fe-lint 包的一些配置,如启用的功能等
51
+ - 配置 git commit 卡口:使用 [husky](https://www.npmjs.com/package/husky) 设置代码提交卡口,在 git commit 时会运行 `encode-fe-lint commit-file-scan` 和 `encode-fe-lint commit-msg-scan` 分别对提交文件和提交信息进行规范检查。新项目使用 Husky 8,已有项目保留其 Husky 版本和现有钩子。`encode-fe-lint commit-file-scan` 默认仅对 error 问题卡口,如果你想对 warn 问题也卡口,可以增加 `--strict` 参数以开启严格模式
52
+
53
+ > 注 1:如果项目已经配置过 ESLint、stylelint 等 Linter,执行 `encode-fe-lint init` 将会提示存在冲突的依赖和配置,并在得到确认后进行覆盖:
54
+ >
55
+ > 注 2:如果项目的 .vscode/ 目录被 .gitignore 忽略,可以在拉取项目后单独执行 `encode-fe-lint init --vscode` 命令写入 `.vscode/extensions.json` 和 `.vscode/settings.json` 配置文件
56
+
57
+ #### `encode-fe-lint scan`:一键扫描
58
+
59
+ 在项目的根目录执行命令,即可扫描项目的规范问题;发现 error 或扫描失败时返回非零退出码,适合用于 CI:
60
+
61
+ 支持下列参数:
62
+
63
+ - `-q` `--quiet` 仅报告 error 级别的问题
64
+ - `-o` `--output-report` 输出扫描出的规范问题日志
65
+ - `-i` `--include <dirpath>` 指定要进行规范扫描的目录
66
+ - `--no-ignore` 忽略 eslint 的 ignore 配置文件和 ignore 规则
67
+
68
+ > 注 1:事实上,你可以在任意目录执行 `encode-fe-lint scan` `encode-fe-lint` 会根据文件类型、JSON 等特征嗅探项目类型。但我们还是推荐在执行过 `encode-fe-lint init` 的项目根目录执行 `encode-fe-lint scan`,以得到最准确的扫描结果。
69
+ >
70
+ > 注 2: `encode-fe-lint` 会根据项目内有无 eslint 和 stylelint 配置文件判断使用项目的配置文件还是 `encode-fe-lint` 默认配置进行扫描。若使用项目的,在未安装依赖时会帮其安装(执行 npm i)。若项目配置无法加载,命令会报告错误并返回非零退出码
71
+
72
+ #### `encode-fe-lint fix`:一键修复
73
+
74
+ 在项目的根目录执行命令,即可修复部分规范问题:
75
+
76
+ 支持下列参数:
77
+
78
+ - `-i` `--include <dirpath>` 指定要进行修复扫描的目录
79
+ - `--no-ignore` 忽略 eslint 的 ignore 配置文件和 ignore 规则
80
+
81
+ 修复后仍有 error 或工具运行失败时,命令返回非零退出码。请检查修复前后的代码,以免工具误修。
82
+
83
+ #### `encode-fe-lint commit-file-scan` 提交文件扫描
84
+
85
+ 在 git commit 时对提交文件进行规范问题扫描,需配合 git 的 pre-commit 钩子使用。
86
+
87
+ 支持下列参数:
88
+
89
+ - `-s` `--strict` 严格模式,对 warn 和 error 问题都卡口,默认仅对 error 问题卡口
90
+
91
+ #### `encode-fe-lint commit-msg-scan` 提交信息扫描
92
+
93
+ git commit 时对 commit message 的格式进行扫描(使用 commitlint),需配合 [husky](https://www.npmjs.com/package/husky) 的 commit-msg 钩子使用。Husky 5 及以上版本的钩子通过 `encode-fe-lint commit-msg-scan "$1"` 传入提交信息文件。
94
+
95
+ ## Node.js API 使用
96
+
97
+ ### 安装
98
+
99
+ ```bash
100
+ npm install @qiangzaifan/encode-fe-lint --save
101
+ ```
102
+
103
+ ### API
104
+
105
+ #### init:初始化
106
+
107
+ - encode-fe-lint.init(config):将项目一键接入规范,效果等同于 `encode-fe-lint init`
108
+
109
+ 示例:
110
+
111
+ ```js
112
+ const lint = require('@qiangzaifan/encode-fe-lint');
113
+
114
+ (async () => {
115
+ await lint.init({
116
+ cwd: process.cwd(),
117
+ eslintType: 'react',
118
+ enableESLint: true,
119
+ enableStylelint: true,
120
+ enableMarkdownlint: true,
121
+ enablePrettier: true,
122
+ disableNpmInstall: false,
123
+ });
124
+ })();
125
+ ```
126
+
127
+ config 参数如下:
128
+
129
+ | 参数 | 类型 | 默认值 | 说明 |
130
+ | ------------------ | ---------- | ------ | ------------------------------------------------------------------------------------------------------------------- |
131
+ | cwd | string | - | 项目绝对路径 |
132
+ | eslintType | ESLintType | - | 语言和框架类型,如果不配置,等同于 encode-fe-lint init,控制台会出现选择器,如果配置,控制台就不会出现选择器 |
133
+ | enableESLint | boolean | true | 是否启用 ESLint,如果不配置默认值为 true,即默认启用 ESLint |
134
+ | enableStylelint | boolean | - | 是否启用 stylelint,如果不配置,等同于 encode-fe-lint init,控制台会出现选择器,如果配置,控制台就不会出现选择器 |
135
+ | enableMarkdownlint | boolean | - | 是否启用 markdownlint,如果不配置,等同于 encode-fe-lint init,控制台会出现选择器,如果配置,控制台就不会出现选择器 |
136
+ | enablePrettier | boolean | - | 是否启用 Prettier |
137
+ | disableNpmInstall | boolean | false | 是否禁用自动在初始化完成后安装依赖 |
138
+
139
+ ##### ESLintType
140
+
141
+ - `index`: JavaScript 项目(未使用 React 和 Vue 的 JS 项目)
142
+ - `react`: JavaScript + React 项目
143
+ - `vue`: JavaScript + Vue 项目
144
+ - `typescript`: TypeScript 项目(未使用 React 和 Vue 的 TS 项目)
145
+ - `typescript/react`: TypeScript + React 项目
146
+ - `typescript/vue`: TypeScript + Vue 项目
147
+ - `es5`: ES5 及之前版本的 JavaScript 老项目
148
+
149
+ ## 配置
150
+
151
+ `encode-fe-lint` 基于一份配置进行扫描(但你也可以零配置使用),支持的配置参数有:
152
+
153
+ | 参数 | 类型 | 默认值 | 说明 |
154
+ | ------------------- | ----------------------- | ------ | ---------------------------------------------------------------------------------------------- |
155
+ | enableESLint | boolean | true | 是否启用 ESLint |
156
+ | enableStylelint | boolean | true | 是否启用 stylelint |
157
+ | enableMarkdownlint | boolean | true | 是否启用 markdownlint |
158
+ | enablePrettier | boolean | - | 是否启用 Prettier |
159
+ | eslintOptions | ESLint.Options | - | ESLint 配置项,若未设置将使用执行目录下或内置的默认 eslintrc 和 eslintignore 进行扫描 |
160
+ | stylelintOptions | stylelint.LinterOptions | - | stylelint 配置项,若未设置将使用执行目录下或内置的默认 stylelintrc 和 stylelintignore 进行扫描 |
161
+ | markdownlintOptions | markdownlint.Options | - | markdownlint 配置项,若未设置将使用执行目录下或内置的默认 markdownlint 配置文件进行扫描 |
162
+
163
+ `encode-fe-lint` 会读取执行目录下的 `encode-fe-lint.config.js` 作为配置文件。`encode-fe-lint init` 会在执行目录下新增如下的 `encode-fe-lint.config.js` 文件:
164
+
165
+ ```js
166
+ module.exports = {
167
+ enableESLint: true,
168
+ enableStylelint: true,
169
+ enableMarkdownlint: true,
170
+ enablePrettier: true,
171
+ };
172
+ ```
173
+
174
+ ## 常见问题
175
+
176
+ ### TypeScript 项目扫描性能问题
177
+
178
+ 如果你的 TS 项目 commit 卡口和 `encode-fe-lint scan` 运行时间很长,可以通过如下在 `.eslintrc.js` 中增加以下配置提升性能:
179
+
180
+ ```js
181
+ module.exports = {
182
+ parserOptions: {
183
+ project: [], // for lint performance
184
+ createDefaultProgram: false, // for lint performance
185
+ },
186
+ rules: {
187
+ '@typescript-eslint/dot-notation': 0, // for lint performance
188
+ '@typescript-eslint/restrict-plus-operands': 0, // for lint performance
189
+ },
190
+ };
191
+ ```
@@ -0,0 +1,3 @@
1
+ import type { InitOptions } from '../types';
2
+ declare const _default: (options: InitOptions) => Promise<void>;
3
+ export default _default;
@@ -0,0 +1,250 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
56
+ if (ar || !(i in from)) {
57
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
58
+ ar[i] = from[i];
59
+ }
60
+ }
61
+ return to.concat(ar || Array.prototype.slice.call(from));
62
+ };
63
+ var __importDefault = (this && this.__importDefault) || function (mod) {
64
+ return (mod && mod.__esModule) ? mod : { "default": mod };
65
+ };
66
+ Object.defineProperty(exports, "__esModule", { value: true });
67
+ var path_1 = __importDefault(require("path"));
68
+ var fs_extra_1 = __importDefault(require("fs-extra"));
69
+ var inquirer_1 = __importDefault(require("inquirer"));
70
+ var cross_spawn_1 = __importDefault(require("cross-spawn"));
71
+ var update_1 = __importDefault(require("./update"));
72
+ var npm_type_1 = __importDefault(require("../utils/npm-type"));
73
+ var log_1 = __importDefault(require("../utils/log"));
74
+ var conflict_resolve_1 = __importDefault(require("../utils/conflict-resolve"));
75
+ var generate_template_1 = __importDefault(require("../utils/generate-template"));
76
+ var configure_hooks_1 = __importDefault(require("../utils/configure-hooks"));
77
+ var constants_1 = require("../utils/constants");
78
+ var step = 0;
79
+ var chooseEslintType = function () { return __awaiter(void 0, void 0, void 0, function () {
80
+ var type;
81
+ return __generator(this, function (_a) {
82
+ switch (_a.label) {
83
+ case 0: return [4, inquirer_1.default.prompt({
84
+ type: 'list',
85
+ name: 'type',
86
+ message: "Step ".concat(++step, ". \u8BF7\u9009\u62E9\u9879\u76EE\u7684\u8BED\u8A00\uFF08JS/TS\uFF09\u548C\u6846\u67B6\uFF08React/Vue\uFF09\u7C7B\u578B\uFF1A"),
87
+ choices: constants_1.PROJECT_TYPES,
88
+ })];
89
+ case 1:
90
+ type = (_a.sent()).type;
91
+ return [2, type];
92
+ }
93
+ });
94
+ }); };
95
+ var chooseEnableStylelint = function (defaultValue) { return __awaiter(void 0, void 0, void 0, function () {
96
+ var enable;
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0: return [4, inquirer_1.default.prompt({
100
+ type: 'confirm',
101
+ name: 'enable',
102
+ message: "Step ".concat(++step, ". \u662F\u5426\u9700\u8981\u4F7F\u7528 stylelint\uFF08\u82E5\u6CA1\u6709\u6837\u5F0F\u6587\u4EF6\u5219\u4E0D\u9700\u8981\uFF09\uFF1A"),
103
+ default: defaultValue,
104
+ })];
105
+ case 1:
106
+ enable = (_a.sent()).enable;
107
+ return [2, enable];
108
+ }
109
+ });
110
+ }); };
111
+ var chooseEnableMarkdownLint = function () { return __awaiter(void 0, void 0, void 0, function () {
112
+ var enable;
113
+ return __generator(this, function (_a) {
114
+ switch (_a.label) {
115
+ case 0: return [4, inquirer_1.default.prompt({
116
+ type: 'confirm',
117
+ name: 'enable',
118
+ message: "Step ".concat(++step, ". \u662F\u5426\u9700\u8981\u4F7F\u7528 markdownlint\uFF08\u82E5\u6CA1\u6709 Markdown \u6587\u4EF6\u5219\u4E0D\u9700\u8981\uFF09\uFF1A"),
119
+ default: true,
120
+ })];
121
+ case 1:
122
+ enable = (_a.sent()).enable;
123
+ return [2, enable];
124
+ }
125
+ });
126
+ }); };
127
+ var chooseEnablePrettier = function () { return __awaiter(void 0, void 0, void 0, function () {
128
+ var enable;
129
+ return __generator(this, function (_a) {
130
+ switch (_a.label) {
131
+ case 0: return [4, inquirer_1.default.prompt({
132
+ type: 'confirm',
133
+ name: 'enable',
134
+ message: "Step ".concat(++step, ". \u662F\u5426\u9700\u8981\u4F7F\u7528 Prettier \u683C\u5F0F\u5316\u4EE3\u7801\uFF1A"),
135
+ default: true,
136
+ })];
137
+ case 1:
138
+ enable = (_a.sent()).enable;
139
+ return [2, enable];
140
+ }
141
+ });
142
+ }); };
143
+ exports.default = (function (options) { return __awaiter(void 0, void 0, void 0, function () {
144
+ var cwd, isTest, checkVersionUpdate, disableNpmInstall, config, pkgPath, pkg, _a, _b, _c, _d, npm, hasHusky, deps, result, logs;
145
+ return __generator(this, function (_e) {
146
+ switch (_e.label) {
147
+ case 0:
148
+ step = 0;
149
+ cwd = options.cwd || process.cwd();
150
+ isTest = process.env.NODE_ENV === 'test';
151
+ checkVersionUpdate = options.checkVersionUpdate || false;
152
+ disableNpmInstall = options.disableNpmInstall || false;
153
+ config = {};
154
+ pkgPath = path_1.default.resolve(cwd, 'package.json');
155
+ pkg = fs_extra_1.default.readJSONSync(pkgPath);
156
+ if (!(!isTest && checkVersionUpdate)) return [3, 2];
157
+ return [4, (0, update_1.default)(false)];
158
+ case 1:
159
+ _e.sent();
160
+ _e.label = 2;
161
+ case 2:
162
+ if (typeof options.enableESLint === 'boolean') {
163
+ config.enableESLint = options.enableESLint;
164
+ }
165
+ else {
166
+ config.enableESLint = true;
167
+ }
168
+ if (!(options.eslintType && constants_1.PROJECT_TYPES.find(function (choice) { return choice.value === options.eslintType; }))) return [3, 3];
169
+ config.eslintType = options.eslintType;
170
+ return [3, 5];
171
+ case 3:
172
+ _a = config;
173
+ return [4, chooseEslintType()];
174
+ case 4:
175
+ _a.eslintType = _e.sent();
176
+ _e.label = 5;
177
+ case 5:
178
+ if (!(typeof options.enableStylelint === 'boolean')) return [3, 6];
179
+ config.enableStylelint = options.enableStylelint;
180
+ return [3, 8];
181
+ case 6:
182
+ _b = config;
183
+ return [4, chooseEnableStylelint(!/node/.test(config.eslintType))];
184
+ case 7:
185
+ _b.enableStylelint = _e.sent();
186
+ _e.label = 8;
187
+ case 8:
188
+ if (!(typeof options.enableMarkdownlint === 'boolean')) return [3, 9];
189
+ config.enableMarkdownlint = options.enableMarkdownlint;
190
+ return [3, 11];
191
+ case 9:
192
+ _c = config;
193
+ return [4, chooseEnableMarkdownLint()];
194
+ case 10:
195
+ _c.enableMarkdownlint = _e.sent();
196
+ _e.label = 11;
197
+ case 11:
198
+ if (!(typeof options.enablePrettier === 'boolean')) return [3, 12];
199
+ config.enablePrettier = options.enablePrettier;
200
+ return [3, 14];
201
+ case 12:
202
+ _d = config;
203
+ return [4, chooseEnablePrettier()];
204
+ case 13:
205
+ _d.enablePrettier = _e.sent();
206
+ _e.label = 14;
207
+ case 14:
208
+ if (!!isTest) return [3, 17];
209
+ log_1.default.info("Step ".concat(++step, ". \u68C0\u67E5\u5E76\u5904\u7406\u9879\u76EE\u4E2D\u53EF\u80FD\u5B58\u5728\u7684\u4F9D\u8D56\u548C\u914D\u7F6E\u51B2\u7A81"));
210
+ return [4, (0, conflict_resolve_1.default)(cwd, options.rewriteConfig)];
211
+ case 15:
212
+ pkg = _e.sent();
213
+ log_1.default.success("Step ".concat(step, ". \u5DF2\u5B8C\u6210\u9879\u76EE\u4F9D\u8D56\u548C\u914D\u7F6E\u51B2\u7A81\u68C0\u67E5\u5904\u7406 :D"));
214
+ if (!!disableNpmInstall) return [3, 17];
215
+ log_1.default.info("Step ".concat(++step, ". \u5B89\u88C5\u4F9D\u8D56"));
216
+ return [4, npm_type_1.default];
217
+ case 16:
218
+ npm = _e.sent();
219
+ hasHusky = Boolean((pkg.dependencies || {}).husky || (pkg.devDependencies || {}).husky);
220
+ deps = hasHusky ? [constants_1.PKG_NAME] : [constants_1.PKG_NAME, 'husky@^8.0.3'];
221
+ result = cross_spawn_1.default.sync(npm, __spreadArray(['i', '-D'], __read(deps), false), { stdio: 'inherit', cwd: cwd });
222
+ if (result.error || result.status !== 0) {
223
+ throw result.error || new Error("".concat(npm, " install \u5931\u8D25\uFF08\u9000\u51FA\u7801 ").concat(result.status, "\uFF09"));
224
+ }
225
+ log_1.default.success("Step ".concat(step, ". \u5B89\u88C5\u4F9D\u8D56\u6210\u529F :D"));
226
+ _e.label = 17;
227
+ case 17:
228
+ pkg = fs_extra_1.default.readJSONSync(pkgPath);
229
+ if (!pkg.scripts) {
230
+ pkg.scripts = {};
231
+ }
232
+ if (!pkg.scripts["".concat(constants_1.BIN_NAME, "-scan")]) {
233
+ pkg.scripts["".concat(constants_1.BIN_NAME, "-scan")] = "".concat(constants_1.BIN_NAME, " scan");
234
+ }
235
+ if (!pkg.scripts["".concat(constants_1.BIN_NAME, "-fix")]) {
236
+ pkg.scripts["".concat(constants_1.BIN_NAME, "-fix")] = "".concat(constants_1.BIN_NAME, " fix");
237
+ }
238
+ log_1.default.info("Step ".concat(++step, ". \u914D\u7F6E git commit \u5361\u70B9"));
239
+ (0, configure_hooks_1.default)(cwd, pkg);
240
+ fs_extra_1.default.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
241
+ log_1.default.success("Step ".concat(step, ". \u914D\u7F6E git commit \u5361\u70B9\u6210\u529F :D"));
242
+ log_1.default.info("Step ".concat(++step, ". \u5199\u5165\u914D\u7F6E\u6587\u4EF6"));
243
+ (0, generate_template_1.default)(cwd, config);
244
+ log_1.default.success("Step ".concat(step, ". \u5199\u5165\u914D\u7F6E\u6587\u4EF6\u6210\u529F :D"));
245
+ logs = ["".concat(constants_1.PKG_NAME, " \u521D\u59CB\u5316\u5B8C\u6210 :D")].join('\r\n');
246
+ log_1.default.success(logs);
247
+ return [2];
248
+ }
249
+ });
250
+ }); });
@@ -0,0 +1,3 @@
1
+ import type { ScanOptions, ScanReport } from '../types';
2
+ declare const _default: (options: ScanOptions) => Promise<ScanReport>;
3
+ export default _default;
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var fs_extra_1 = __importDefault(require("fs-extra"));
54
+ var path_1 = __importDefault(require("path"));
55
+ var lints_1 = require("../lints");
56
+ var constants_1 = require("../utils/constants");
57
+ exports.default = (function (options) { return __awaiter(void 0, void 0, void 0, function () {
58
+ var cwd, fix, outputReport, scanConfig, readConfigFile, pkg, config, runErrors, results, e_1, eslintResults, e_2, stylelintResults, e_3, markdownlintResults, e_4, reportPath;
59
+ return __generator(this, function (_a) {
60
+ switch (_a.label) {
61
+ case 0:
62
+ cwd = options.cwd, fix = options.fix, outputReport = options.outputReport, scanConfig = options.config;
63
+ readConfigFile = function (pth) {
64
+ var localPath = path_1.default.resolve(cwd, pth);
65
+ return fs_extra_1.default.existsSync(localPath) ? require(localPath) : {};
66
+ };
67
+ pkg = readConfigFile('package.json');
68
+ config = scanConfig || readConfigFile("".concat(constants_1.BIN_NAME, ".config.js"));
69
+ runErrors = [];
70
+ results = [];
71
+ if (!(fix && config.enablePrettier !== false)) return [3, 4];
72
+ _a.label = 1;
73
+ case 1:
74
+ _a.trys.push([1, 3, , 4]);
75
+ return [4, (0, lints_1.doPrettier)(options)];
76
+ case 2:
77
+ _a.sent();
78
+ return [3, 4];
79
+ case 3:
80
+ e_1 = _a.sent();
81
+ runErrors.push(e_1);
82
+ return [3, 4];
83
+ case 4:
84
+ if (!(config.enableESLint !== false)) return [3, 8];
85
+ _a.label = 5;
86
+ case 5:
87
+ _a.trys.push([5, 7, , 8]);
88
+ return [4, (0, lints_1.doESLint)(__assign(__assign({}, options), { pkg: pkg, config: config }))];
89
+ case 6:
90
+ eslintResults = _a.sent();
91
+ results = results.concat(eslintResults);
92
+ return [3, 8];
93
+ case 7:
94
+ e_2 = _a.sent();
95
+ runErrors.push(e_2);
96
+ return [3, 8];
97
+ case 8:
98
+ if (!(config.enableStylelint !== false)) return [3, 12];
99
+ _a.label = 9;
100
+ case 9:
101
+ _a.trys.push([9, 11, , 12]);
102
+ return [4, (0, lints_1.doStylelint)(__assign(__assign({}, options), { pkg: pkg, config: config }))];
103
+ case 10:
104
+ stylelintResults = _a.sent();
105
+ results = results.concat(stylelintResults);
106
+ return [3, 12];
107
+ case 11:
108
+ e_3 = _a.sent();
109
+ runErrors.push(e_3);
110
+ return [3, 12];
111
+ case 12:
112
+ if (!(config.enableMarkdownlint !== false)) return [3, 16];
113
+ _a.label = 13;
114
+ case 13:
115
+ _a.trys.push([13, 15, , 16]);
116
+ return [4, (0, lints_1.doMarkdownlint)(__assign(__assign({}, options), { pkg: pkg, config: config }))];
117
+ case 14:
118
+ markdownlintResults = _a.sent();
119
+ results = results.concat(markdownlintResults);
120
+ return [3, 16];
121
+ case 15:
122
+ e_4 = _a.sent();
123
+ runErrors.push(e_4);
124
+ return [3, 16];
125
+ case 16:
126
+ if (!outputReport) return [3, 18];
127
+ reportPath = path_1.default.resolve(cwd, "./".concat(constants_1.BIN_NAME, "-report.json"));
128
+ return [4, fs_extra_1.default.outputFile(reportPath, JSON.stringify(results, null, 2))];
129
+ case 17:
130
+ _a.sent();
131
+ _a.label = 18;
132
+ case 18: return [2, {
133
+ results: results,
134
+ errorCount: results.reduce(function (count, _a) {
135
+ var errorCount = _a.errorCount;
136
+ return count + errorCount;
137
+ }, 0),
138
+ warningCount: results.reduce(function (count, _a) {
139
+ var warningCount = _a.warningCount;
140
+ return count + warningCount;
141
+ }, 0),
142
+ runErrors: runErrors,
143
+ }];
144
+ }
145
+ });
146
+ }); });
@@ -0,0 +1,2 @@
1
+ declare const _default: (install?: boolean) => Promise<void>;
2
+ export default _default;