-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (158 loc) · 5.62 KB
/
Copy pathindex.js
File metadata and controls
198 lines (158 loc) · 5.62 KB
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
import process from 'node:process';
import path from 'node:path';
import isUrl from 'is-url-superb';
import isGithubUrl from 'is-github-url';
import ora from 'ora';
import {remark} from 'remark';
import {globbySync} from 'globby';
import rmfr from 'rmfr';
import {temporaryDirectory} from 'tempy';
import {readSync as readVFileSync} from 'to-vfile';
import vfileReporterPretty from 'vfile-reporter-pretty';
import {execa} from 'execa';
import config, {createConfig} from './config.js';
import {createRules} from './rules/index.js';
import {fetchGitHubData} from './lib/github-api.js';
import findReadmeFile from './lib/find-readme-file.js';
import codeOfConductRule from './rules/code-of-conduct.js';
const ruleIdFromPlugin = plugin => plugin?.name?.replace(/^remark-lint:/, '');
const getConfiguredPlugins = config => Array.isArray(config) ? config : (config?.plugins ?? []);
const getDirectRuleIds = config => {
const ruleIds = new Set();
for (const plugin of getConfiguredPlugins(config)) {
const rule = Array.isArray(plugin) ? plugin[0] : plugin;
const ruleId = ruleIdFromPlugin(rule);
if (ruleId) {
ruleIds.add(ruleId);
}
}
return ruleIds;
};
const getWarnRuleIds = config => {
const warnRuleIds = new Set();
for (const plugin of getConfiguredPlugins(config)) {
if (!Array.isArray(plugin)) {
continue;
}
const [rule, options] = plugin;
if (!Array.isArray(options) || options[0] !== 'warn') {
continue;
}
const ruleId = ruleIdFromPlugin(rule);
if (ruleId) {
warnRuleIds.add(ruleId);
}
}
return warnRuleIds;
};
const resolveOptions = async options => {
let projectWebsite;
if (options.repoURL && !options.config) {
const data = await fetchGitHubData(options.repoURL);
projectWebsite = data?.homepage;
}
return {
...options,
config: options.config ?? (options.repoURL ? createConfig(createRules({repoURL: options.repoURL, projectWebsite})) : config),
filename: options.filename ?? 'readme.md',
};
};
const lint = async options => {
options = await resolveOptions(options);
const readmeFile = globbySync(options.filename.replaceAll('\\', '/'), {caseSensitiveMatch: false})[0];
if (!readmeFile) {
throw new Error(`Couldn't find the file ${options.filename}`);
}
const readmeVFile = readVFileSync(path.resolve(readmeFile));
const {dirname} = readmeVFile;
readmeVFile.repoURL = options.repoURL; // Pass repoURL to all rules
const processTasks = [{
vfile: readmeVFile,
plugins: options.config,
}];
const codeOfConductFile = globbySync(['{code-of-conduct,code_of_conduct}.md', '.github/{code-of-conduct,code_of_conduct}.md'], {caseSensitiveMatch: false, cwd: dirname})[0];
if (codeOfConductFile) {
const codeOfConductVFile = readVFileSync(path.resolve(dirname, codeOfConductFile));
codeOfConductVFile.repoURL = options.repoURL;
processTasks.push({
vfile: codeOfConductVFile,
plugins: [[codeOfConductRule, ['error']]],
});
}
return Promise.all(processTasks.map(({vfile, plugins}) => remark().use(plugins).process(vfile)));
};
lint.report = async options => {
const spinner = ora('Linting').start();
try {
await lint._report(options, spinner);
} catch (error) {
spinner.fail(error.message);
process.exitCode = 1;
}
};
lint._report = async (options, spinner) => {
let temporary = null;
if (isUrl(options.filename)) {
if (!isGithubUrl(options.filename, {repository: true})) {
throw new Error(`Invalid GitHub repo URL: ${options.filename}`);
}
temporary = temporaryDirectory();
await execa('git', ['clone', '--depth', '1', '--', options.filename, temporary]);
const readme = findReadmeFile(temporary);
if (!readme) {
await rmfr(temporary);
throw new Error(`Unable to find valid readme for "${options.filename}"`);
}
options.repoURL = options.filename;
options.filename = readme;
}
// Resolve here so we can inspect the final config for severity classification. `lint()` calls `resolveOptions` again, but it's a no-op once `config` is set.
options = await resolveOptions(options);
const vfiles = await lint(options);
const directRuleIds = getDirectRuleIds(options.config);
const warnRuleIds = getWarnRuleIds(options.config);
const messages = [];
for (const vfile of vfiles) {
vfile.path = path.basename(vfile.path);
messages.push(...vfile.messages);
}
if (temporary) {
await rmfr(temporary);
}
if (messages.length === 0) {
spinner.succeed();
if (options.reporter) {
console.log(options.reporter([]));
}
return;
}
/*
Classify each message as error or warning.
remark-lint defaults a rule's severity to warn (`fatal: false`) when no severity is passed, so a plain `[plugin]` entry would pass-through as non-fatal. To preserve the historical "any violation fails the run" behavior, any message from a rule configured directly at the top level is treated as an error unless it was explicitly given `['warn']`. Messages from transitive rules (pulled in via presets) are left as warnings.
- `fatal === true`: always an error (plugin called `file.fail`, or severity was `['error']`).
- Rule in `warnRuleIds`: explicit `['warn']` — warning.
- `fatal === false` and rule not in our top-level config: transitive — warning.
- Otherwise (direct rule, non-fatal message): error.
*/
const hasErrors = messages.some(message => {
if (message.fatal === true) {
return true;
}
if (warnRuleIds.has(message.ruleId)) {
return false;
}
if (message.fatal === false && !directRuleIds.has(message.ruleId)) {
return false;
}
return true;
});
if (hasErrors) {
spinner.fail();
process.exitCode = 1;
} else {
spinner.succeed();
}
const reporter = options.reporter || vfileReporterPretty;
console.log(reporter(vfiles));
};
export default lint;