-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·137 lines (113 loc) · 4.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
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
#! /usr/bin/env node
/* eslint-disable no-console */
const globby = require('globby');
const { promisify } = require('util');
const readFile = promisify(require('fs').readFile);
const markdownLinkCheckOrig = require('markdown-link-check');
const async = require('async');
const _ = require('lodash');
const path = require('path');
const tsm = require('teamcity-service-messages');
const parseArgs = require('minimist');
require('colors'); // magic that allows us to use colors in our console output
const cp = require('child_process');
const util = require('util');
const exec = util.promisify(cp.exec);
const userArgs = parseArgs(process.argv.slice(2));
const cwd = process.cwd();
/* eslint-disable import/no-dynamic-require */
const whitelist = (userArgs.whitelist) ? _.map(require(path.join(cwd, userArgs.whitelist)), 'link') : [];
/* eslint-enable import/no-dynamic-require */
const mapLimit = promisify(async.mapLimit);
const globPatterns = ['**/*.md', '!**/*icd*.md', '!node_modules/**/*.md'];
const allMarkdownFilesPromise = globby(globPatterns);
function markdownLinkCheck(file, opts) {
return new Promise((resolve, reject) => {
markdownLinkCheckOrig(file, opts, (err, results) => {
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
}
async function checkLinksInFile(file) {
try {
const fullPath = `file:///${path.join(cwd, file)}`;
const opts = { baseUrl: path.dirname(fullPath) };
const content = await readFile(file, { encoding: 'utf8' });
const allLinks = await markdownLinkCheck(content, opts);
const deadLinks = allLinks
.filter(link => link.status === 'dead')
.map(l => ({ file, link: l.link, whitelisted: whitelist.indexOf(l.link) >= 0 }));
return deadLinks;
} catch (e) {
console.log(e);
return [];
}
}
// files is a promise<[string]>
async function checkLinksInFiles(filesToCheck) {
const result = await mapLimit(filesToCheck, 10, checkLinksInFile);
return _.flatten(result).map(value =>
({ file: value.file, link: value.link, whitelisted: value.whitelisted }));
}
/**
* Find all the files that differ from specified branch
*/
async function getChangedFiles(base_branch) {
// the diff-filter switch is used to exclude deleted files.
// obviously we can't check links in a file that has been deleted.
const { stdout } = await exec(`git diff --name-only --diff-filter=d ${base_branch}`);
// output will be a list of files: one per line
return stdout
.trim()
.split(/\n/)
.filter(entry => entry.endsWith('.md'));
}
// The changed switch can optionally take the name of a base branch
// This base branch is used as the base in the comparison to see if
// anything changed. If no branch is specified, it defaults to master
function getBaseBranch(changed) {
const defaultBranch = 'master';
if (!changed) return defaultBranch;
if (changed === true) return defaultBranch;
return changed;
}
async function main() {
const base_branch = getBaseBranch(userArgs.changed);
const allMarkdownFiles = await allMarkdownFilesPromise;
const changedMarkdownFiles = await getChangedFiles(base_branch);
const filesToCheck = userArgs.changed ? changedMarkdownFiles : allMarkdownFiles;
const allDeadLinks = await checkLinksInFiles(filesToCheck);
const whiteListedDeadLinks = allDeadLinks.filter(v => v.whitelisted);
const notWhiteListedDeadLinks = allDeadLinks.filter(v => !v.whitelisted);
if (userArgs.reporter === 'teamcity') {
// Register the potential inspection types
tsm.inspectionType({
id: 'LINK001', name: 'no-dead-links', description: 'Reports links that were not reachable.', category: 'Document issues',
});
tsm.inspectionType({
id: 'LINK002',
name: 'no-whitelisted-dead-links',
description: 'Reports links that were on a whitelist. These are links that we know may not be reachable by an automated build tool. This inspection is just meant as a informational message.',
category: 'Document issues',
});
whiteListedDeadLinks.forEach(v => tsm.inspection({
typeId: 'LINK002', message: `Whitelisted dead link: ${v.link}`, file: v.file, SEVERITY: 'INFO',
}));
if (notWhiteListedDeadLinks.length > 0) {
tsm.buildProblem({ description: 'Dead links detected.' });
}
notWhiteListedDeadLinks.forEach(v => tsm.inspection({
typeId: 'LINK001', message: `Dead link: ${v.link}`, file: v.file, SEVERITY: 'ERROR',
}));
} else {
whiteListedDeadLinks
.forEach(value => console.log(`WARN: '${value.link.yellow}' in file '${value.file.green}' could not be reached but is whitelisted.`));
notWhiteListedDeadLinks
.forEach(value => console.log(`ERROR: '${value.link.red}' in file '${value.file.blue}' could not be reached.`));
}
}
main();