-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrelease-sanity-check.js
More file actions
143 lines (117 loc) · 4.04 KB
/
release-sanity-check.js
File metadata and controls
143 lines (117 loc) · 4.04 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const childProcess = require('child_process')
const VERSION = getVersion()
const PACKAGE_JSON_MAP = getWorkspacePackageJsons()
const CHANGELOG_MD_CONTENT = readFile('../CHANGELOG.md')
const issuesFound = []
function runAllValidations () {
if (!VERSION) {
return reportCheckSkipped()
}
validateSemverCompatibility()
validatePackageJsonVersion()
validateChangelogHasVersion()
validateChangelogHasVersionOnTop()
validateChangelogAnchorsLegend()
report()
}
function validateSemverCompatibility () {
const semverRe = /^\d+\.\d+\.\d+((-rc|-x)\.\d+)?$/i
if (!semverRe.test(VERSION)) {
issuesFound.push(`Version ${VERSION} is not semver compatible`)
}
}
function validatePackageJsonVersion () {
Object.entries(PACKAGE_JSON_MAP).forEach(([pack, packageJSON]) => {
if (packageJSON.version !== VERSION) {
issuesFound
.push(`@distributedlab/${pack} package.json version should equal ${VERSION}, got ${packageJSON.version}`)
}
})
}
function validateChangelogHasVersion () {
const todayYmd = new Date().toISOString().split('T')[0] // YYYY-MM-DD
const releaseTagRe =
new RegExp(`^## \\[${escapeRe(VERSION)}\\] - ${todayYmd}$`, 'm')
if (!releaseTagRe.test(CHANGELOG_MD_CONTENT)) {
issuesFound
.push(`"## [${VERSION}] - ${todayYmd}" is absent in CHANGELOG.md`)
}
}
function validateChangelogHasVersionOnTop () {
const todayYmd = new Date().toISOString().split('T')[0] // YYYY-MM-DD
const releaseTagIsNotTopRe = new RegExp(
'## \\[\\d+\\.\\d+\\.\\d+((-rc|-x)\\.\\d+)?\\][\\s\\S]*' + // any other tag
`## \\[${escapeRe(VERSION)}\\] - ${todayYmd}`, // the new
'i'
)
if (releaseTagIsNotTopRe.test(CHANGELOG_MD_CONTENT)) {
issuesFound.push(`The ${VERSION} is not the top tag in CHANGELOG.md`)
}
}
function validateChangelogAnchorsLegend () {
const baseRepoUrl = 'https://github.com/distributed-lab/web-kit'
const anyReleaseTagRe =
/## \[\d+\.\d+\.\d+((-rc|-x)\.\d+)?\] - \d{4}-\d{2}-\d{2}/gi
const expectedAnchorsLegend =
`[Unreleased]: ${baseRepoUrl}/compare/${VERSION}...HEAD\n` +
CHANGELOG_MD_CONTENT
.match(anyReleaseTagRe)
?.map(tag => tag.match(/\[(.*)\]/)[1])
?.map((cur, curId, arr) => {
return curId === arr.length - 1
? `[${cur}]: ${baseRepoUrl}/releases/tag/${cur}`
: `[${cur}]: ${baseRepoUrl}/compare/${arr[curId + 1]}...${cur}`
})
?.join('\n')
if (!CHANGELOG_MD_CONTENT.includes(expectedAnchorsLegend)) {
issuesFound.push(`The anchors legend is invalid, should be:\n${expectedAnchorsLegend}`)
}
}
function report () {
/* eslint-disable no-console */
if (issuesFound.length) {
console.error(chalk`{red Release sanity check for {yellow ${VERSION}} failed!}`)
for (const issue of issuesFound) {
console.error(chalk`{red ${issue}}`)
}
process.exitCode = 1
} else {
console.log(chalk`{green Release sanity check for {yellow ${VERSION}} passed}`)
}
/* eslint-enable no-console */
}
function reportCheckSkipped () {
/* eslint-disable-next-line no-console */
console.log(chalk.gray('No version tag found, skipping release sanity check...'))
}
function getVersion () {
if (process.argv[2]) {
return process.argv[2]
}
const refsReport = exec('git log -1 --format="%D"').toString()
const versionMatch = refsReport.match(/tag: ([\w\d\-_.]+)/i)
return versionMatch ? versionMatch[1] : ''
}
function getWorkspacePackageJsons () {
const dirs = fs.readdirSync(path.resolve(__dirname, '../packages'))
return dirs.reduce((acc, dir) => {
acc[dir] = require(path.resolve(__dirname, `../packages/${dir}/package.json`))
return acc
}, {})
}
function readFile (relativePath) {
const resolvedPath = path.resolve(__dirname, relativePath)
return fs.readFileSync(resolvedPath, 'utf8')
}
function escapeRe (string) {
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}
function exec (command) {
return childProcess.execSync(command, {
cwd: path.resolve(__dirname, '..')
})
}
runAllValidations()