cz-ghostwriter
Version:
A configurable commitizen adapter
68 lines (67 loc) • 2.39 kB
JavaScript
;
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfiguration = void 0;
const find_up_1 = require("find-up");
const fs_1 = require("fs");
const SUPPORTED_FILES = [
'.changelogrc.cjs',
'.changelogrc.js',
'.changelogrc.json',
'.changelogrc',
];
let cachedConfig;
const getConfiguration = () => {
var _a, _b, _c;
if (cachedConfig) {
return cachedConfig;
}
const configPath = (0, find_up_1.sync)(SUPPORTED_FILES);
if (!configPath) {
throw new Error(`You must provide one of the configuration files: ${SUPPORTED_FILES.join(', ')}`);
}
const config = configPath.endsWith('.cjs') || configPath.endsWith('.js')
? require(configPath)
: JSON.parse((0, fs_1.readFileSync)(configPath).toString());
if ((_a = config.scopes) === null || _a === void 0 ? void 0 : _a.length) {
config.scopes = config.scopes.map((scope) => {
const sanitizedScopeType = scope.type.trim();
if (!sanitizedScopeType) {
throw new Error('You provided an empty scope');
}
return {
description: scope.description,
type: sanitizedScopeType,
};
});
}
if (!((_b = config.types) === null || _b === void 0 ? void 0 : _b.length)) {
throw new Error('You must provide types');
}
if (!config.issueReferencesPrefix) {
config.issueReferencesPrefix = 'for';
}
if (config.preset === 'github') {
cachedConfig = {
issuePrefixes: ['#'],
issueReferencesPrefix: config.issueReferencesPrefix,
scopes: config.scopes,
types: config.types,
};
return cachedConfig;
}
if (!((_c = config.issuePrefixes) === null || _c === void 0 ? void 0 : _c.length)) {
throw new Error('You must provide issuePrefixes');
}
config.issuePrefixes = config.issuePrefixes.map((issuePrefix) => {
const sanitizedIssuePrefix = issuePrefix.trim();
if (!sanitizedIssuePrefix) {
throw new Error('You provided an empty issue prefix');
}
return sanitizedIssuePrefix;
});
cachedConfig = config;
return cachedConfig;
};
exports.getConfiguration = getConfiguration;