|
| 1 | +import {existsSync} from 'node:fs'; |
| 2 | +import {green, Log} from '../../../utils/logging.js'; |
| 3 | +import {join} from 'node:path'; |
| 4 | +import {writeFile, readFile} from 'node:fs/promises'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Updates the `renovate.json` configuration file to include a new base branch. |
| 8 | + * |
| 9 | + * @param projectDir - The project directory path. |
| 10 | + * @param newBranchName - The name of the new branch to add to the base branches list. |
| 11 | + * @returns A promise that resolves to an string containing the path to the modified `renovate.json` file, |
| 12 | + * or null if config updating is disabled. |
| 13 | + */ |
| 14 | +export async function updateRenovateConfig( |
| 15 | + projectDir: string, |
| 16 | + newBranchName: string, |
| 17 | +): Promise<string | null> { |
| 18 | + const renovateConfigPath = join(projectDir, 'renovate.json'); |
| 19 | + if (!existsSync(renovateConfigPath)) { |
| 20 | + Log.warn(` ✘ Skipped updating Renovate config as it was not found.`); |
| 21 | + |
| 22 | + return null; |
| 23 | + } |
| 24 | + |
| 25 | + const config = await readFile(renovateConfigPath, 'utf-8'); |
| 26 | + const configJson = JSON.parse(config) as Record<string, unknown>; |
| 27 | + const baseBranches = configJson.baseBranches; |
| 28 | + if (!Array.isArray(baseBranches) || baseBranches.length !== 2) { |
| 29 | + Log.warn( |
| 30 | + ` ✘ Skipped updating Renovate config: "baseBranches" must contain exactly 2 branches.`, |
| 31 | + ); |
| 32 | + |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + configJson.baseBranches = ['main', newBranchName]; |
| 37 | + await writeFile(renovateConfigPath, JSON.stringify(configJson, undefined, 2)); |
| 38 | + Log.info(green(` ✓ Updated Renovate config.`)); |
| 39 | + |
| 40 | + return renovateConfigPath; |
| 41 | +} |
0 commit comments