-
-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathstandalone.js
314 lines (263 loc) · 8.57 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
'use strict';
const createStylelint = require('./createStylelint');
const createStylelintResult = require('./createStylelintResult');
const debug = require('debug')('stylelint:standalone');
const fastGlob = require('fast-glob');
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 = require('globby');
const hash = require('./utils/hash');
const isPathNotFoundError = require('./utils/isPathNotFoundError');
const NoFilesFoundError = require('./utils/noFilesFoundError');
const normalizePath = require('normalize-path');
const path = require('path');
const pkg = require('../package.json');
const prepareReturnValue = require('./prepareReturnValue');
const { default: ignore } = require('ignore');
const DEFAULT_IGNORE_FILENAME = '.stylelintignore';
const ALWAYS_IGNORED_GLOBS = ['**/node_modules/**'];
const writeFileAtomic = require('write-file-atomic');
/** @typedef {import('stylelint').LinterOptions} LinterOptions */
/** @typedef {import('stylelint').LinterResult} LinterResult */
/** @typedef {import('stylelint').LintResult} StylelintResult */
/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').FormatterType} FormatterType */
/**
*
* @param {LinterOptions} options
* @returns {Promise<LinterResult>}
*/
async function standalone({
allowEmptyInput = false,
cache: useCache = false,
cacheLocation,
code,
codeFilename,
config,
configBasedir,
configFile,
customSyntax,
disableDefaultIgnores,
files,
fix,
formatter,
globbyOptions,
ignoreDisables,
ignorePath = DEFAULT_IGNORE_FILENAME,
ignorePattern = [],
maxWarnings,
quiet,
reportDescriptionlessDisables,
reportInvalidScopeDisables,
reportNeedlessDisables,
syntax,
}) {
/** @type {FileCache} */
let fileCache;
const startTime = Date.now();
const isValidCode = typeof code === 'string';
if ((!files && !isValidCode) || (files && (code || isValidCode))) {
return Promise.reject(
new Error('You must pass stylelint a `files` glob or a `code` string, though not both'),
);
}
// The ignorer will be used to filter file paths after the glob is checked,
// before any files are actually read
const absoluteIgnoreFilePath = path.isAbsolute(ignorePath)
? ignorePath
: path.resolve(process.cwd(), ignorePath);
let ignoreText = '';
try {
ignoreText = fs.readFileSync(absoluteIgnoreFilePath, 'utf8');
} catch (readError) {
if (!isPathNotFoundError(readError)) {
return Promise.reject(readError);
}
}
const ignorer = ignore().add(ignoreText).add(ignorePattern);
/** @type {Formatter} */
let formatterFunction;
try {
formatterFunction = getFormatterFunction(formatter);
} catch (error) {
return Promise.reject(error);
}
const stylelint = createStylelint({
config,
configFile,
configBasedir,
ignoreDisables,
ignorePath,
reportNeedlessDisables,
reportInvalidScopeDisables,
reportDescriptionlessDisables,
syntax,
customSyntax,
fix,
quiet,
});
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 prepareReturnValue([], maxWarnings, formatterFunction);
}
let stylelintResult;
try {
const postcssResult = await stylelint._lintSource({
code,
codeFilename: absoluteCodeFilename,
});
stylelintResult = await stylelint._createStylelintResult(postcssResult, absoluteCodeFilename);
} catch (error) {
stylelintResult = await handleError(stylelint, error);
}
const postcssResult = stylelintResult._postcssResult;
const returnValue = prepareReturnValue([stylelintResult], maxWarnings, formatterFunction);
if (
fix &&
postcssResult &&
!postcssResult.stylelint.ignored &&
!postcssResult.stylelint.ruleDisableFix
) {
returnValue.output =
!postcssResult.stylelint.disableWritingFix && postcssResult.opts
? // If we're fixing, the output should be the fixed code
postcssResult.root.toString(postcssResult.opts.syntax)
: // If the writing of the fix is disabled, the input code is returned as-is
code;
}
return returnValue;
}
let fileList = [files].flat().map((entry) => {
const cwd = (globbyOptions && globbyOptions.cwd) || process.cwd();
const absolutePath = !path.isAbsolute(entry) ? path.join(cwd, entry) : path.normalize(entry);
if (fs.existsSync(absolutePath)) {
// This path points to a file. Return an escaped path to avoid globbing
return fastGlob.escapePath(normalizePath(entry));
}
return entry;
});
if (!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();
}
let filePaths = await globby(fileList, globbyOptions);
// The ignorer filter needs to check paths relative to cwd
filePaths = filterFilePaths(
ignorer,
filePaths.map((p) => path.relative(process.cwd(), p)),
);
let stylelintResults;
if (filePaths.length) {
const cwd = (globbyOptions && 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(async (absoluteFilepath) => {
debug(`Processing ${absoluteFilepath}`);
try {
const postcssResult = await stylelint._lintSource({
filePath: absoluteFilepath,
});
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
*/
if (
postcssResult.root &&
postcssResult.opts &&
!postcssResult.stylelint.ignored &&
fix &&
!postcssResult.stylelint.disableWritingFix
) {
const fixedCss = postcssResult.root.toString(postcssResult.opts.syntax);
if (
postcssResult.root &&
postcssResult.root.source &&
postcssResult.root.source.input.css !== fixedCss
) {
await writeFileAtomic(absoluteFilepath, fixedCss);
}
}
return stylelint._createStylelintResult(postcssResult, absoluteFilepath);
} catch (error) {
// On any error, we should not cache the lint result
fileCache.removeEntry(absoluteFilepath);
return handleError(stylelint, error, absoluteFilepath);
}
});
stylelintResults = await Promise.all(getStylelintResults);
} else if (allowEmptyInput) {
stylelintResults = await Promise.all([]);
} else {
stylelintResults = await Promise.reject(new NoFilesFoundError(fileList));
}
if (useCache) {
fileCache.reconcile();
}
const result = prepareReturnValue(stylelintResults, maxWarnings, formatterFunction);
debug(`Linting complete in ${Date.now() - startTime}ms`);
return result;
}
/**
* @param {FormatterType | Formatter | undefined} selected
* @returns {Formatter}
*/
function getFormatterFunction(selected) {
/** @type {Formatter} */
let formatterFunction;
if (typeof selected === 'string') {
formatterFunction = formatters[selected];
if (formatterFunction === undefined) {
throw new Error(
`You must use a valid formatter option: ${getFormatterOptionsText()} or a function`,
);
}
} else if (typeof selected === 'function') {
formatterFunction = selected;
} else {
formatterFunction = formatters.json;
}
return formatterFunction;
}
/**
* @param {import('stylelint').InternalApi} stylelint
* @param {any} error
* @param {string} [filePath]
* @return {Promise<StylelintResult>}
*/
function handleError(stylelint, error, filePath = undefined) {
if (error.name === 'CssSyntaxError') {
return createStylelintResult(stylelint, undefined, filePath, error);
}
throw error;
}
module.exports = /** @type {typeof import('stylelint').lint} */ (standalone);