-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBlockChanges.mjs
129 lines (120 loc) · 3.64 KB
/
getBlockChanges.mjs
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
import fs from "fs";
import { getItemsForFile } from "./lib/freshdoc-lib.mjs";
const referenceMap = {};
function buildMarkdownReference(
sourceMarkdown,
markdownFreshDocReferenceLineNumber
) {
return `${sourceMarkdown.replace(
/\//g,
"-"
)}:${markdownFreshDocReferenceLineNumber}`;
}
function buildCodeReference(referencedCodeFilename, codeBlockRangeStart) {
return `${referencedCodeFilename.replace(/\//g, "-")}:${codeBlockRangeStart}`;
}
export async function getAllChanges(filePath) {
const { codeBlocks } = await getItemsForFile(filePath);
//consolidate codeBlocks for the same source file
const codeBlocksBySource = {};
for (const codeBlock of codeBlocks) {
const { sourceMarkdown } = codeBlock;
if (!codeBlocksBySource[sourceMarkdown]) {
codeBlocksBySource[sourceMarkdown] = [];
}
codeBlocksBySource[sourceMarkdown].push(codeBlock);
}
for (const codeBlockList of Object.values(codeBlocksBySource).sort(
(a, b) => a[0].fastDocLineNumber - b[0].fastDocLineNumber
)) {
const markdownFileContents = fs.readFileSync(
codeBlockList[0].sourceMarkdown,
"utf8"
);
let markdownLines = markdownFileContents.split("\n");
for (const codeBlock of codeBlockList) {
const {
sourceMarkdown,
referencedCodeFilename,
codeBlockRangeStart,
codeBlockRangeEnd,
markdownFreshDocReferenceLineNumber,
} = codeBlock;
const fileContents = fs.readFileSync(referencedCodeFilename, "utf8");
const lines = fileContents.split("\n");
const codeBlockLines = lines.slice(
codeBlockRangeStart - 1,
codeBlockRangeEnd
);
const previousLines = markdownLines.slice(
0,
markdownFreshDocReferenceLineNumber
);
//the next lines should be the lines after the codeBlock
const endOfCodeBlockRegex = /```/gi;
const markdownCodeBlock = [];
//account for the commented reference to the location by adding 1
for (
let i = markdownFreshDocReferenceLineNumber + 1;
i < markdownLines.length;
i++
) {
if (endOfCodeBlockRegex.test(markdownLines[i])) {
break;
} else {
markdownCodeBlock.push(markdownLines[i]);
}
}
referenceMap[
buildCodeReference(referencedCodeFilename, codeBlockRangeStart)
] = codeBlockLines.join("\n");
referenceMap[
buildMarkdownReference(
sourceMarkdown,
markdownFreshDocReferenceLineNumber
)
] = markdownCodeBlock.join("\n");
}
}
function markdownAndCodeAreTheSame(markdown, code) {
if (markdown === code) {
return true;
}
return false;
}
const filesWithErrors = [];
for (const codeBlock of codeBlocks) {
const {
sourceMarkdown,
referencedCodeFilename,
codeBlockRangeStart,
codeBlockRangeEnd,
markdownFreshDocReferenceLineNumber,
} = codeBlock;
const code =
referenceMap[
buildCodeReference(referencedCodeFilename, codeBlockRangeStart)
];
const markdown =
referenceMap[
buildMarkdownReference(
sourceMarkdown,
markdownFreshDocReferenceLineNumber
)
];
if (!markdownAndCodeAreTheSame(markdown, code)) {
filesWithErrors.push(
`${sourceMarkdown}:${markdownFreshDocReferenceLineNumber} != ${referencedCodeFilename}:${codeBlockRangeStart}`
);
}
}
if (filesWithErrors.length > 0) {
console.log();
console.log("FreshDoc️ found differences the Docs and the Code");
for (const file of filesWithErrors) {
console.log(file);
}
console.log();
process.exit(0);
}
}