All files / scripts/package-release/lib config.js

0% Statements 0/23
0% Branches 0/48
0% Functions 0/5
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                           
import { readFileSync, existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, '../../..');
 
let cachedConfig = null;
 
function loadUserConfig() {
  const candidates = [
    path.join(repoRoot, 'package-release.config.js'),
    path.join(repoRoot, 'package-release.config.json'),
  ];
  for (const candidate of candidates) {
    if (existsSync(candidate)) {
      if (candidate.endsWith('.json')) {
        return JSON.parse(readFileSync(candidate, 'utf8'));
      }
      // Dynamic import would be async; for config we read .json synchronously.
      // .js config is handled by the top-level config file exporting via default.
      return null;
    }
  }
  return null;
}
 
const DEFAULTS = {
  tagFormat: '{name}@{version}',
  tagMessage: '{tag}',
  commitHeadline: 'chore(release): publish',
  commitMessage: null,
  releaseBranchFormat: 'chore/release/{date}',
  ignoreChanges: [
    '**/.storybook/**',
    '**/plugins/**',
    '**/scripts/**',
    '**/__tests__/**',
    '**/__stories__/**',
    '**/__specs__/**',
    '**/*.md',
    '**/*.mdx',
    '**/CHANGELOG.md',
    '**/README.md',
  ],
  pr: {
    base: 'develop',
    labels: ['chore', 'package-bump'],
    titleFormat: '@{workItem} chore: bumping package versions for nexus publish {date}',
    workItemEnv: 'SCS_WI',
    releaseConfigPath: 'packages/design-system/.release-config',
    templatePath: '.github/PULL_REQUEST_TEMPLATE.md',
  },
  branches: {
    start: [
      'develop',
      'develop-[0-9]{3}-patch',
    ],
    version: [
      'develop',
      'develop-[0-9]{3}-patch',
      'chore/release/*',
      'release/main/[0-9]{2}-[0-9]{2}-[0-9]{2}',
      'release/main-patch/[0-9]{2}-[0-9]{2}-[0-9]{2}',
    ],
    releaseCycle: [
      'develop',
      'develop-[0-9]{3}-patch',
    ],
    pr: [
      'chore/release/*',
      'release/main/[0-9]{2}-[0-9]{2}-[0-9]{2}',
      'release/main-patch/[0-9]{2}-[0-9]{2}-[0-9]{2}',
    ],
    tag: [
      'main',
      'main-patch',
      'release/[0-9]{3}/**',
    ],
    github: [
      'main',
      'main-patch',
      'release/[0-9]{3}/**',
    ],
  },
};
 
export function getConfig() {
  if (cachedConfig) return cachedConfig;
 
  const userConfig = loadUserConfig() || {};
  cachedConfig = {
    tagFormat: userConfig.tagFormat || DEFAULTS.tagFormat,
    tagMessage: userConfig.tagMessage || DEFAULTS.tagMessage,
    commitHeadline: userConfig.commitHeadline || DEFAULTS.commitHeadline,
    commitMessage: userConfig.commitMessage || DEFAULTS.commitMessage,
    releaseBranchFormat: userConfig.releaseBranchFormat || DEFAULTS.releaseBranchFormat,
    ignoreChanges: userConfig.ignoreChanges || DEFAULTS.ignoreChanges,
    releasePostVersionHooks: userConfig.releasePostVersionHooks || {},
    pr: {
      base: userConfig.pr?.base || DEFAULTS.pr.base,
      labels: userConfig.pr?.labels || DEFAULTS.pr.labels,
      titleFormat: userConfig.pr?.titleFormat || DEFAULTS.pr.titleFormat,
      workItemEnv: userConfig.pr?.workItemEnv || DEFAULTS.pr.workItemEnv,
      releaseConfigPath: userConfig.pr?.releaseConfigPath || DEFAULTS.pr.releaseConfigPath,
      templatePath: userConfig.pr?.templatePath || DEFAULTS.pr.templatePath,
    },
    branches: {
      start: userConfig.branches?.start || DEFAULTS.branches.start,
      version: userConfig.branches?.version || DEFAULTS.branches.version,
      releaseCycle: userConfig.branches?.releaseCycle || DEFAULTS.branches.releaseCycle,
      pr: userConfig.branches?.pr || DEFAULTS.branches.pr,
      tag: userConfig.branches?.tag || DEFAULTS.branches.tag,
      github: userConfig.branches?.github || DEFAULTS.branches.github,
    },
  };
  return cachedConfig;
}
 
export function getRepoRoot() {
  return repoRoot;
}
 
export function getRootPackageJson() {
  return JSON.parse(readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
}
 
export function getLernaJson() {
  const lernaPath = path.join(repoRoot, 'lerna.json');
  if (!existsSync(lernaPath)) return null;
  return JSON.parse(readFileSync(lernaPath, 'utf8'));
}