-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
check-docs-quality.js
executable file
·134 lines (119 loc) · 3.81 KB
/
check-docs-quality.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
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { spawnSync } = require('child_process');
const {
resolve: resolvePath,
join: joinPath,
relative: relativePath,
} = require('path');
const fs = require('fs').promises;
const IGNORED_WHEN_LISTING = [
/^ADOPTERS\.md$/,
/^OWNERS\.md$/,
/^.*[/\\]CHANGELOG\.md$/,
/^.*[/\\]([^\/]+-)?api-report\.md$/,
/^.*[/\\]knip-report\.md$/,
/^docs[/\\]releases[/\\].*-changelog\.md$/,
/^docs[/\\]reference[/\\]/,
/^README-[a-z]{2}_[A-Z]{2}\.md$/,
];
const IGNORED_WHEN_EXPLICIT = [
/^ADOPTERS\.md$/,
/^OWNERS\.md$/,
/^.*[/\\]knip-report\.md$/,
];
const rootDir = resolvePath(__dirname, '..');
// Manual listing to avoid dependency install for listing files in CI
async function listFiles(dir = '') {
const files = await fs.readdir(dir || rootDir);
const paths = await Promise.all(
files
.filter(file => file !== 'node_modules')
.map(async file => {
const path = joinPath(dir, file);
if (IGNORED_WHEN_LISTING.some(pattern => pattern.test(path))) {
return [];
}
if ((await fs.stat(path)).isDirectory()) {
return listFiles(path);
}
if (!path.endsWith('.md')) {
return [];
}
return path;
}),
);
return paths.flat();
}
// Proceed with the script only if Vale linter is installed. Limit the friction and surprises
// caused by the script. In CI, we want to ensure vale linter is run.
async function exitIfMissingVale() {
try {
// eslint-disable-next-line @backstage/no-undeclared-imports
await require('command-exists')('vale');
} catch (e) {
console.log(
`Language linter (vale) was not found. Please install vale linter (https://vale.sh/docs/vale-cli/installation/).\n`,
);
process.exit(process.env.CI ? 1 : 0);
}
}
async function runVale(files) {
const result = spawnSync(
'vale',
['--config', resolvePath(rootDir, '.vale.ini'), ...files],
{
stdio: 'inherit',
},
);
if (result.status !== 0) {
// TODO(Rugvip): This logic was here before but seems a bit odd, could use some verification on windows.
// If it contains system level error. In this case vale does not exist.
if (process.platform !== 'win32' || result.error) {
console.log(`Language linter (vale) generated errors. Please check the errors and review any markdown files that you changed.
Possibly update .github/vale/config/vocabularies/Backstage/accept.txt to add new valid words.\n`);
}
return false;
}
return true;
}
async function main() {
if (process.argv.includes('--ci-args')) {
const files = await listFiles();
process.stdout.write(
// Workaround for not being able to pass arguments to the vale action
JSON.stringify([...files]),
);
return;
}
await exitIfMissingVale();
const absolutePaths = process.argv
.slice(2)
.filter(path => !path.startsWith('-'));
const relativePaths = absolutePaths
.map(path => relativePath(rootDir, path))
.filter(path => !IGNORED_WHEN_EXPLICIT.some(pattern => pattern.test(path)));
const success = await runVale(
relativePaths.length === 0 ? await listFiles() : relativePaths,
);
if (!success) {
process.exit(2);
}
}
main().catch(error => {
console.error(error);
process.exit(1);
});