-
Notifications
You must be signed in to change notification settings - Fork 0
/
release-changelog.js
43 lines (33 loc) · 1001 Bytes
/
release-changelog.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
const { createReadStream, createWriteStream } = require('node:fs');
const path = require('node:path');
const readline = require('readline');
const FILENAME = 'CHANGELOG.md';
const FILE_HEADER_END_LINE = 4;
const inputPath = path.normalize(`./${FILENAME}`);
const outputPath = path.normalize(`./${FILENAME}.tmp`);
const readStream = createReadStream(inputPath);
const writeStream = createWriteStream(outputPath);
const lineReader = readline.createInterface({
input: readStream,
output: writeStream,
crlfDelay: Infinity
});
const versionHeaderPattern = /\[\d+\.\d+(\.\d+)?\]/;
let versionHeaderFound = 0;
let lineNumber = 0;
lineReader.on('line', line => {
lineNumber += 1;
if (lineNumber > FILE_HEADER_END_LINE) {
if (versionHeaderPattern.test(line)) {
versionHeaderFound += 1;
}
if (versionHeaderFound < 2) {
writeStream.write(line + '\n');
}
}
});
lineReader.on('close', () => {
lineReader.close();
readStream.close();
writeStream.close();
});