-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (93 loc) · 2.85 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
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
'use strict';
const path = require('path');
const isUrl = require('is-url-superb');
const isGithubUrl = require('is-github-url');
const ora = require('ora');
const remark = require('remark');
const gitClone = require('git-clone');
const globby = require('globby');
const pify = require('pify');
const rmfr = require('rmfr');
const tempy = require('tempy');
const toVfile = require('to-vfile');
const vfileReporterPretty = require('vfile-reporter-pretty');
const config = require('./config');
const findReadmeFile = require('./lib/find-readme-file');
const codeOfConductRule = require('./rules/code-of-conduct');
const lint = options => {
options = {
config,
filename: 'readme.md',
...options
};
const readmeFile = globby.sync(options.filename, {nocase: true})[0];
if (!readmeFile) {
return Promise.reject(new Error(`Couldn't find the file ${options.filename}`));
}
const readmeVFile = toVfile.readSync(path.resolve(readmeFile));
const {dirname} = readmeVFile;
const processTasks = [{
vfile: readmeVFile,
plugins: options.config
}];
const codeOfConductFile = globby.sync(['{.github/,}{code-of-conduct,code_of_conduct}.md'], {nocase: true, cwd: dirname})[0];
if (codeOfConductFile) {
const codeOfConductVFile = toVfile.readSync(path.resolve(dirname, codeOfConductFile));
codeOfConductVFile.repoURL = options.repoURL;
processTasks.push({
vfile: codeOfConductVFile,
plugins: [codeOfConductRule]
});
}
return Promise.all(processTasks.map(({vfile, plugins}) => pify(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 temp = null;
if (isUrl(options.filename)) {
if (!isGithubUrl(options.filename, {repository: true})) {
throw new Error(`Invalid GitHub repo URL: ${options.filename}`);
}
temp = tempy.directory();
await pify(gitClone)(options.filename, temp);
const readme = findReadmeFile(temp);
if (!readme) {
await rmfr(temp);
throw new Error(`Unable to find valid readme for "${options.filename}"`);
}
options.repoURL = options.filename;
options.filename = readme;
}
const vfiles = await lint(options);
const messages = [];
for (const vfile of vfiles) {
vfile.path = path.basename(vfile.path);
messages.push(...vfile.messages);
}
if (temp) {
await rmfr(temp);
}
if (messages.length === 0) {
spinner.succeed();
if (options.reporter) {
console.log(options.reporter([]));
}
return;
}
for (const message of messages) {
message.fatal = true; // TODO: because of https://github.com/wooorm/remark-lint/issues/65
}
spinner.fail();
process.exitCode = 1;
const reporter = options.reporter || vfileReporterPretty;
console.log(reporter(vfiles));
};
module.exports = lint;