|
2 | 2 | const fs = require('fs') |
3 | 3 | const path = require('path') |
4 | 4 | const { userFacingChanges } = require('./change-categories') |
5 | | - |
6 | | -const isValidSection = (section) => { |
7 | | - return Object.values(userFacingChanges).some((set) => { |
8 | | - return set.section === section |
9 | | - }) |
10 | | -} |
| 5 | +const userFacingSections = Object.values(userFacingChanges).map(({ section }) => section) |
11 | 6 |
|
12 | 7 | async function parseChangelog (pendingRelease = true) { |
13 | 8 | const changelog = fs.readFileSync(path.join(__dirname, '..', '..', 'cli', 'CHANGELOG.md'), 'utf8') |
@@ -38,35 +33,39 @@ async function parseChangelog (pendingRelease = true) { |
38 | 33 |
|
39 | 34 | if (index === 1) { |
40 | 35 | if (!/^## \d+\.\d+\.\d+/.test(line)) { |
41 | | - throw new Error(`Expected line number ${index} to include "## x.x.x"`) |
| 36 | + throw new Error(`Expected line number ${index + 1} to include "## x.x.x"`) |
42 | 37 | } |
43 | 38 |
|
44 | 39 | sections['version'] = line |
45 | 40 | } else if (index === 3) { |
46 | 41 | nextKnownLineBreak = index + 1 |
47 | 42 | if (pendingRelease && !/_Released \d+\/\d+\/\d+ \(PENDING\)_/.test(line)) { |
48 | | - throw new Error(`Expected line number ${index} to include "_Released xx/xx/xxxx (PENDING)_"`) |
| 43 | + throw new Error(`Expected line number ${index + 1} to include "_Released xx/xx/xxxx (PENDING)_"`) |
49 | 44 | } else if (!pendingRelease && !/_Released \d+\/\d+\/\d+__/.test(line)) { |
50 | | - throw new Error(`Expected line number ${index} to include "_Released xx/xx/xxxx_"`) |
| 45 | + throw new Error(`Expected line number ${index + 1} to include "_Released xx/xx/xxxx_"`) |
51 | 46 | } |
52 | 47 |
|
53 | 48 | sections['releaseDate'] = line |
54 | 49 | } else if (index === nextKnownLineBreak) { |
55 | 50 | if (line !== '') { |
56 | | - throw new Error(`Expected line number ${index} to be a line break`) |
| 51 | + throw new Error(`Expected line number ${index + 1} to be a line break`) |
57 | 52 | } |
58 | 53 | } else { |
59 | | - const result = /\*\*([A-Z])\w+:\*\*/.exec(line) |
| 54 | + const result = /\*\*.+?:\*\*/.exec(line) |
| 55 | + |
| 56 | + if (currentSection === '' && !result) { |
| 57 | + throw new Error(`Expected line number ${index + 1} to be a valid section header. Received ${line}. Expected one of ...\n - ${userFacingSections.join('\n - ')}`) |
| 58 | + } |
60 | 59 |
|
61 | 60 | if (result) { |
62 | 61 | const section = result[0] |
63 | 62 |
|
64 | | - if (!isValidSection(section)) { |
65 | | - throw new Error(`Expected line number ${index} to be a valid section header. Received ${section}. Expected one of ...`) |
| 63 | + if (!userFacingSections.includes(section)) { |
| 64 | + throw new Error(`Expected line number ${index + 1} to be a valid section header. Received ${section}. Expected one of ...\n - ${userFacingSections.join('\n - ')}`) |
66 | 65 | } |
67 | 66 |
|
68 | 67 | if (result === currentSection || sections[section]) { |
69 | | - throw new Error(`Duplicate section header of "${section}" on line number ${index}. Condense change content under a single section header.`) |
| 68 | + throw new Error(`Duplicate section header of "${section}" on line number ${index + 1}. Condense change content under a single section header.`) |
70 | 69 | } |
71 | 70 |
|
72 | 71 | if (currentSection !== '') { |
|
0 commit comments