Skip to content

Commit 6933385

Browse files
authored
added version check to pre-push script (#170)
When pushing, a script checks that the version in package.json and and the tag are in accordance
1 parent fbf6955 commit 6933385

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

.husky/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
lint-staged
2-
32
#!/bin/sh
3+
44
# Husky pre-commit hook to update companion/HELP.md with all action names and descriptions
55
node scripts/update-actions-md.js
66
git add companion/HELP.md

.husky/pre-push

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
node scripts/check-tag.js

scripts/check-tag.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { execSync } from 'child_process'
2+
import * as fs from 'fs'
3+
4+
function run(cmd) {
5+
try {
6+
return execSync(cmd, { stdio: ['ignore', 'pipe', 'ignore'] })
7+
.toString()
8+
.trim()
9+
} catch {
10+
return null
11+
}
12+
}
13+
14+
if (!fs.existsSync('package.json')) {
15+
throw new Error('package.json not found')
16+
}
17+
18+
const tag = run('git describe --tags --abbrev=0')
19+
20+
if (!tag) {
21+
throw new Error('No git tag found on this branch')
22+
}
23+
24+
if (!/^v\d+(\.\d+){2,3}$/.test(tag)) {
25+
throw new Error(`Latest tag '${tag}' does not match vX.Y.Z or vX.Y.Z.W`)
26+
}
27+
28+
let pkg
29+
try {
30+
pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
31+
} catch {
32+
throw new Error('Cannot parse package.json')
33+
}
34+
35+
const pkgVersion = pkg.version
36+
const tagVersion = tag.slice(1)
37+
38+
if (pkgVersion !== tagVersion) {
39+
throw new Error(`ERROR: package.json version (${pkgVersion}) does not match git tag (${tag})`)
40+
}
41+
42+
console.log(`Git tag '${tag}' matches package.json version '${pkgVersion}'`)

0 commit comments

Comments
 (0)