-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.prepare_release.mjs
143 lines (123 loc) · 4.13 KB
/
.prepare_release.mjs
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
import { readFile, writeFile, stat } from "node:fs/promises"
import { basename } from "path"
const sxplrGithubApi = "https://api.github.com/repos/FZJ-INM1-BDA/siibra-explorer/releases"
const ACTIONS = {
MAJOR: "major",
MINOR: "minor",
BUGFIX: "bugfix"
}
/**
*
* @param {string} version
* @returns {number[]}
*/
function parseVersion(version){
const splittedVersion = version.replace(/^v/, '').split('.')
if (splittedVersion.length !== 3) {
throw new Error(`Expected version to be in the form of v?[0-9]+\.[0-9]+\.[0-9]+, but got ${version}`)
}
const castToNum = splittedVersion.map(v => Number(v))
if (castToNum.some(v => isNaN(v))) {
throw new Error(`Expected version to be in the form of v?[0-9]+\.[0-9]+\.[0-9]+, but got ${version}`)
}
return castToNum
}
/**
*
* @returns {Promise<string>}
*/
async function getLatestReleases(){
const resp = await fetch(sxplrGithubApi)
const result = await resp.json()
const lastRelease = result[0]
const lastTag = lastRelease['tag_name']
return lastTag // n.b. v{major}.{minor}.{bugfix} <-- has 'v' prepended
}
/**
*
* @param {string} version
*/
async function updatePackageJson(version){
const data = await readFile("package.json", "utf-8")
const currPackageJson = JSON.parse(data)
currPackageJson["version"] = version
await writeFile("package.json", JSON.stringify(currPackageJson, null, 2), "utf-8")
}
/**
*
* @param {string} version
*/
async function ensureReleaseNotes(version) {
const placeholder = `# v${version}\n\n`
const pathToReleaseNotes = `docs/releases/v${version}.md`
try {
const fd = await stat(pathToReleaseNotes)
} catch (e) {
await writeFile(pathToReleaseNotes, placeholder, "utf-8")
}
}
/**
*
* @param {string} version
*/
async function ensureMkDocYaml(version){
const pathToMkYaml = `mkdocs.yml`
const lineTobeInserted = ` - v${version}: 'releases/v${version}.md'`
const mkdocsYamlContent = await readFile(pathToMkYaml, "utf-8")
if (mkdocsYamlContent.includes(lineTobeInserted)) {
return
}
const updatedYamlContent = mkdocsYamlContent.replace(" - Release notes:", s => `${s}\n${lineTobeInserted}`)
await writeFile(pathToMkYaml, updatedYamlContent, "utf-8")
}
/**
*
* @param {string} version
*/
async function updateCodemeta(version){
const pathToCodemeta = 'codemeta.json'
const pathToReleaseNotes = `docs/releases/v${version}.md`
const codemetaContent = JSON.parse(await readFile(pathToCodemeta, "utf-8"))
codemetaContent["version"] = version
const releaseNotes = await readFile(pathToReleaseNotes, "utf-8")
codemetaContent["schema:releaseNotes"] = releaseNotes
const date = new Date()
const YYYY = date.getFullYear().toString()
const MM = date.getMonth().toString().padStart(2, "0")
const DD = date.getDate().toString()
codemetaContent["dateModified"] = `${YYYY}-${MM}-${DD}`
await writeFile(pathToCodemeta, JSON.stringify(codemetaContent, null, 4), "utf-8")
}
async function main() {
const filename = basename(import.meta.url)
const args = process.argv.slice(2)
const target = args[0] || ACTIONS.BUGFIX
const helperText = `Usage: node ${filename} {${ACTIONS.MAJOR}|${ACTIONS.MINOR}|${ACTIONS.BUGFIX}|x.y.z} (default: bugfix)`
if (target === "help" || target === "--help") {
console.log(helperText)
return
}
let targetVersion = null
if ([ACTIONS.MAJOR, ACTIONS.MINOR, ACTIONS.BUGFIX].includes(target)) {
const latest = await getLatestReleases()
const latestVersion = parseVersion(latest)
targetVersion = latestVersion
let vIdx = -1
if (target === ACTIONS.MAJOR) vIdx = 0
if (target === ACTIONS.MINOR) vIdx = 1
if (target === ACTIONS.BUGFIX) vIdx = 2
if (vIdx === -1) {
throw new Error(`target is expected to be one of ${ACTIONS.MAJOR}/${ACTIONS.MINOR}/${ACTIONS.BUGFIX}`)
}
latestVersion[vIdx] += 1
targetVersion = latestVersion.join(".")
} else if (target !== ACTIONS.LINT) {
targetVersion = parseVersion(target)
}
await updatePackageJson(targetVersion)
await ensureReleaseNotes(targetVersion)
await ensureMkDocYaml(targetVersion)
await updateCodemeta(targetVersion)
console.log(`Done ${targetVersion}`)
}
main()