ko-lints 2.0.0 → 3.1.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.
- package/index.d.ts +3 -1
- package/lib/eslint/index.js +26 -0
- package/lib/eslint/parser.js +52 -0
- package/lib/factory/parser.js +29 -0
- package/lib/factory/runner.js +92 -0
- package/lib/index.js +0 -1
- package/lib/prettier/index.js +26 -0
- package/lib/prettier/parser.js +49 -0
- package/lib/stylelint/index.js +26 -0
- package/lib/{stylelint.js → stylelint/parser.js} +10 -23
- package/lib/threads/Pool.js +57 -0
- package/lib/threads/Worker.js +22 -0
- package/lib/threads/index.js +16 -0
- package/package.json +4 -4
- package/lib/eslint.js +0 -70
- package/lib/factory.js +0 -57
- package/lib/prettier.js +0 -73
package/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type IOpts = {
|
|
|
4
4
|
write: boolean;
|
|
5
5
|
configPath: string;
|
|
6
6
|
patterns: Pattern[];
|
|
7
|
+
concurrency?: boolean;
|
|
8
|
+
concurrentNumber?: number;
|
|
7
9
|
};
|
|
8
10
|
|
|
9
11
|
export type IKeys = 'eslint' | 'prettier' | 'stylelint';
|
|
@@ -13,7 +15,7 @@ declare class Lints {
|
|
|
13
15
|
private opts;
|
|
14
16
|
state: 0 | 1;
|
|
15
17
|
constructor(opts: IOpts);
|
|
16
|
-
run(key: IKeys): Promise<
|
|
18
|
+
run(key: IKeys): Promise<string[]>;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export default Lints;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
const parser_1 = __importDefault(require("./parser"));
|
|
7
|
+
const runner_1 = __importDefault(require("../factory/runner"));
|
|
8
|
+
class ESLintRunner extends runner_1.default {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
const { configPath, write } = opts;
|
|
11
|
+
const parser = new parser_1.default({
|
|
12
|
+
configPath,
|
|
13
|
+
write,
|
|
14
|
+
name: ESLintRunner.NAME,
|
|
15
|
+
});
|
|
16
|
+
const childOpts = {
|
|
17
|
+
parser,
|
|
18
|
+
name: ESLintRunner.NAME,
|
|
19
|
+
ignoreFiles: ESLintRunner.IGNORE_FILES,
|
|
20
|
+
};
|
|
21
|
+
super(opts, childOpts);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
ESLintRunner.IGNORE_FILES = ['.eslintignore'];
|
|
25
|
+
ESLintRunner.NAME = 'eslint';
|
|
26
|
+
exports.default = ESLintRunner;
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
const ko_lint_config_1 = require("ko-lint-config");
|
|
7
|
+
const parser_1 = __importDefault(require("../factory/parser"));
|
|
8
|
+
class ESLintParser extends parser_1.default {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
super();
|
|
11
|
+
this.opts = opts;
|
|
12
|
+
this.generateConfig();
|
|
13
|
+
this.initInstance();
|
|
14
|
+
}
|
|
15
|
+
initInstance() {
|
|
16
|
+
const { write } = this.opts;
|
|
17
|
+
this.eslintInstance = new ko_lint_config_1.eslint.ESLint({
|
|
18
|
+
fix: write,
|
|
19
|
+
overrideConfig: this.config,
|
|
20
|
+
useEslintrc: false,
|
|
21
|
+
extensions: ESLintParser.EXTENSIONS,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async format(file) {
|
|
25
|
+
const formatter = await this.eslintInstance.loadFormatter();
|
|
26
|
+
let resultText = '';
|
|
27
|
+
try {
|
|
28
|
+
const result = await this.eslintInstance.lintFiles(file);
|
|
29
|
+
if (result[0].errorCount) {
|
|
30
|
+
resultText = formatter.format(result);
|
|
31
|
+
}
|
|
32
|
+
return resultText;
|
|
33
|
+
}
|
|
34
|
+
catch (ex) {
|
|
35
|
+
console.log(ex);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
generateConfig() {
|
|
40
|
+
if (this.opts.configPath) {
|
|
41
|
+
this.config = this.getConfigFromFile(this.opts.configPath);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const localConfigPath = this.detectLocalRunnerConfig(this.opts.name);
|
|
45
|
+
if (localConfigPath) {
|
|
46
|
+
this.config = this.getConfigFromFile(localConfigPath);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
ESLintParser.EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
|
|
52
|
+
exports.default = ESLintParser;
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
const path_1 = require("path");
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const assert_1 = __importDefault(require("assert"));
|
|
9
|
+
class LintParserFactory {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.cwd = process.cwd();
|
|
12
|
+
}
|
|
13
|
+
getConfigFromFile(filepath) {
|
|
14
|
+
(0, assert_1.default)((0, path_1.isAbsolute)(filepath), 'only accept absolute config filepath');
|
|
15
|
+
return require(filepath);
|
|
16
|
+
}
|
|
17
|
+
detectLocalRunnerConfig(name) {
|
|
18
|
+
const files = (0, fs_1.readdirSync)(this.cwd).filter(path => !(0, fs_1.statSync)(path).isDirectory());
|
|
19
|
+
let ret = '';
|
|
20
|
+
for (let file of files) {
|
|
21
|
+
if (file.includes(name) && !file.includes('ignore')) {
|
|
22
|
+
ret = file;
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return ret ? (0, path_1.join)(this.cwd, ret) : ret;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.default = LintParserFactory;
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
const path_1 = require("path");
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const os_1 = require("os");
|
|
9
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
10
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
11
|
+
const threads_1 = __importDefault(require("../threads"));
|
|
12
|
+
class LintRunnerFactory {
|
|
13
|
+
constructor(opts, childOpts) {
|
|
14
|
+
this.cwd = process.cwd();
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
this.childOpts = childOpts;
|
|
17
|
+
this.parser = childOpts.parser;
|
|
18
|
+
}
|
|
19
|
+
async getEntries(patterns, ignoreFiles) {
|
|
20
|
+
return (0, fast_glob_1.default)(patterns, {
|
|
21
|
+
dot: true,
|
|
22
|
+
ignore: this.getIgnorePatterns(...ignoreFiles),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
getIgnorePatterns(...ignoreFiles) {
|
|
26
|
+
return ['.gitignore', ...ignoreFiles]
|
|
27
|
+
.map(fileName => {
|
|
28
|
+
const filePath = (0, path_1.join)(this.cwd, fileName);
|
|
29
|
+
if ((0, fs_1.existsSync)(filePath)) {
|
|
30
|
+
return (0, fs_1.readFileSync)(filePath, 'utf-8')
|
|
31
|
+
.split('\n')
|
|
32
|
+
.filter(str => str && !str.startsWith('#'));
|
|
33
|
+
}
|
|
34
|
+
return [];
|
|
35
|
+
})
|
|
36
|
+
.reduce((acc, current) => {
|
|
37
|
+
current.forEach(p => {
|
|
38
|
+
if (!acc.includes(p)) {
|
|
39
|
+
acc.push(p);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return acc;
|
|
43
|
+
}, []);
|
|
44
|
+
}
|
|
45
|
+
getConcurrentNumber(num) {
|
|
46
|
+
return num ? num : (0, os_1.cpus)().length;
|
|
47
|
+
}
|
|
48
|
+
async run(entries) {
|
|
49
|
+
let ret;
|
|
50
|
+
const { concurrency } = this.opts;
|
|
51
|
+
if (concurrency) {
|
|
52
|
+
ret = await this.runInConcurrencyMode(entries);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
ret = await this.runInNormalMode(entries);
|
|
56
|
+
}
|
|
57
|
+
return ret;
|
|
58
|
+
}
|
|
59
|
+
async runInConcurrencyMode(entries) {
|
|
60
|
+
const { concurrentNumber, write, configPath } = this.opts;
|
|
61
|
+
const threads = new threads_1.default({
|
|
62
|
+
entries,
|
|
63
|
+
concurrentNumber: this.getConcurrentNumber(concurrentNumber),
|
|
64
|
+
write,
|
|
65
|
+
configPath,
|
|
66
|
+
name: this.childOpts.name,
|
|
67
|
+
});
|
|
68
|
+
return threads.batch();
|
|
69
|
+
}
|
|
70
|
+
async runInNormalMode(entries) {
|
|
71
|
+
const pList = entries.map(async (file) => await this.parser.format(file));
|
|
72
|
+
const result = await Promise.all(pList);
|
|
73
|
+
return result.filter(Boolean);
|
|
74
|
+
}
|
|
75
|
+
async start() {
|
|
76
|
+
const { patterns } = this.opts;
|
|
77
|
+
const { ignoreFiles, name } = this.childOpts;
|
|
78
|
+
const entries = await this.getEntries(patterns, ignoreFiles);
|
|
79
|
+
const totalCount = entries.length;
|
|
80
|
+
if (entries.length === 0) {
|
|
81
|
+
console.log(`No files matched with pattern:${patterns} via ${name}`);
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
84
|
+
const startTime = perf_hooks_1.performance.now();
|
|
85
|
+
const ret = await this.run(entries);
|
|
86
|
+
const endTime = perf_hooks_1.performance.now();
|
|
87
|
+
console.log(`exec ${name} with ${totalCount} files cost ${((endTime - startTime) /
|
|
88
|
+
1000).toFixed(2)}s`);
|
|
89
|
+
return ret;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.default = LintRunnerFactory;
|
package/lib/index.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
const runner_1 = __importDefault(require("../factory/runner"));
|
|
7
|
+
const parser_1 = __importDefault(require("./parser"));
|
|
8
|
+
class PrettierRunner extends runner_1.default {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
const { configPath, write } = opts;
|
|
11
|
+
const parser = new parser_1.default({
|
|
12
|
+
configPath,
|
|
13
|
+
write,
|
|
14
|
+
name: PrettierRunner.NAME,
|
|
15
|
+
});
|
|
16
|
+
const childOpts = {
|
|
17
|
+
parser,
|
|
18
|
+
name: PrettierRunner.NAME,
|
|
19
|
+
ignoreFiles: PrettierRunner.IGNORE_FILES,
|
|
20
|
+
};
|
|
21
|
+
super(opts, childOpts);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
PrettierRunner.IGNORE_FILES = ['.prettierignore'];
|
|
25
|
+
PrettierRunner.NAME = 'prettier';
|
|
26
|
+
exports.default = PrettierRunner;
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
const promises_1 = require("fs/promises");
|
|
7
|
+
const ko_lint_config_1 = require("ko-lint-config");
|
|
8
|
+
const parser_1 = __importDefault(require("../factory/parser"));
|
|
9
|
+
const { format, check } = ko_lint_config_1.prettier;
|
|
10
|
+
class PrettierParser extends parser_1.default {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
super();
|
|
13
|
+
this.opts = opts;
|
|
14
|
+
this.generateConfig();
|
|
15
|
+
}
|
|
16
|
+
generateConfig() {
|
|
17
|
+
if (this.opts.configPath) {
|
|
18
|
+
this.config = this.getConfigFromFile(this.opts.configPath);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const localConfigPath = this.detectLocalRunnerConfig(this.opts.name);
|
|
22
|
+
if (localConfigPath) {
|
|
23
|
+
this.config = this.getConfigFromFile(localConfigPath);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async format(file) {
|
|
28
|
+
const opts = { ...this.config, filepath: file };
|
|
29
|
+
let resultText = '';
|
|
30
|
+
try {
|
|
31
|
+
const source = await (0, promises_1.readFile)(file, 'utf-8');
|
|
32
|
+
if (this.opts.write) {
|
|
33
|
+
const formatContent = format(source, opts);
|
|
34
|
+
await (0, promises_1.writeFile)(file, formatContent, 'utf-8');
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (!check(source, opts)) {
|
|
38
|
+
resultText = `file ${opts.filepath} doesn't match ${this.opts.name} config`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return resultText;
|
|
42
|
+
}
|
|
43
|
+
catch (ex) {
|
|
44
|
+
console.log(ex);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.default = PrettierParser;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
const parser_1 = __importDefault(require("./parser"));
|
|
7
|
+
const runner_1 = __importDefault(require("../factory/runner"));
|
|
8
|
+
class StyleLintRunner extends runner_1.default {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
const { configPath, write } = opts;
|
|
11
|
+
const parser = new parser_1.default({
|
|
12
|
+
configPath,
|
|
13
|
+
write,
|
|
14
|
+
name: StyleLintRunner.NAME,
|
|
15
|
+
});
|
|
16
|
+
const childOpts = {
|
|
17
|
+
parser,
|
|
18
|
+
name: StyleLintRunner.NAME,
|
|
19
|
+
ignoreFiles: StyleLintRunner.IGNORE_FILES,
|
|
20
|
+
};
|
|
21
|
+
super(opts, childOpts);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
StyleLintRunner.IGNORE_FILES = ['.stylelintignore'];
|
|
25
|
+
StyleLintRunner.NAME = 'stylelint';
|
|
26
|
+
exports.default = StyleLintRunner;
|
|
@@ -4,12 +4,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const ko_lint_config_1 = require("ko-lint-config");
|
|
7
|
-
const
|
|
7
|
+
const parser_1 = __importDefault(require("../factory/parser"));
|
|
8
8
|
const { lint, formatters } = ko_lint_config_1.stylelint;
|
|
9
|
-
class
|
|
9
|
+
class StyleLintParser extends parser_1.default {
|
|
10
10
|
constructor(opts) {
|
|
11
11
|
super();
|
|
12
|
-
this.stdout = [];
|
|
13
12
|
this.opts = opts;
|
|
14
13
|
this.generateConfig();
|
|
15
14
|
}
|
|
@@ -18,34 +17,25 @@ class StyleLintRunner extends factory_1.default {
|
|
|
18
17
|
this.config = this.getConfigFromFile(this.opts.configPath);
|
|
19
18
|
}
|
|
20
19
|
else {
|
|
21
|
-
const localConfigPath = this.detectLocalRunnerConfig(
|
|
20
|
+
const localConfigPath = this.detectLocalRunnerConfig(this.opts.name);
|
|
22
21
|
if (localConfigPath) {
|
|
23
22
|
this.config = this.getConfigFromFile(localConfigPath);
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
}
|
|
27
|
-
async
|
|
28
|
-
const { write
|
|
29
|
-
|
|
30
|
-
...StyleLintRunner.IGNORE_FILES,
|
|
31
|
-
]);
|
|
32
|
-
if (entries.length === 0) {
|
|
33
|
-
console.log(`No files matched with pattern:${patterns} via ${StyleLintRunner.NAME}`);
|
|
34
|
-
process.exit(0);
|
|
35
|
-
}
|
|
26
|
+
async format(file) {
|
|
27
|
+
const { write } = this.opts;
|
|
28
|
+
let resultText = '';
|
|
36
29
|
try {
|
|
37
30
|
const result = await lint({
|
|
38
31
|
fix: write,
|
|
39
32
|
config: this.config,
|
|
40
|
-
files:
|
|
33
|
+
files: file,
|
|
41
34
|
});
|
|
42
35
|
if (result.errored) {
|
|
43
|
-
|
|
44
|
-
return this.stdout;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
return true;
|
|
36
|
+
resultText = formatters.string(result.results);
|
|
48
37
|
}
|
|
38
|
+
return resultText;
|
|
49
39
|
}
|
|
50
40
|
catch (ex) {
|
|
51
41
|
console.error(ex);
|
|
@@ -53,7 +43,4 @@ class StyleLintRunner extends factory_1.default {
|
|
|
53
43
|
}
|
|
54
44
|
}
|
|
55
45
|
}
|
|
56
|
-
|
|
57
|
-
StyleLintRunner.IGNORE_FILES = ['.stylelintignore'];
|
|
58
|
-
StyleLintRunner.NAME = 'stylelint';
|
|
59
|
-
exports.default = StyleLintRunner;
|
|
46
|
+
exports.default = StyleLintParser;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path_1 = require("path");
|
|
4
|
+
const worker_threads_1 = require("worker_threads");
|
|
5
|
+
class ThreadPool {
|
|
6
|
+
constructor(opts) {
|
|
7
|
+
this.workers = [];
|
|
8
|
+
this.workerPList = [];
|
|
9
|
+
this.stdout = [];
|
|
10
|
+
console.log('Using Multithreading...');
|
|
11
|
+
this.opts = opts;
|
|
12
|
+
this.queue = this.opts.entries;
|
|
13
|
+
this.format();
|
|
14
|
+
}
|
|
15
|
+
format() {
|
|
16
|
+
const { concurrentNumber, configPath, write, name } = this.opts;
|
|
17
|
+
if (this.workers.length < concurrentNumber) {
|
|
18
|
+
this.workerPList.push(this.createWorker({
|
|
19
|
+
configPath,
|
|
20
|
+
write,
|
|
21
|
+
name,
|
|
22
|
+
}));
|
|
23
|
+
this.format();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
createWorker(opts) {
|
|
27
|
+
const worker = new worker_threads_1.Worker((0, path_1.join)(__dirname, './Worker.js'), {
|
|
28
|
+
workerData: {
|
|
29
|
+
opts,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return new Promise(resolve => {
|
|
33
|
+
worker.postMessage(this.queue.shift());
|
|
34
|
+
worker.on('message', (result) => {
|
|
35
|
+
this.stdout.push(result);
|
|
36
|
+
if (this.queue.length === 0) {
|
|
37
|
+
resolve(true);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const next = this.queue.shift();
|
|
41
|
+
worker.postMessage(next);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
worker.on('error', err => {
|
|
45
|
+
console.log(err);
|
|
46
|
+
process.exit(1);
|
|
47
|
+
});
|
|
48
|
+
this.workers.push(worker);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async exec() {
|
|
52
|
+
return Promise.all(this.workerPList).then(() => {
|
|
53
|
+
return this.stdout;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.default = ThreadPool;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const worker_threads_1 = require("worker_threads");
|
|
4
|
+
let parser;
|
|
5
|
+
switch (worker_threads_1.workerData.opts.name) {
|
|
6
|
+
case 'eslint':
|
|
7
|
+
const ESLintParser = require('../eslint/parser').default;
|
|
8
|
+
parser = new ESLintParser(worker_threads_1.workerData.opts);
|
|
9
|
+
break;
|
|
10
|
+
case 'prettier':
|
|
11
|
+
const PrettierParser = require('../prettier/parser').default;
|
|
12
|
+
parser = new PrettierParser(worker_threads_1.workerData.opts);
|
|
13
|
+
break;
|
|
14
|
+
case 'stylelint':
|
|
15
|
+
const StyleParser = require('../stylelint/parser').default;
|
|
16
|
+
parser = new StyleParser(worker_threads_1.workerData.opts);
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
worker_threads_1.parentPort?.on('message', async (file) => {
|
|
20
|
+
const result = await parser.format(file);
|
|
21
|
+
worker_threads_1.parentPort?.postMessage(result);
|
|
22
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
const Pool_1 = __importDefault(require("./Pool"));
|
|
7
|
+
class MultiThreading {
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
this.opts = opts;
|
|
10
|
+
}
|
|
11
|
+
async batch() {
|
|
12
|
+
const pool = new Pool_1.default(this.opts);
|
|
13
|
+
return await pool.exec();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.default = MultiThreading;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ko-lints",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "lint tools used by ko",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ko",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"fast-glob": "^3.2.11"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"ko-lint-config": "1.0
|
|
32
|
+
"ko-lint-config": "2.1.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"typescript": "^4.6.4",
|
|
36
|
-
"ko-lint-config": "1.0
|
|
36
|
+
"ko-lint-config": "2.1.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc",
|
|
40
|
-
"debug": "tsc --sourcemap -w"
|
|
40
|
+
"debug": "rm -rf lib && tsc --sourcemap -w"
|
|
41
41
|
},
|
|
42
42
|
"readme": "# ko-lint\n\nThis package is used by [ko](https://github.com/DTStack/ko)\n"
|
|
43
43
|
}
|
package/lib/eslint.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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
|
-
const ko_lint_config_1 = require("ko-lint-config");
|
|
7
|
-
const factory_1 = __importDefault(require("./factory"));
|
|
8
|
-
const { ESLint } = ko_lint_config_1.eslint;
|
|
9
|
-
class ESlintRunner extends factory_1.default {
|
|
10
|
-
constructor(opts) {
|
|
11
|
-
super();
|
|
12
|
-
this.stdout = [];
|
|
13
|
-
this.opts = opts;
|
|
14
|
-
this.generateConfig();
|
|
15
|
-
}
|
|
16
|
-
generateConfig() {
|
|
17
|
-
if (this.opts.configPath) {
|
|
18
|
-
this.config = this.getConfigFromFile(this.opts.configPath);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
const localConfigPath = this.detectLocalRunnerConfig('eslint');
|
|
22
|
-
if (localConfigPath) {
|
|
23
|
-
this.config = this.getConfigFromFile(localConfigPath);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
async start() {
|
|
28
|
-
const { write, patterns } = this.opts;
|
|
29
|
-
const entries = await this.getEntries(patterns, [
|
|
30
|
-
...ESlintRunner.IGNORE_FILES,
|
|
31
|
-
]);
|
|
32
|
-
if (entries.length === 0) {
|
|
33
|
-
console.log(`No files matched with pattern:${patterns} via ${ESlintRunner.NAME}`);
|
|
34
|
-
process.exit(0);
|
|
35
|
-
}
|
|
36
|
-
const eslintInstance = new ESLint({
|
|
37
|
-
fix: write,
|
|
38
|
-
overrideConfig: this.config,
|
|
39
|
-
useEslintrc: false,
|
|
40
|
-
extensions: ESlintRunner.EXTENSIONS,
|
|
41
|
-
});
|
|
42
|
-
try {
|
|
43
|
-
const formatter = await eslintInstance.loadFormatter();
|
|
44
|
-
const eslintFilesPromises = entries.map(async (file) => {
|
|
45
|
-
const result = await eslintInstance.lintFiles(file);
|
|
46
|
-
if (result[0].errorCount) {
|
|
47
|
-
const resultText = formatter.format(result);
|
|
48
|
-
this.stdout.push(resultText);
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
return true;
|
|
52
|
-
});
|
|
53
|
-
const result = await Promise.all(eslintFilesPromises);
|
|
54
|
-
if (result.includes(false)) {
|
|
55
|
-
return this.stdout;
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
catch (ex) {
|
|
62
|
-
console.error(ex);
|
|
63
|
-
process.exit(1);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
ESlintRunner.EXTENSIONS = ['ts', 'tsx', 'js', 'jsx'];
|
|
68
|
-
ESlintRunner.IGNORE_FILES = ['.eslintignore'];
|
|
69
|
-
ESlintRunner.NAME = 'eslint';
|
|
70
|
-
exports.default = ESlintRunner;
|
package/lib/factory.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
const path_1 = require("path");
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
|
-
const assert_1 = __importDefault(require("assert"));
|
|
9
|
-
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
10
|
-
class LintRunnerFactory {
|
|
11
|
-
constructor() {
|
|
12
|
-
this.cwd = process.cwd();
|
|
13
|
-
}
|
|
14
|
-
async getEntries(patterns, ignoreFiles) {
|
|
15
|
-
return (0, fast_glob_1.default)(patterns, {
|
|
16
|
-
dot: true,
|
|
17
|
-
ignore: this.getIgnorePatterns(...ignoreFiles),
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
getIgnorePatterns(...ignoreFiles) {
|
|
21
|
-
return ['.gitignore', ...ignoreFiles]
|
|
22
|
-
.map(fileName => {
|
|
23
|
-
const filePath = (0, path_1.join)(this.cwd, fileName);
|
|
24
|
-
if ((0, fs_1.existsSync)(filePath)) {
|
|
25
|
-
return (0, fs_1.readFileSync)(filePath, 'utf-8')
|
|
26
|
-
.split('\n')
|
|
27
|
-
.filter(str => str && !str.startsWith('#'));
|
|
28
|
-
}
|
|
29
|
-
return [];
|
|
30
|
-
})
|
|
31
|
-
.reduce((acc, current) => {
|
|
32
|
-
current.forEach(p => {
|
|
33
|
-
if (!acc.includes(p)) {
|
|
34
|
-
acc.push(p);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
return acc;
|
|
38
|
-
}, []);
|
|
39
|
-
}
|
|
40
|
-
getConfigFromFile(filepath) {
|
|
41
|
-
(0, assert_1.default)((0, path_1.isAbsolute)(filepath), 'only accept absolute config filepath');
|
|
42
|
-
return require(filepath);
|
|
43
|
-
}
|
|
44
|
-
detectLocalRunnerConfig(name) {
|
|
45
|
-
const cwd = process.cwd();
|
|
46
|
-
const files = (0, fs_1.readdirSync)(cwd).filter(path => !(0, fs_1.statSync)(path).isDirectory());
|
|
47
|
-
let ret = '';
|
|
48
|
-
for (let file of files) {
|
|
49
|
-
if (file.includes(name) && !file.includes('ignore')) {
|
|
50
|
-
ret = file;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return ret ? (0, path_1.join)(cwd, ret) : ret;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.default = LintRunnerFactory;
|
package/lib/prettier.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
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
|
-
const promises_1 = require("fs/promises");
|
|
7
|
-
const ko_lint_config_1 = require("ko-lint-config");
|
|
8
|
-
const factory_1 = __importDefault(require("./factory"));
|
|
9
|
-
const { format, check } = ko_lint_config_1.prettier;
|
|
10
|
-
class PrettierRunner extends factory_1.default {
|
|
11
|
-
constructor(opts) {
|
|
12
|
-
super();
|
|
13
|
-
this.stdout = [];
|
|
14
|
-
this.opts = opts;
|
|
15
|
-
this.generateConfig();
|
|
16
|
-
}
|
|
17
|
-
generateConfig() {
|
|
18
|
-
if (this.opts.configPath) {
|
|
19
|
-
this.config = this.getConfigFromFile(this.opts.configPath);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
const localConfigPath = this.detectLocalRunnerConfig('prettier');
|
|
23
|
-
if (localConfigPath) {
|
|
24
|
-
this.config = this.getConfigFromFile(localConfigPath);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async start() {
|
|
29
|
-
const { write, patterns } = this.opts;
|
|
30
|
-
const entries = await this.getEntries(patterns, [
|
|
31
|
-
...PrettierRunner.IGNORE_FILES,
|
|
32
|
-
]);
|
|
33
|
-
if (entries.length === 0) {
|
|
34
|
-
console.log(`No files matched with pattern:${patterns} via ${PrettierRunner.NAME}`);
|
|
35
|
-
process.exit(0);
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
const formatFilesPromises = entries.map(async (file) => {
|
|
39
|
-
const source = await (0, promises_1.readFile)(file, 'utf-8');
|
|
40
|
-
const opts = { ...this.config, filepath: file };
|
|
41
|
-
if (write) {
|
|
42
|
-
const formatContent = format(source, opts);
|
|
43
|
-
await (0, promises_1.writeFile)(file, formatContent, 'utf-8');
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
if (check(source, opts)) {
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
this.stdout.push(`file ${opts.filepath} doesn't match prettier config`);
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
const result = await Promise.all(formatFilesPromises);
|
|
57
|
-
if (result.includes(false)) {
|
|
58
|
-
return this.stdout;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
catch (ex) {
|
|
65
|
-
console.error(ex);
|
|
66
|
-
process.exit(1);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
PrettierRunner.EXTENSIONS = ['ts', 'tsx', 'js', 'jsx', 'json'];
|
|
71
|
-
PrettierRunner.IGNORE_FILES = ['.prettierignore'];
|
|
72
|
-
PrettierRunner.NAME = 'prettier';
|
|
73
|
-
exports.default = PrettierRunner;
|