|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * script to auto update CHANGELOG.md file |
| 5 | + */ |
| 6 | +import fs from 'node:fs' |
| 7 | +import path from 'node:path' |
| 8 | +import url from 'node:url' |
| 9 | + |
| 10 | +import * as core from '@actions/core' |
| 11 | +import * as dotenv from 'dotenv' |
| 12 | +import shell from 'shelljs' |
| 13 | + |
| 14 | +import { createPreReleaseVer, getCurrentDate } from './utils.js' |
| 15 | +import pkg from '../../../lerna.json' with { type: 'json' } |
| 16 | + |
| 17 | +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)) |
| 18 | +const root = path.resolve(__dirname, '..', '..', '..') |
| 19 | +const changelogPath = path.join(root, 'CHANGELOG.md') |
| 20 | + |
| 21 | +dotenv.config({ path: path.join(root, '.env') }) |
| 22 | + |
| 23 | +if (!process.env.GITHUB_AUTH) { |
| 24 | + shell.exec('git checkout -- .') |
| 25 | + console.error( |
| 26 | + 'Please export a "GITHUB_AUTH" access token to generate the changelog.\n' + |
| 27 | + 'See also https://github.com/webdriverio/vscode-webdriverio/blob/main/CONTRIBUTING.md#release-new-version' |
| 28 | + ) |
| 29 | + process.exit(1) |
| 30 | +} |
| 31 | + |
| 32 | +const createInitial = (version: string) => { |
| 33 | + return `## v${version} (${getCurrentDate()}) |
| 34 | +
|
| 35 | +#### :rocket: New Feature |
| 36 | +* All packages |
| 37 | + * Initial release |
| 38 | +
|
| 39 | +` |
| 40 | +} |
| 41 | + |
| 42 | +console.log('Start generating changelog...') |
| 43 | +const result = shell.exec('pnpm exec lerna-changelog --next-version-from-metadata', { silent: true }) |
| 44 | + |
| 45 | +const isNoGitTags = result.stdout.match(new RegExp('fatal: No names found')) |
| 46 | + |
| 47 | +if (result.code !== 0 && !isNoGitTags) { |
| 48 | + console.log('ERROR: Failed to generate changelog.') |
| 49 | + process.exit(1) |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * update local tags |
| 54 | + */ |
| 55 | +const BANNER = ` |
| 56 | +####################### |
| 57 | +### ### |
| 58 | +### CHANGELOG ### |
| 59 | +### ### |
| 60 | +#######################` |
| 61 | + |
| 62 | +const orgNewChangelog = isNoGitTags ? createInitial(pkg.version) : result.stdout |
| 63 | + |
| 64 | +const isPreRelease = Boolean(process.env.VSCODE_WDIO_PRE_RELEASE_PATCH_NUMBER || '') |
| 65 | + |
| 66 | +const newChangelog = !isPreRelease |
| 67 | + ? orgNewChangelog |
| 68 | + : orgNewChangelog + `\n\n> Package as v${createPreReleaseVer(pkg.version)} ` |
| 69 | + |
| 70 | +let changelogContent = fs.readFileSync(changelogPath, 'utf8') |
| 71 | +changelogContent = changelogContent.replace('---', '---\n' + newChangelog) |
| 72 | +fs.writeFileSync(changelogPath, changelogContent, 'utf8') |
| 73 | + |
| 74 | +console.log(BANNER) |
| 75 | +console.log(newChangelog) |
| 76 | +core.setOutput('changelog', newChangelog) |
0 commit comments