/
Changelogs.DISABLED.js
49 lines (41 loc) · 1.87 KB
/
Changelogs.DISABLED.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
// @flow
import os from 'os';
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import { Workspaces } from '@adeira/monorepo-utils';
// https://keepachangelog.com/en/1.0.0/
Workspaces.iterateWorkspaces(packageJSONLocation => {
const packagePath = path.dirname(packageJSONLocation);
// $FlowAllowDynamicImport
const packageJson = require(packageJSONLocation);
if (semver.gte(packageJson.version, '1.1.0')) {
const changelogPath = path.join(packagePath, 'CHANGELOG.md');
test(`${changelogPath}`, () => {
expect(fs.existsSync(changelogPath) === true).toGiveHelp(
`Changelog doesn't exist: ${changelogPath}`,
);
const changelog = fs.readFileSync(changelogPath).toString();
const changelogRows = changelog.split(os.EOL);
// first part of the changelog is dedicated to unreleased changes (can be empty)
expect(changelogRows[0] === '# Unreleased').toGiveHelp(
'Changelog must contain section "Unreleased" even though it is empty.',
);
// changelog should contain the last version number (patches are ignored)
const majorVersion = semver.major(packageJson.version);
const minorVersion = semver.minor(packageJson.version);
expect(changelog).toMatch(new RegExp(`\n# ${majorVersion}.${minorVersion}.0`));
const versionTitleRegexp = /# (?<version>[0-9]+\.[0-9]+\.[0-9])+/g;
let result;
while ((result = versionTitleRegexp.exec(changelog)) !== null) {
const foundVersion = result?.groups?.version ?? '0.0.0';
const latestVersion = packageJson.version;
if (semver.gt(foundVersion, latestVersion)) {
throw new Error(
`Changelog contains information about unreleased version ${foundVersion} but the latest version should be ${latestVersion}. Please use "Unreleased" section instead.`,
);
}
}
});
}
});