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
|
import { Server } from "http";
import * as express from "express";
import { AddressInfo } from "net";
import * as stylelint from "stylelint";
import * as fs from "fs";
import * as bodyParser from "body-parser";
// for testing purposes
let log = console.log;
let logError = console.error;
export function setLogHandlersForTests(
logHandler: typeof console.log,
errorHandler: typeof console.error
) {
log = logHandler;
logError = errorHandler;
}
export function start(port = 0): Promise<Server> {
return new Promise(resolve => {
log("DEBUG starting stylelint-bridge server at port", port);
const app = express();
app.use(bodyParser.json());
app.post("/analyze", analyzeWithStylelint);
app.get("/status", (_: express.Request, resp: express.Response) =>
resp.send("OK!")
);
app.post("/close", (_req: express.Request, resp: express.Response) => {
console.log("DEBUG stylelint-bridge server will shutdown");
resp.end(() => {
server.close();
});
});
// every time something is wrong we log error and send empty response (with 0 issues)
// it's important to keep this call last in configuring "app"
app.use(
(
error: any,
_req: express.Request,
response: express.Response,
_next: any
) => processError(error, response)
);
const server = app.listen(port, () => {
log(
"DEBUG stylelint-bridge server is running at port",
(server.address() as AddressInfo).port
);
resolve(server);
});
});
}
function analyzeWithStylelint(
request: express.Request,
response: express.Response
) {
const parsedRequest = request.body as AnalysisInput;
const { filePath, fileContent, configFile } = parsedRequest;
const code =
typeof fileContent == "string" ? fileContent : getFileContent(filePath);
const options = {
code,
codeFilename: filePath,
configFile
};
stylelint
.lint(options)
.then(result => response.json(toIssues(result.results, filePath)))
.catch(error => processError(error, response));
}
function processError(error: any, response: express.Response) {
logError(error);
response.json([]);
}
function toIssues(results: stylelint.LintResult[], filePath: string): Issue[] {
const analysisResponse: Issue[] = [];
// we should have only one element in 'results' as we are analyzing only 1 file
results.forEach(result => {
// to avoid reporting on "fake" source like <input ccs 1>
if (result.source !== filePath) {
log(
`DEBUG For file [${filePath}] received issues with [${result.source}] as a source. They will not be reported.`
);
return;
}
result.warnings.forEach(warning =>
analysisResponse.push({
line: warning.line,
text: warning.text,
rule: warning.rule
})
);
});
return analysisResponse;
}
function getFileContent(filePath: string) {
const fileContent = fs.readFileSync(filePath, { encoding: "utf8" });
// strip BOM
if (fileContent.charCodeAt(0) === 0xfeff) {
return fileContent.slice(1);
}
return fileContent;
}
export interface AnalysisInput {
filePath: string;
fileContent: string | undefined;
configFile: string;
}
export interface Issue {
line: number;
rule: string;
text: string;
}
|