UNPKG

post-merge

Version:

A reusable library for handling post-merge operations including version bumping and git tagging

209 lines 8.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CONFIG_FILE_NAME = void 0; exports.configFileExists = configFileExists; exports.readConfigFile = readConfigFile; exports.validateConfigFile = validateConfigFile; exports.resolveAccessToken = resolveAccessToken; exports.resolveNodejsMirror = resolveNodejsMirror; exports.convertFileConfigToHotfixConfig = convertFileConfigToHotfixConfig; exports.loadConfiguration = loadConfiguration; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Default config file name */ exports.CONFIG_FILE_NAME = 'post-merge-config.json'; /** * Checks if the config file exists in the current directory */ function configFileExists(configPath) { const filePath = configPath || path.join(process.cwd(), exports.CONFIG_FILE_NAME); return fs.existsSync(filePath); } /** * Reads and parses the config file */ function readConfigFile(configPath) { const filePath = configPath || path.join(process.cwd(), exports.CONFIG_FILE_NAME); if (!fs.existsSync(filePath)) { return {}; } try { const content = fs.readFileSync(filePath, 'utf8'); const config = JSON.parse(content); // Validate config structure validateConfigFile(config); return config; } catch (error) { console.warn(`Warning: Failed to read config file ${filePath}: ${error instanceof Error ? error.message : String(error)}`); return {}; } } /** * Validates the config file structure */ function validateConfigFile(config) { if (typeof config !== 'object' || config === null) { throw new Error('Config file must contain a valid JSON object'); } // Validate versionStrategy if provided if (config.versionStrategy && !['patch', 'prerelease', 'auto'].includes(config.versionStrategy)) { throw new Error('versionStrategy must be one of: patch, prerelease, auto'); } // Validate boolean fields const booleanFields = ['createTags']; for (const field of booleanFields) { if (config[field] !== undefined && typeof config[field] !== 'boolean') { throw new Error(`${field} must be a boolean value`); } } // Validate string fields const stringFields = ['accessTokenProp', 'nodejsImageUrl', 'commitMessageTemplate', 'prereleaseId', 'packageJsonPath', 'gitUserName', 'gitUserEmail']; for (const field of stringFields) { if (config[field] !== undefined && typeof config[field] !== 'string') { throw new Error(`${field} must be a string value`); } } } /** * Resolves access token from environment using the specified property name */ function resolveAccessToken(accessTokenProp, fallbackToken) { if (accessTokenProp) { const tokenFromProp = process.env[accessTokenProp]; if (tokenFromProp) { console.log(`✅ Access token resolved from environment variable: ${accessTokenProp}`); return tokenFromProp; } console.warn(`⚠️ Warning: Environment variable ${accessTokenProp} not found or empty, falling back to default`); } const fallback = fallbackToken || process.env.CI_PUSH_TOKEN || ''; if (fallback) { console.log(`✅ Access token resolved from fallback (CI_PUSH_TOKEN or default)`); } else { console.error(`❌ No access token found! Check your environment variables.`); } return fallback; } /** * Resolves nodejs mirror URL from environment using the specified property name */ function resolveNodejsMirror(nodejsImageUrl) { if (nodejsImageUrl) { const mirrorFromProp = process.env[nodejsImageUrl]; if (mirrorFromProp) { console.log(`✅ Nodejs mirror URL resolved from environment variable: ${nodejsImageUrl}`); return mirrorFromProp; } console.warn(`⚠️ Warning: Environment variable ${nodejsImageUrl} not found or empty, using default npm registry`); } return undefined; } /** * Converts config file format to HotfixConfig format */ function convertFileConfigToHotfixConfig(fileConfig) { const hotfixConfig = {}; // Copy direct mappings if (fileConfig.commitMessageTemplate !== undefined) { hotfixConfig.commitMessageTemplate = fileConfig.commitMessageTemplate; } if (fileConfig.createTags !== undefined) { hotfixConfig.createTags = fileConfig.createTags; } if (fileConfig.versionStrategy !== undefined) { hotfixConfig.versionStrategy = fileConfig.versionStrategy; } if (fileConfig.prereleaseId !== undefined) { hotfixConfig.prereleaseId = fileConfig.prereleaseId; } if (fileConfig.packageJsonPath !== undefined) { hotfixConfig.packageJsonPath = fileConfig.packageJsonPath; } if (fileConfig.gitUserName !== undefined) { hotfixConfig.gitUserName = fileConfig.gitUserName; } if (fileConfig.gitUserEmail !== undefined) { hotfixConfig.gitUserEmail = fileConfig.gitUserEmail; } if (fileConfig.nodejsImageUrl !== undefined) { hotfixConfig.nodejsImageUrl = fileConfig.nodejsImageUrl; } // Handle access token resolution if (fileConfig.accessTokenProp) { const resolvedToken = resolveAccessToken(fileConfig.accessTokenProp); hotfixConfig.accessToken = resolvedToken; } return hotfixConfig; } /** * Loads configuration with proper precedence: defaults < config file < CLI args */ function loadConfiguration(cliConfig = {}, configPath) { // Load config file const fileConfig = readConfigFile(configPath); // Convert file config to HotfixConfig format const convertedFileConfig = convertFileConfigToHotfixConfig(fileConfig); // Merge configurations with proper precedence const mergedConfig = { // Default values (lowest priority) packageJsonPath: './package.json', remoteUrlPattern: '', accessToken: process.env.CI_PUSH_TOKEN || '', branchName: process.env.CI_COMMIT_BRANCH || 'main', createTags: true, versionStrategy: 'auto', prereleaseId: 'release', commitMessageTemplate: 'chore: bump version to {version} after hotfix merge', gitUserName: process.env.GITLAB_USER_NAME || 'GitLab CI', gitUserEmail: process.env.GITLAB_USER_EMAIL || 'ci@gitlab.com', nodejsImageUrl: process.env.NODEJS_IMAGE_URL || 'node:14', // Config file values (medium priority) ...convertedFileConfig, // CLI arguments (highest priority) ...cliConfig }; // Special handling for access token to respect accessTokenProp even if CLI overrides if (fileConfig.accessTokenProp && !cliConfig.accessToken) { const resolvedToken = resolveAccessToken(fileConfig.accessTokenProp, mergedConfig.accessToken); mergedConfig.accessToken = resolvedToken; } return mergedConfig; } //# sourceMappingURL=config-utils.js.map