-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
158 lines (122 loc) · 3.62 KB
/
server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
const {join, parse, resolve} = require('path');
const fs = require('fs');
const {createConnection, ProposedFeatures, TextDocuments} = require('vscode-languageserver');
const findPkgDir = require('find-pkg-dir');
const parseUri = require('vscode-uri').URI.parse;
const pathIsInside = require('path-is-inside');
const stylelintVSCode = require('./stylelint-vscode');
let config;
let configOverrides;
let autoFixOnSave;
let useLocal;
let disableErrorMessage;
const connection = createConnection(ProposedFeatures.all);
const documents = new TextDocuments();
async function validate(document, isAutoFixOnSave = false) {
const options = {
fix: isAutoFixOnSave
};
if (config) {
options.config = config;
}
if (configOverrides) {
options.configOverrides = configOverrides;
}
const documentPath = parseUri(document.uri).fsPath;
if (documentPath) {
const workspaceFolders = await connection.workspace.getWorkspaceFolders();
if (workspaceFolders) {
for (const {uri} of workspaceFolders) {
const workspacePath = parseUri(uri).fsPath;
if (pathIsInside(documentPath, workspacePath)) {
options.ignorePath = join(workspacePath, '.stylelintignore');
break;
}
}
}
if (options.ignorePath === undefined) {
options.ignorePath = join(findPkgDir(documentPath) || parse(documentPath).root, '.stylelintignore');
}
if (useLocal) {
let dir;
let startDir = documentPath;
while (dir = findPkgDir(startDir)) {
const localPath = join(dir, 'node_modules', 'stylelint');
if (fs.existsSync(localPath)) {
options.path = localPath;
break;
}
startDir = resolve(dir, '..');
}
if (!options.path) {
connection.sendRequest('setStatusBarError');
return;
}
}
else {
connection.sendRequest('setStatusBarOk');
}
}
try {
const diagnostics = await stylelintVSCode(document, options);
connection.sendDiagnostics({
uri: document.uri,
diagnostics
});
connection.sendRequest('setStatusBarOk');
}
catch (err) {
connection.console.log(err);
connection.sendRequest('setStatusBarError');
if (disableErrorMessage) {
return;
}
if (err.reasons) {
for (const reason of err.reasons) {
connection.window.showErrorMessage(`stylelint: ${reason}`);
}
return;
}
// https://github.com/stylelint/stylelint/blob/10.0.1/lib/utils/configurationError.js#L10
if (err.code === 78) {
connection.window.showErrorMessage(`stylelint: ${err.message}`);
return;
}
connection.window.showErrorMessage(err.stack.replace(/\n/ug, ' '));
}
}
function validateAll() {
for (const document of documents.all()) {
validate(document);
}
}
connection.onInitialize(() => {
validateAll();
return {
capabilities: {
textDocumentSync: documents.syncKind
}
};
});
connection.onDidChangeConfiguration(({settings}) => {
config = settings.stylelint.config;
configOverrides = settings.stylelint.configOverrides;
autoFixOnSave = settings.stylelint.autoFixOnSave;
useLocal = settings.stylelint.useLocal;
disableErrorMessage = settings.stylelint.disableErrorMessage;
validateAll();
});
connection.onDidChangeWatchedFiles(validateAll);
documents.onDidChangeContent(({document}) => validate(document));
documents.onDidClose(({document}) => connection.sendDiagnostics({
uri: document.uri,
diagnostics: []
}));
documents.onDidSave(({document}) => {
if (autoFixOnSave) {
validate(document, true);
}
});
documents.listen(connection);
connection.listen();