-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathgenerate-security-test-summary.ts
executable file
·65 lines (58 loc) · 1.98 KB
/
generate-security-test-summary.ts
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
#!/usr/bin/env ts-node
import { execFile } from 'child_process';
import { promisify } from 'util';
import ts from 'typescript';
import { readFile } from 'fs/promises';
import path from 'path';
const securityTestTag = '@securityTest';
(async function () {
const template = await readFile(
path.join(__dirname, 'security-test-summary-template.md'),
'utf8'
);
const gitGrepResult = await promisify(execFile)('git', [
'grep',
'-Fz',
securityTestTag,
]);
const files = gitGrepResult.stdout
.split('\n')
.map((file) => file.split('\0')[0])
.filter(Boolean);
const comments = new Set();
for (const file of files) {
const program = ts.createProgram([file], { allowJs: true });
const sourceFile = program.getSourceFile(file)!;
const text = sourceFile.getFullText();
ts.forEachChild(sourceFile, (node) => {
for (const { pos, end } of [
...(ts.getLeadingCommentRanges(text, node.pos) || []),
...(ts.getTrailingCommentRanges(text, node.pos) || []),
]) {
let commentText = text.substring(pos, end).trim();
if (!commentText.includes(securityTestTag)) continue;
if (!commentText.startsWith('/**'))
throw new Error(
`${securityTestTag} comments must be multiline doc comments`
);
commentText = commentText.replace(/^\s*\/\**\s*/gm, ''); // strip /**
commentText = commentText.replace(/\s*\*\/$/gm, ''); // strip */
commentText = commentText.replace(/^\s*\*/gm, ''); // strip *
commentText = commentText.replace(`${securityTestTag}`, '##');
commentText = commentText
.split('\n')
.map((line) => line.trim())
.join('\n');
commentText += `\n\n<!-- Source File: \`${file}\` -->\n`;
comments.add(commentText);
}
});
}
process.stdout.write(
template.replace(/__SUMMARY__|\*\*SUMMARY\*\*/, [...comments].join('\n\n'))
);
})().catch((err) => {
queueMicrotask(() => {
throw err;
});
});