-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
124 lines (105 loc) · 2.95 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable no-console */
const fs = require('fs');
const glob = require('glob');
const path = require('path');
/**
* @typedef {import('./lib/linter').Failure} Failure
* @typedef {import('./lib/linter').FailureSeverity} FailureSeverity
*/
const linter = require('./lib/linter');
(async () => {
try {
const cwd = process.cwd();
let totalFailures = 0;
let filesWithFailures = 0;
/** @type {{ files?: string[]; rules?: import('./lib/linter').OptionsRules}} */
let options = {};
try {
// eslint-disable-next-line global-require
options = require(path.resolve(cwd, './smartylint'));
}
catch (ex) { /* If 'smartylint.(js|json)' cannot be found in CWD, the linter is using the default settings, so we can ignore this error */ }
/** @type {{ files: string[]; rules: import('./lib/linter').OptionsRules}} */
// @ts-ignore
// eslint-disable-next-line global-require
const defaultOptions = require('./smartylint');
await linter.initialize({ rules: options.rules });
const files = (options.files ? options.files : defaultOptions.files).reduce((resolvedFiles, pattern) => [...resolvedFiles, ...glob.sync(pattern)], /** @type {string[]} */([]));
for (const fileName of files) {
const filePath = path.resolve(cwd, fileName);
// eslint-disable-next-line no-await-in-loop
const failures = await verifyFile(filePath);
totalFailures += failures.length;
filesWithFailures++;
for (const failure of failures) {
console.log(`${filePath}:${failure.startLine + 1}:${failure.startColumn + 1}: ${failure.message} [${failure.severity}/${failure.ruleId}]`);
}
}
console.log('');
if (totalFailures === 0) {
console.log('No problems found');
}
else {
console.log(totalFailures === 1 ? '1 problem' : `${totalFailures} problems in ${filesWithFailures} of ${files.length} ${(files.length === 1 ? 'file' : 'files')}`);
}
}
catch (error) {
console.error(error.message);
// eslint-disable-next-line no-process-exit
process.exit(-1);
}
})();
/**
* Utility to read a file as a promise
*
* @param {string} filePath
* @returns {Promise<Failure[]>}
*/
async function verifyFile (filePath) {
/** @type {string} */
let sourceCode;
try {
sourceCode = (await readFile(filePath)).toString('utf8');
}
catch (error) {
return [
{
ruleId: 'internalFileRead',
message: error.message,
startLine: 0,
startColumn: 0,
endLine: 0,
endColumn: 0
}
];
}
try {
const failures = await linter.verify(sourceCode, {
fileName: filePath
});
return failures;
}
catch (error) {
return [
{
ruleId: 'internalLinterError',
message: error.stack,
startLine: 0,
startColumn: 0,
endLine: 0,
endColumn: 0
}
];
}
}
/**
* Utility to read a file as a promise
*
* @param {string} filePath
* @returns {Promise<Buffer>}
*/
function readFile (filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (error, data) => (error ? reject(error) : resolve(data)));
});
}