/
index.js
75 lines (59 loc) · 1.71 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
'use strict';
const path = require('path');
const isUrl = require('is-url-superb');
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 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 run = remark().use(options.config).process;
const file = toVfile.readSync(path.resolve(readmeFile));
return pify(run)(file);
};
lint.report = async options => {
const spinner = ora('Linting').start();
let temp = null;
if (isUrl(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.filename = readme;
}
const file = await lint(options);
const {messages} = file;
if (temp) {
await rmfr(temp);
}
if (messages.length === 0) {
spinner.succeed();
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;
file.path = path.basename(file.path);
console.log(vfileReporterPretty([file]));
};
module.exports = lint;