-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (64 loc) · 2.06 KB
/
index.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
import fs from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { fdir as Fdir } from 'fdir';
import fixFile from './lib/fix.js';
import lintFile from './lib/lint.js';
function isFileExtnameAllowed(filePath, extensions) {
const extname = path.extname(filePath)
.toLocaleLowerCase()
.slice(1);
return extname && extensions.includes(extname);
}
async function markdownLint({ paths = [], fix, ext, recursive, config, typograph }) {
const extensions = ext.join('|');
const directoryFilesCrawler = new Fdir()
.filter(filePath => isFileExtnameAllowed(filePath, extensions))
.withMaxDepth(recursive ? undefined : 0)
.withBasePath();
const filePaths = [...new Set(
paths.reduce((accumulator, filePath) => {
if (fs.existsSync(filePath)) {
const isDirectory = fs.statSync(filePath).isDirectory();
// search for files recursively inside the directory
if (isDirectory) {
accumulator = [
...accumulator,
...directoryFilesCrawler.crawl(filePath).sync(),
];
}
// filter files by extension
if (!isDirectory && isFileExtnameAllowed(filePath, extensions)) {
accumulator.push(filePath);
}
}
return accumulator;
}, []),
)];
const defaultConfigFilePath = path.join(path.dirname(import.meta.url), '.markdownlintrc.cjs');
const defaultConfigData = await import(defaultConfigFilePath);
const appConfig = defaultConfigData.default;
let externalConfig;
if (config && fs.existsSync(config)) {
const externalConfigFilepath = pathToFileURL(path.resolve(config));
const externalConfigData = await import(externalConfigFilepath);
externalConfig = externalConfigData.default;
}
for (const filePath of filePaths) {
let fileContent = fs.readFileSync(filePath, 'utf8');
if (fix) {
fileContent = await fixFile(fileContent, { // eslint-disable-line no-await-in-loop
appConfig,
externalConfig,
typograph,
});
fs.writeFileSync(filePath, fileContent);
}
lintFile(fileContent, {
appConfig,
externalConfig,
filePath,
});
}
}
export default markdownLint;