@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
@@ -0,0 +1,77 @@
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
+ exports.doStylelint = void 0;
54
+ var stylelint_1 = __importDefault(require("stylelint"));
55
+ var constants_1 = require("../../utils/constants");
56
+ var get_files_1 = __importDefault(require("../../utils/get-files"));
57
+ var formatStylelintResults_1 = require("./formatStylelintResults");
58
+ var getStylelintConfig_1 = require("./getStylelintConfig");
59
+ function doStylelint(options) {
60
+ return __awaiter(this, void 0, void 0, function () {
61
+ var files, data;
62
+ return __generator(this, function (_a) {
63
+ switch (_a.label) {
64
+ case 0: return [4, (0, get_files_1.default)(options, constants_1.STYLELINT_FILE_EXT, constants_1.STYLELINT_IGNORE_PATTERN)];
65
+ case 1:
66
+ files = _a.sent();
67
+ if (files.length === 0)
68
+ return [2, []];
69
+ return [4, stylelint_1.default.lint(__assign(__assign({}, (0, getStylelintConfig_1.getStylelintConfig)(options, options.pkg, options.config)), { files: files }))];
70
+ case 2:
71
+ data = _a.sent();
72
+ return [2, (0, formatStylelintResults_1.formatStylelintResults)(data.results, options.quiet)];
73
+ }
74
+ });
75
+ });
76
+ }
77
+ exports.doStylelint = doStylelint;
@@ -0,0 +1,3 @@
1
+ import { LintResult } from 'stylelint';
2
+ import type { ScanResult } from '../../types';
3
+ export declare function formatStylelintResults(results: LintResult[], quiet: boolean): ScanResult[];
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatStylelintResults = void 0;
4
+ var getStylelintDocUrl_1 = require("./getStylelintDocUrl");
5
+ function formatStylelintResults(results, quiet) {
6
+ return results.map(function (_a) {
7
+ var source = _a.source, warnings = _a.warnings;
8
+ var errorCount = 0;
9
+ var warningCount = 0;
10
+ var messages = warnings
11
+ .filter(function (item) { return !quiet || item.severity === 'error'; })
12
+ .map(function (item) {
13
+ var _a = item.line, line = _a === void 0 ? 0 : _a, _b = item.column, column = _b === void 0 ? 0 : _b, rule = item.rule, severity = item.severity, text = item.text;
14
+ if (severity === 'error') {
15
+ errorCount++;
16
+ }
17
+ else {
18
+ warningCount++;
19
+ }
20
+ return {
21
+ line: line,
22
+ column: column,
23
+ rule: rule,
24
+ url: (0, getStylelintDocUrl_1.getStylelintRuleDocUrl)(rule),
25
+ message: text.replace(/([^ ])\.$/u, '$1').replace(new RegExp("\\(".concat(rule, "\\)")), ''),
26
+ errored: severity === 'error',
27
+ };
28
+ });
29
+ return {
30
+ filePath: source,
31
+ messages: messages,
32
+ errorCount: errorCount,
33
+ warningCount: warningCount,
34
+ fixableErrorCount: 0,
35
+ fixableWarningCount: 0,
36
+ };
37
+ });
38
+ }
39
+ exports.formatStylelintResults = formatStylelintResults;
@@ -0,0 +1,3 @@
1
+ import { LinterOptions } from 'stylelint';
2
+ import type { Config, PKG, ScanOptions } from '../../types';
3
+ export declare function getStylelintConfig(opts: ScanOptions, pkg: PKG, config: Config): LinterOptions;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getStylelintConfig = void 0;
7
+ var fs_extra_1 = __importDefault(require("fs-extra"));
8
+ var glob_1 = __importDefault(require("glob"));
9
+ var path_1 = __importDefault(require("path"));
10
+ var constants_1 = require("../../utils/constants");
11
+ function getStylelintConfig(opts, pkg, config) {
12
+ var cwd = opts.cwd, fix = opts.fix;
13
+ if (config.enableStylelint === false)
14
+ return {};
15
+ var lintConfig = {
16
+ fix: Boolean(fix),
17
+ allowEmptyInput: true,
18
+ };
19
+ if (config.stylelintOptions) {
20
+ Object.assign(lintConfig, config.stylelintOptions);
21
+ }
22
+ else {
23
+ var lintConfigFiles = glob_1.default.sync('.stylelintrc?(.@(js|yaml|yml|json))', { cwd: cwd });
24
+ if (lintConfigFiles.length === 0 && !pkg.stylelint) {
25
+ lintConfig.config = {
26
+ extends: require.resolve('@qiangzaifan/stylelint-config-encode'),
27
+ };
28
+ }
29
+ var ignoreFilePath = path_1.default.resolve(cwd, '.stylelintignore');
30
+ if (!fs_extra_1.default.existsSync(ignoreFilePath)) {
31
+ lintConfig.ignorePattern = constants_1.STYLELINT_IGNORE_PATTERN;
32
+ }
33
+ }
34
+ return lintConfig;
35
+ }
36
+ exports.getStylelintConfig = getStylelintConfig;
@@ -0,0 +1 @@
1
+ export declare function getStylelintRuleDocUrl(rule: string): string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStylelintRuleDocUrl = void 0;
4
+ function getStylelintRuleDocUrl(rule) {
5
+ var match = rule.match(/^@scss\/(\S+)$/);
6
+ if (match) {
7
+ return "https://github.com/kristerkari/stylelint-scss/tree/master/src/rules/".concat(match[1]);
8
+ }
9
+ if (rule !== 'CssSyntaxError')
10
+ return "https://stylelint.io/user-guide/rules/list/".concat(rule);
11
+ return '';
12
+ }
13
+ exports.getStylelintRuleDocUrl = getStylelintRuleDocUrl;
@@ -0,0 +1,3 @@
1
+ export * from './getStylelintConfig';
2
+ export * from './formatStylelintResults';
3
+ export * from './doStylelint';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./getStylelintConfig"), exports);
18
+ __exportStar(require("./formatStylelintResults"), exports);
19
+ __exportStar(require("./doStylelint"), exports);
package/lib/types.d.ts ADDED
@@ -0,0 +1,73 @@
1
+ import { ESLint } from 'eslint';
2
+ import stylelint from 'stylelint';
3
+ import markdownlint from 'markdownlint';
4
+ export interface PKG {
5
+ eslintConfig?: any;
6
+ eslintIgnore?: string[];
7
+ stylelint?: any;
8
+ peerDependencies?: Record<string, string>;
9
+ devDependencies?: Record<string, string>;
10
+ dependencies?: Record<string, string>;
11
+ [key: string]: any;
12
+ }
13
+ export interface Config {
14
+ enableESLint?: boolean;
15
+ enableStylelint?: boolean;
16
+ enableMarkdownlint?: boolean;
17
+ enablePrettier?: boolean;
18
+ eslintOptions?: ESLint.Options;
19
+ stylelintOptions?: stylelint.LinterOptions;
20
+ markdownlintOptions?: markdownlint.Options;
21
+ }
22
+ export interface ScanOptions {
23
+ cwd: string;
24
+ include: string;
25
+ files?: string[];
26
+ quiet?: boolean;
27
+ ignore?: boolean;
28
+ fix?: boolean;
29
+ outputReport?: boolean;
30
+ config?: Config;
31
+ }
32
+ export interface ScanResult {
33
+ filePath: string;
34
+ errorCount: number;
35
+ warningCount: number;
36
+ fixableErrorCount: number;
37
+ fixableWarningCount: number;
38
+ messages: Array<{
39
+ line: number;
40
+ column: number;
41
+ rule: string;
42
+ url: string;
43
+ message: string;
44
+ errored: boolean;
45
+ }>;
46
+ }
47
+ export interface ScanReport {
48
+ results: ScanResult[];
49
+ errorCount: number;
50
+ warningCount: number;
51
+ runErrors: Error[];
52
+ }
53
+ export interface InitOptions {
54
+ cwd: string;
55
+ checkVersionUpdate: boolean;
56
+ rewriteConfig?: boolean;
57
+ eslintType?: string;
58
+ enableESLint?: boolean;
59
+ enableStylelint?: boolean;
60
+ enableMarkdownlint?: boolean;
61
+ enablePrettier?: boolean;
62
+ disableNpmInstall?: boolean;
63
+ }
64
+ export interface IGetLintConfig {
65
+ (options: ScanOptions, pkg: PKG, config: Config): ESLint.Options;
66
+ (options: ScanOptions, pkg: PKG, config: Config): stylelint.LinterOptions;
67
+ (options: ScanOptions, pkg: PKG, config: Config): markdownlint.Options;
68
+ }
69
+ export interface IFormatResults {
70
+ (results: ESLint.LintResult[], quiet: boolean): ScanResult[];
71
+ (results: stylelint.LintResult[], quiet: boolean): ScanResult[];
72
+ (results: markdownlint.LintResults, quiet: boolean): ScanResult[];
73
+ }
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import type { PKG } from '../types';
2
+ declare const _default: (cwd: string, pkg: PKG) => void;
3
+ export default _default;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var path_1 = __importDefault(require("path"));
7
+ var fs_extra_1 = __importDefault(require("fs-extra"));
8
+ var cross_spawn_1 = __importDefault(require("cross-spawn"));
9
+ var log_1 = __importDefault(require("./log"));
10
+ var constants_1 = require("./constants");
11
+ var appendCommand = function (existing, command) {
12
+ if (existing === void 0) { existing = ''; }
13
+ if (existing.includes(command))
14
+ return existing;
15
+ return existing ? "".concat(existing, " && ").concat(command) : command;
16
+ };
17
+ var writeHook = function (cwd, hook, command, major) {
18
+ var file = path_1.default.resolve(cwd, '.husky', hook);
19
+ var content = fs_extra_1.default.existsSync(file) ? fs_extra_1.default.readFileSync(file, 'utf8') : '';
20
+ if (!content) {
21
+ content = '#!/usr/bin/env sh\n';
22
+ if (major < 9)
23
+ content += '. "$(dirname -- "$0")/_/husky.sh"\n';
24
+ }
25
+ if (!content.includes(command)) {
26
+ content = "".concat(content.trimEnd(), "\n\n").concat(command, "\n");
27
+ fs_extra_1.default.outputFileSync(file, content, { mode: 493 });
28
+ }
29
+ fs_extra_1.default.chmodSync(file, 493);
30
+ };
31
+ exports.default = (function (cwd, pkg) {
32
+ var installedPath = path_1.default.resolve(cwd, 'node_modules/husky/package.json');
33
+ var installed = fs_extra_1.default.existsSync(installedPath) ? fs_extra_1.default.readJSONSync(installedPath) : undefined;
34
+ var declared = (pkg.devDependencies || {}).husky || (pkg.dependencies || {}).husky;
35
+ var version = (installed && installed.version) || declared || '8';
36
+ var match = String(version).match(/\d+/);
37
+ var major = match ? Number(match[0]) : 8;
38
+ if (major < 5) {
39
+ if (!pkg.husky)
40
+ pkg.husky = {};
41
+ if (!pkg.husky.hooks)
42
+ pkg.husky.hooks = {};
43
+ pkg.husky.hooks['pre-commit'] = appendCommand(pkg.husky.hooks['pre-commit'], "".concat(constants_1.BIN_NAME, " commit-file-scan"));
44
+ pkg.husky.hooks['commit-msg'] = appendCommand(pkg.husky.hooks['commit-msg'], "".concat(constants_1.BIN_NAME, " commit-msg-scan"));
45
+ return;
46
+ }
47
+ if (!declared) {
48
+ if (!pkg.devDependencies)
49
+ pkg.devDependencies = {};
50
+ pkg.devDependencies.husky = '^8.0.3';
51
+ }
52
+ var install = major >= 9 ? 'husky' : 'husky install';
53
+ if (!/\bhusky(?:\s+install)?\b/.test(pkg.scripts.prepare || '')) {
54
+ pkg.scripts.prepare = appendCommand(pkg.scripts.prepare, install);
55
+ }
56
+ var command = "./node_modules/.bin/".concat(constants_1.BIN_NAME);
57
+ writeHook(cwd, 'pre-commit', "".concat(command, " commit-file-scan"), major);
58
+ writeHook(cwd, 'commit-msg', "".concat(command, " commit-msg-scan \"$1\""), major);
59
+ if (!fs_extra_1.default.existsSync(path_1.default.resolve(cwd, '.git'))) {
60
+ log_1.default.warn('未找到项目根目录的 .git;初始化 Git 并安装依赖后,请执行 npm run prepare。');
61
+ return;
62
+ }
63
+ if (!installed) {
64
+ log_1.default.warn('未找到 Husky;安装项目依赖后,请执行 npm run prepare。');
65
+ return;
66
+ }
67
+ var configured = cross_spawn_1.default.sync('git', ['config', '--get', 'core.hooksPath'], { cwd: cwd, encoding: 'utf8' });
68
+ var currentPath = configured.status === 0 ? configured.stdout.trim() : '';
69
+ if (currentPath && path_1.default.resolve(cwd, currentPath) !== path_1.default.resolve(cwd, '.husky')) {
70
+ log_1.default.warn("Git \u5DF2\u4F7F\u7528 ".concat(currentPath, " \u4F5C\u4E3A hooks \u76EE\u5F55\uFF0C\u672A\u8986\u76D6\u8BE5\u8BBE\u7F6E\uFF1B\u8BF7\u624B\u52A8\u63A5\u5165 .husky \u4E2D\u7684\u68C0\u67E5\u3002"));
71
+ return;
72
+ }
73
+ var bin = typeof installed.bin === 'string' ? installed.bin : installed.bin.husky;
74
+ var args = [path_1.default.resolve(path_1.default.dirname(installedPath), bin)];
75
+ if (major < 9)
76
+ args.push('install');
77
+ var result = cross_spawn_1.default.sync(process.execPath, args, { cwd: cwd, stdio: 'inherit' });
78
+ if (result.error || result.status !== 0) {
79
+ throw result.error || new Error("Husky \u5B89\u88C5\u5931\u8D25\uFF08\u9000\u51FA\u7801 ".concat(result.status, "\uFF09"));
80
+ }
81
+ });
@@ -0,0 +1,3 @@
1
+ import type { PKG } from '../types';
2
+ declare const _default: (cwd: string, rewriteConfig?: boolean) => Promise<PKG>;
3
+ export default _default;
@@ -0,0 +1,174 @@
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 __values = (this && this.__values) || function(o) {
39
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
+ if (m) return m.call(o);
41
+ if (o && typeof o.length === "number") return {
42
+ next: function () {
43
+ if (o && i >= o.length) o = void 0;
44
+ return { value: o && o[i++], done: !o };
45
+ }
46
+ };
47
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
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 path_1 = __importDefault(require("path"));
54
+ var fs_extra_1 = __importDefault(require("fs-extra"));
55
+ var glob_1 = __importDefault(require("glob"));
56
+ var inquirer_1 = __importDefault(require("inquirer"));
57
+ var log_1 = __importDefault(require("./log"));
58
+ var constants_1 = require("./constants");
59
+ var packageNamesToRemove = [
60
+ '@babel/eslint-parser',
61
+ '@commitlint/cli',
62
+ '@iceworks/spec',
63
+ 'babel-eslint',
64
+ 'eslint',
65
+ 'markdownlint',
66
+ 'prettier',
67
+ 'stylelint',
68
+ 'tslint',
69
+ ];
70
+ var packagePrefixesToRemove = [
71
+ '@commitlint/',
72
+ '@typescript-eslint/',
73
+ 'eslint-',
74
+ 'stylelint-',
75
+ 'markdownlint-',
76
+ 'commitlint-',
77
+ ];
78
+ var checkUselessConfig = function (cwd) {
79
+ return []
80
+ .concat(glob_1.default.sync('.eslintrc?(.@(yaml|yml|json))', { cwd: cwd }))
81
+ .concat(glob_1.default.sync('.stylelintrc?(.@(yaml|yml|json))', { cwd: cwd }))
82
+ .concat(glob_1.default.sync('.markdownlint@(rc|.@(yaml|yml|jsonc))', { cwd: cwd }))
83
+ .concat(glob_1.default.sync('.prettierrc?(.@(cjs|config.js|config.cjs|yaml|yml|json|json5|toml))', { cwd: cwd }))
84
+ .concat(glob_1.default.sync('tslint.@(yaml|yml|json)', { cwd: cwd }))
85
+ .concat(glob_1.default.sync('.kylerc?(.@(yaml|yml|json))', { cwd: cwd }));
86
+ };
87
+ var checkReWriteConfig = function (cwd) {
88
+ return glob_1.default
89
+ .sync('**/*.ejs', { cwd: path_1.default.resolve(__dirname, '../config') })
90
+ .map(function (name) { return name.replace(/^_/, '.').replace(/\.ejs$/, ''); })
91
+ .filter(function (filename) { return fs_extra_1.default.existsSync(path_1.default.resolve(cwd, filename)); });
92
+ };
93
+ exports.default = (function (cwd, rewriteConfig) { return __awaiter(void 0, void 0, void 0, function () {
94
+ var pkgPath, pkg, dependencies, willRemovePackage, uselessConfig, reWriteConfig, willChangeCount, isOverWrite, uselessConfig_1, uselessConfig_1_1, name_1, willRemovePackage_1, willRemovePackage_1_1, name_2;
95
+ var e_1, _a, e_2, _b;
96
+ return __generator(this, function (_c) {
97
+ switch (_c.label) {
98
+ case 0:
99
+ pkgPath = path_1.default.resolve(cwd, 'package.json');
100
+ pkg = fs_extra_1.default.readJSONSync(pkgPath);
101
+ dependencies = [].concat(Object.keys(pkg.dependencies || {}), Object.keys(pkg.devDependencies || []));
102
+ willRemovePackage = dependencies.filter(function (name) {
103
+ return packageNamesToRemove.includes(name) ||
104
+ packagePrefixesToRemove.some(function (prefix) { return name.startsWith(prefix); });
105
+ });
106
+ uselessConfig = checkUselessConfig(cwd);
107
+ reWriteConfig = checkReWriteConfig(cwd);
108
+ willChangeCount = willRemovePackage.length + uselessConfig.length + reWriteConfig.length;
109
+ if (!(willChangeCount > 0)) return [3, 3];
110
+ log_1.default.warn("\u68C0\u6D4B\u5230\u9879\u76EE\u4E2D\u5B58\u5728\u53EF\u80FD\u4E0E ".concat(constants_1.PKG_NAME, " \u51B2\u7A81\u7684\u4F9D\u8D56\u548C\u914D\u7F6E\uFF0C\u4E3A\u4FDD\u8BC1\u6B63\u5E38\u8FD0\u884C\u5C06"));
111
+ if (willRemovePackage.length > 0) {
112
+ log_1.default.warn('删除以下依赖:');
113
+ log_1.default.warn(JSON.stringify(willRemovePackage, null, 2));
114
+ }
115
+ if (uselessConfig.length > 0) {
116
+ log_1.default.warn('删除以下配置文件:');
117
+ log_1.default.warn(JSON.stringify(uselessConfig, null, 2));
118
+ }
119
+ if (reWriteConfig.length > 0) {
120
+ log_1.default.warn('覆盖以下配置文件:');
121
+ log_1.default.warn(JSON.stringify(reWriteConfig, null, 2));
122
+ }
123
+ if (!(typeof rewriteConfig === 'undefined')) return [3, 2];
124
+ return [4, inquirer_1.default.prompt({
125
+ type: 'confirm',
126
+ name: 'isOverWrite',
127
+ message: '请确认是否继续:',
128
+ })];
129
+ case 1:
130
+ isOverWrite = (_c.sent()).isOverWrite;
131
+ if (!isOverWrite)
132
+ process.exit(0);
133
+ return [3, 3];
134
+ case 2:
135
+ if (!rewriteConfig) {
136
+ throw new Error('存在冲突配置,已取消初始化');
137
+ }
138
+ _c.label = 3;
139
+ case 3:
140
+ try {
141
+ for (uselessConfig_1 = __values(uselessConfig), uselessConfig_1_1 = uselessConfig_1.next(); !uselessConfig_1_1.done; uselessConfig_1_1 = uselessConfig_1.next()) {
142
+ name_1 = uselessConfig_1_1.value;
143
+ fs_extra_1.default.removeSync(path_1.default.resolve(cwd, name_1));
144
+ }
145
+ }
146
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
147
+ finally {
148
+ try {
149
+ if (uselessConfig_1_1 && !uselessConfig_1_1.done && (_a = uselessConfig_1.return)) _a.call(uselessConfig_1);
150
+ }
151
+ finally { if (e_1) throw e_1.error; }
152
+ }
153
+ delete pkg.eslintConfig;
154
+ delete pkg.eslintIgnore;
155
+ delete pkg.stylelint;
156
+ try {
157
+ for (willRemovePackage_1 = __values(willRemovePackage), willRemovePackage_1_1 = willRemovePackage_1.next(); !willRemovePackage_1_1.done; willRemovePackage_1_1 = willRemovePackage_1.next()) {
158
+ name_2 = willRemovePackage_1_1.value;
159
+ delete (pkg.dependencies || {})[name_2];
160
+ delete (pkg.devDependencies || {})[name_2];
161
+ }
162
+ }
163
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
164
+ finally {
165
+ try {
166
+ if (willRemovePackage_1_1 && !willRemovePackage_1_1.done && (_b = willRemovePackage_1.return)) _b.call(willRemovePackage_1);
167
+ }
168
+ finally { if (e_2) throw e_2.error; }
169
+ }
170
+ fs_extra_1.default.writeFileSync(path_1.default.resolve(cwd, 'package.json'), JSON.stringify(pkg, null, 2), 'utf8');
171
+ return [2, pkg];
172
+ }
173
+ });
174
+ }); });
@@ -0,0 +1,19 @@
1
+ export declare enum UNICODE {
2
+ success = "\u2714",
3
+ failure = "\u2716"
4
+ }
5
+ export declare const PKG_NAME: string;
6
+ export declare const BIN_NAME: string;
7
+ export declare const PKG_VERSION: string;
8
+ export declare const PROJECT_TYPES: Array<{
9
+ name: string;
10
+ value: string;
11
+ }>;
12
+ export declare const ESLINT_FILE_EXT: string[];
13
+ export declare const ESLINT_IGNORE_PATTERN: string[];
14
+ export declare const STYLELINT_FILE_EXT: string[];
15
+ export declare const STYLELINT_IGNORE_PATTERN: string[];
16
+ export declare const MARKDOWN_LINT_FILE_EXT: string[];
17
+ export declare const MARKDOWN_LINT_IGNORE_PATTERN: string[];
18
+ export declare const PRETTIER_FILE_EXT: string[];
19
+ export declare const PRETTIER_IGNORE_PATTERN: string[];