forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone.js
316 lines (270 loc) · 9.38 KB
/
standalone.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* @flow */
"use strict";
const _ = require("lodash");
const createStylelint = require("./createStylelint");
const createStylelintResult = require("./createStylelintResult");
const debug = require("debug")("stylelint:standalone");
const FileCache = require("./utils/FileCache");
const filterFilePaths = require("./utils/filterFilePaths");
const formatters = require("./formatters");
const fs = require("fs");
const getFormatterOptionsText = require("./utils/getFormatterOptionsText");
const globby /*: Function*/ = require("globby");
const hash = require("./utils/hash");
const ignore = require("ignore");
const needlessDisables /*: Function*/ = require("./needlessDisables");
const NoFilesFoundError = require("./utils/noFilesFoundError");
const path = require("path");
const pify = require("pify");
const pkg = require("../package.json");
const writeFileAtomic /*: Function*/ = require("./vendor/writeFileAtomic");
const DEFAULT_IGNORE_FILENAME = ".stylelintignore";
const FILE_NOT_FOUND_ERROR_CODE = "ENOENT";
const ALWAYS_IGNORED_GLOBS = ["**/node_modules/**", "**/bower_components/**"];
module.exports = function(
options /*: stylelint$standaloneOptions */
) /*: Promise<stylelint$standaloneReturnValue>*/ {
const cacheLocation = options.cacheLocation;
const code = options.code;
const codeFilename = options.codeFilename;
const config = options.config;
const configBasedir = options.configBasedir;
const configFile = options.configFile;
const configOverrides = options.configOverrides;
const customSyntax = options.customSyntax;
const globbyOptions = options.globbyOptions;
const files = options.files;
const fix = options.fix;
const formatter = options.formatter;
const ignoreDisables = options.ignoreDisables;
const reportNeedlessDisables = options.reportNeedlessDisables;
const maxWarnings = options.maxWarnings;
const syntax = options.syntax;
const allowEmptyInput = options.allowEmptyInput || false;
const useCache = options.cache || false;
let fileCache;
const startTime = Date.now();
// The ignorer will be used to filter file paths after the glob is checked,
// before any files are actually read
const ignoreFilePath = options.ignorePath || DEFAULT_IGNORE_FILENAME;
const absoluteIgnoreFilePath = path.isAbsolute(ignoreFilePath)
? ignoreFilePath
: path.resolve(process.cwd(), ignoreFilePath);
let ignoreText = "";
try {
ignoreText = fs.readFileSync(absoluteIgnoreFilePath, "utf8");
} catch (readError) {
if (readError.code !== FILE_NOT_FOUND_ERROR_CODE) throw readError;
}
const ignorePattern = options.ignorePattern || [];
const ignorer = ignore()
.add(ignoreText)
.add(ignorePattern);
const isValidCode = typeof code === "string";
if ((!files && !isValidCode) || (files && (code || isValidCode))) {
throw new Error(
"You must pass stylelint a `files` glob or a `code` string, though not both"
);
}
let formatterFunction;
if (typeof formatter === "string") {
formatterFunction = formatters[formatter];
if (formatterFunction === undefined) {
return Promise.reject(
new Error(
`You must use a valid formatter option: ${getFormatterOptionsText()} or a function`
)
);
}
} else if (typeof formatter === "function") {
formatterFunction = formatter;
} else {
formatterFunction = formatters.json;
}
const stylelint = createStylelint({
config,
configFile,
configBasedir,
configOverrides,
ignoreDisables,
reportNeedlessDisables,
syntax,
customSyntax,
fix
});
if (!files) {
const absoluteCodeFilename =
codeFilename !== undefined && !path.isAbsolute(codeFilename)
? path.join(process.cwd(), codeFilename)
: codeFilename;
// if file is ignored, return nothing
if (
absoluteCodeFilename &&
!filterFilePaths(ignorer, [
path.relative(process.cwd(), absoluteCodeFilename)
]).length
) {
return Promise.resolve(prepareReturnValue([]));
}
return stylelint
._lintSource({
code,
codeFilename: absoluteCodeFilename
})
.then(postcssResult => {
// Check for file existence
return new Promise((resolve, reject) => {
if (!absoluteCodeFilename) {
reject();
return;
}
fs.stat(absoluteCodeFilename, err => {
if (err) {
reject();
} else {
resolve();
}
});
})
.then(() => {
return stylelint._createStylelintResult(
postcssResult,
absoluteCodeFilename
);
})
.catch(() => {
return stylelint._createStylelintResult(postcssResult);
});
})
.catch(_.partial(handleError, stylelint))
.then(stylelintResult => {
const postcssResult = stylelintResult._postcssResult;
const returnValue = prepareReturnValue([stylelintResult]);
if (options.fix && postcssResult && !postcssResult.stylelint.ignored) {
// If we're fixing, the output should be the fixed code
returnValue.output = postcssResult.root.toString(
postcssResult.opts.syntax
);
}
return returnValue;
});
}
let fileList = files;
if (typeof fileList === "string") {
fileList = [fileList];
}
if (!options.disableDefaultIgnores) {
fileList = fileList.concat(ALWAYS_IGNORED_GLOBS.map(glob => "!" + glob));
}
if (useCache) {
const stylelintVersion = pkg.version;
const hashOfConfig = hash(`${stylelintVersion}_${JSON.stringify(config)}`);
fileCache = new FileCache(cacheLocation, hashOfConfig);
} else {
// No need to calculate hash here, we just want to delete cache file.
fileCache = new FileCache(cacheLocation);
// Remove cache file if cache option is disabled
fileCache.destroy();
}
return globby(fileList, globbyOptions)
.then(filePaths => {
// The ignorer filter needs to check paths relative to cwd
filePaths = filterFilePaths(
ignorer,
filePaths.map(p => path.relative(process.cwd(), p))
);
if (!filePaths.length) {
if (!allowEmptyInput) {
throw new NoFilesFoundError(fileList);
}
return Promise.all([]);
}
const cwd = _.get(globbyOptions, "cwd", process.cwd());
let absoluteFilePaths = filePaths.map(filePath => {
const absoluteFilepath = !path.isAbsolute(filePath)
? path.join(cwd, filePath)
: path.normalize(filePath);
return absoluteFilepath;
});
if (useCache) {
absoluteFilePaths = absoluteFilePaths.filter(
fileCache.hasFileChanged.bind(fileCache)
);
}
const getStylelintResults = absoluteFilePaths.map(absoluteFilepath => {
debug(`Processing ${absoluteFilepath}`);
return stylelint
._lintSource({
filePath: absoluteFilepath
})
.then(postcssResult => {
if (postcssResult.stylelint.stylelintError && useCache) {
debug(
`${absoluteFilepath} contains linting errors and will not be cached.`
);
fileCache.removeEntry(absoluteFilepath);
}
// If we're fixing, save the file with changed code
let fixFile = Promise.resolve();
if (!postcssResult.stylelint.ignored && options.fix) {
const fixedCss = postcssResult.root.toString(
postcssResult.opts.syntax
);
if (postcssResult.root.source.input.css !== fixedCss) {
fixFile = pify(writeFileAtomic)(absoluteFilepath, fixedCss);
}
}
return fixFile.then(() =>
stylelint._createStylelintResult(postcssResult, absoluteFilepath)
);
})
.catch(error => {
// On any error, we should not cache the lint result
fileCache.removeEntry(absoluteFilepath);
return handleError(stylelint, error);
});
});
return Promise.all(getStylelintResults);
})
.then(stylelintResults => {
if (useCache) {
fileCache.reconcile();
}
return prepareReturnValue(stylelintResults);
});
function prepareReturnValue(
stylelintResults /*: Array<stylelint$result>*/
) /*: stylelint$standaloneReturnValue*/ {
const errored = stylelintResults.some(
result => result.errored || result.parseErrors.length > 0
);
const returnValue /*: stylelint$standaloneReturnValue*/ = {
errored,
output: formatterFunction(stylelintResults),
results: stylelintResults
};
if (reportNeedlessDisables) {
returnValue.needlessDisables = needlessDisables(stylelintResults);
}
if (maxWarnings !== undefined) {
const foundWarnings = stylelintResults.reduce((count, file) => {
return count + file.warnings.length;
}, 0);
if (foundWarnings > maxWarnings) {
returnValue.maxWarningsExceeded = { maxWarnings, foundWarnings };
}
}
debug(`Linting complete in ${Date.now() - startTime}ms`);
return returnValue;
}
};
function handleError(
stylelint /*: stylelint$internalApi */,
error /*: Object */
) /*: Promise<stylelint$result> */ {
if (error.name === "CssSyntaxError") {
return createStylelintResult(stylelint, null, null, error);
} else {
throw error;
}
}