@halospv3/hce.shared-config
Version:
Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als
109 lines (108 loc) • 4.98 kB
JavaScript
import { DefaultOptions } from "./setupGitPluginSpec.default.mjs";
//#region src/setupGitPluginSpec.ts
const GitPluginId = "@semantic-release/git";
/**
* Check if {@link unk} is an {@link AssetEntry}.
* @param unk Anything.
* @returns `true` if {@link unk} is an {@link AssetEntry}. Else, `false`.
*/
function isGitAsset(unk) {
if (typeof unk === "string") return true;
if (typeof unk === "object" && unk != void 0 && "path" in unk) return typeof unk.path === "string";
return false;
}
/**
* Convert one or more {@link AssetEntry AssetEntries} to a `string[]`.
* @param assets The `assets` property of a {@link GitOptions} object. This may not be `false`.
* @returns A `string[]` of the given {@link AssetEntry} objects or strings.
*/
function gitAssetsToStringArray(assets) {
if (assets === void 0) return [];
if (Array.isArray(assets)) return assets.filter((asset) => isGitAsset(asset)).map((v) => typeof v === "string" ? v : v.path);
if (typeof assets === "string") return [assets];
if (typeof assets.path === "string") return [assets.path];
throw new TypeError("assets is not typeof GitOptions['assets'!");
}
/**
* Sanitize a {@link GitOptions} object so its {@link GitOptions#assets} property is either `false` or a `string[]`.
* @param options A {@link GitOptions} object.
* @returns A {@link GitOptions} object whose {@link GitOptions#assets} is `string[] | false`.
*/
function sanitizeGitOptions(options) {
return {
...options,
assets: options.assets === false ? options.assets : gitAssetsToStringArray(options.assets)
};
}
/**
*Determine if {@link options} is a {@link GitOptions} object.
* @param options Anything.
* @returns `true` if {@link options} is a {@link GitOptions} object. Else, `false`.
*/
function isGitOptions(options) {
let isOptions = false;
if (typeof options !== "object" || options == void 0) return isOptions;
if ("assets" in options) isOptions = Array.isArray(options.assets) ? options.assets.every((unk) => isGitAsset(unk)) : isGitAsset(options.assets);
if ("message" in options) isOptions = typeof options.message === "string";
return isOptions;
}
/**
* Determine if {@link pluginSpec} includes a {@link GitOptions} object.
* @param pluginSpec a {@link PluginSpecTuple}.
* @returns `true` if {@link pluginSpec[1]} is a {@link GitOptions} object. Else, `false`.
*/
function hasGitOptions(pluginSpec) {
return isGitOptions(pluginSpec[1]);
}
/**
* Determined if the plugin ID in {@link pluginSpec} is {@link GitPluginId}.
* @param pluginSpec A {@link PluginSpecTuple}
* @returns `true` if {@link pluginSpec[0]} is {@link GitPluginId}
*/
function isGitPluginSpecTuple(pluginSpec) {
return pluginSpec[0] === GitPluginId;
}
/**
* https://github.com/semantic-release/git#options
*
* This plugin may be deprecated at a later date.
* Q: Why would I need to commit during release?
* A: This is for committing your changelog, README, and/or other files updated during the release procedure.
* @param plugins An ordered array of {@link PluginSpecTuple PluginSpecTuples}.
* @returns A {@link PluginSpecTuple}[]. Duplicate `@semantic-release/git` plugin entries are merged or overridden. The last entry takes priority e.g. if the last entry is `{assets: false}`, previous entries' assets are ignored.
*/
function setupGitPluginSpec(plugins) {
/** if Git plugin not in load order, return as-is. */
const firstGitPluginIndex = plugins.findIndex((plugin) => isGitPluginSpecTuple(plugin));
if (firstGitPluginIndex === -1) return plugins;
/**
* the following two const variables are references--not clones.
* Modifying them will affect the plugins array.
*/
const firstGitPlugin = plugins[firstGitPluginIndex];
const firstGitOptions = isGitOptions(firstGitPlugin[1]) ? sanitizeGitOptions(firstGitPlugin[1]) : DefaultOptions;
/**
* remove duplicate Git plugin entries;
* merge extra options into firstGitPlugin's options
* if `firstGitOpts.assets === false`, do not change it.
* All duplicate PluginSpecSRGit entries are then reassigned `undefined` and all
* `undefined` items are filtered from the plugins array.
*/
return plugins.map((current, index) => {
if (index <= firstGitPluginIndex || !isGitPluginSpecTuple(current)) return current;
/** if another Git PluginSpec is discovered, copy its options to the first Git PluginSpec and return undefined. */
if (hasGitOptions(current)) {
const currentGitOptions = sanitizeGitOptions(current[1]);
if (currentGitOptions.assets === false) firstGitOptions.assets = false;
else {
const assets = gitAssetsToStringArray(currentGitOptions.assets);
if (Array.isArray(firstGitOptions.assets)) firstGitOptions.assets.push(...assets);
else firstGitOptions.assets = assets;
}
if (typeof currentGitOptions.message === "string") firstGitOptions.message = currentGitOptions.message;
}
}).filter((pluginSpec) => pluginSpec !== void 0);
}
//#endregion
export { DefaultOptions, GitPluginId, setupGitPluginSpec };
//# sourceMappingURL=setupGitPluginSpec.mjs.map