2022-09-26 18:28:25 +03:00
|
|
|
import fs from 'node:fs'
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-26 19:01:03 +03:00
|
|
|
import { command } from 'execa'
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
import { LogHelper } from '@/helpers/log-helper'
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update version number in files which need version number
|
|
|
|
*/
|
2022-09-03 14:12:41 +03:00
|
|
|
export default (version) =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
const changelog = 'CHANGELOG.md'
|
|
|
|
const tmpChangelog = 'TMP-CHANGELOG.md'
|
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.info(`Generating ${changelog}...`)
|
2022-09-03 14:12:41 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
await command(
|
|
|
|
`git-changelog --changelogrc .changelogrc --template scripts/assets/CHANGELOG-TEMPLATE.md --file scripts/tmp/${tmpChangelog} --version_name ${version}`,
|
|
|
|
{ shell: true }
|
|
|
|
)
|
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(`Error during git-changelog: ${e}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject(e)
|
|
|
|
}
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
try {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.info('Getting remote origin URL...')
|
|
|
|
LogHelper.info('Getting previous tag...')
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
const sh = await command(
|
|
|
|
'git config --get remote.origin.url && git tag | tail -n1',
|
|
|
|
{ shell: true }
|
|
|
|
)
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
const repoUrl = sh.stdout.substr(0, sh.stdout.lastIndexOf('.git'))
|
|
|
|
const previousTag = sh.stdout.substr(sh.stdout.indexOf('\n') + 1).trim()
|
2023-04-02 21:34:50 +03:00
|
|
|
const changelogData = await fs.promises.readFile(changelog, 'utf8')
|
2022-09-03 14:12:41 +03:00
|
|
|
const compareUrl = `${repoUrl}/compare/${previousTag}...v${version}`
|
2023-04-02 21:34:50 +03:00
|
|
|
let tmpData = await fs.promises.readFile(
|
|
|
|
`scripts/tmp/${tmpChangelog}`,
|
|
|
|
'utf8'
|
|
|
|
)
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.success(`Remote origin URL gotten: ${repoUrl}.git`)
|
|
|
|
LogHelper.success(`Previous tag gotten: ${previousTag}`)
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-09-03 14:12:41 +03:00
|
|
|
if (previousTag !== '') {
|
|
|
|
tmpData = tmpData.replace(version, `[${version}](${compareUrl})`)
|
|
|
|
}
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2023-04-02 21:34:50 +03:00
|
|
|
try {
|
|
|
|
await fs.promises.writeFile(changelog, `${tmpData}${changelogData}`)
|
|
|
|
await fs.promises.unlink(`scripts/tmp/${tmpChangelog}`)
|
|
|
|
LogHelper.success(`${changelog} generated`)
|
|
|
|
resolve()
|
|
|
|
} catch (error) {
|
|
|
|
LogHelper.error(`Failed to write into file: ${error}`)
|
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
} catch (e) {
|
2022-09-26 16:29:56 +03:00
|
|
|
LogHelper.error(`Error during git commands: ${e}`)
|
2022-09-03 14:12:41 +03:00
|
|
|
reject(e)
|
2019-02-10 15:26:50 +03:00
|
|
|
}
|
2022-09-03 14:12:41 +03:00
|
|
|
})
|