@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
1 lines • 8.05 kB
Source Map (JSON)
{"version":3,"file":"setupGitPluginSpec.mjs","names":[],"sources":["../src/setupGitPluginSpec.ts"],"sourcesContent":["// @ts-types=\"./semantic-release__git.d.ts\"\nimport type { AssetEntry, Options as GitOptions } from '@semantic-release/git';\nimport type { PluginSpecSRGit, PluginSpecTuple } from './semanticReleaseConfig.ts';\nimport { DefaultOptions } from './setupGitPluginSpec.default.ts';\n\nexport const GitPluginId = '@semantic-release/git';\nexport { DefaultOptions } from './setupGitPluginSpec.default.ts';\n\n/**\n * Check if {@link unk} is an {@link AssetEntry}.\n * @param unk Anything.\n * @returns `true` if {@link unk} is an {@link AssetEntry}. Else, `false`.\n */\nfunction isGitAsset(unk: unknown): unk is AssetEntry {\n if (typeof unk === 'string')\n return true;\n // Avoid ending condition with `typeof unk.path === 'string'`.\n // TS narrowing is bugged; requires the check to be performed TWICE!!\n if (typeof unk === 'object' && unk != undefined && 'path' in unk) {\n return typeof unk.path === 'string';\n }\n return false;\n}\n\n/**\n * Convert one or more {@link AssetEntry AssetEntries} to a `string[]`.\n * @param assets The `assets` property of a {@link GitOptions} object. This may not be `false`.\n * @returns A `string[]` of the given {@link AssetEntry} objects or strings.\n */\nfunction gitAssetsToStringArray(\n assets: Exclude<GitOptions['assets'], false>,\n): string[] {\n if (assets === undefined)\n return [];\n if (Array.isArray(assets)) {\n return assets.filter(asset => isGitAsset(asset))\n .map(v => typeof v === 'string' ? v : v.path);\n }\n if (typeof assets === 'string')\n return [assets] as string[];\n if (typeof assets.path === 'string')\n return [assets.path];\n throw new TypeError('assets is not typeof GitOptions[\\'assets\\'!');\n}\n\n/**\n * Sanitize a {@link GitOptions} object so its {@link GitOptions#assets} property is either `false` or a `string[]`.\n * @param options A {@link GitOptions} object.\n * @returns A {@link GitOptions} object whose {@link GitOptions#assets} is `string[] | false`.\n */\nfunction sanitizeGitOptions(options: GitOptions): Omit<GitOptions, 'assets'> & { assets: string[] | false } {\n return { ...options, assets: options.assets === false ? options.assets : gitAssetsToStringArray(options.assets) };\n}\n\n/**\n *Determine if {@link options} is a {@link GitOptions} object.\n * @param options Anything.\n * @returns `true` if {@link options} is a {@link GitOptions} object. Else, `false`.\n */\nfunction isGitOptions(options: unknown): options is GitOptions {\n let isOptions = false;\n\n if (typeof options !== 'object' || options == undefined)\n return isOptions;\n if ('assets' in options) {\n isOptions = Array.isArray(options.assets)\n ? options.assets.every(unk => isGitAsset(unk))\n : isGitAsset(options.assets);\n }\n if ('message' in options)\n isOptions = typeof options.message === 'string';\n return isOptions;\n}\n\n/**\n * Determine if {@link pluginSpec} includes a {@link GitOptions} object.\n * @param pluginSpec a {@link PluginSpecTuple}.\n * @returns `true` if {@link pluginSpec[1]} is a {@link GitOptions} object. Else, `false`.\n */\nfunction hasGitOptions<P extends string>(pluginSpec: PluginSpecTuple<P>): pluginSpec is PluginSpecTuple<P, GitOptions> {\n return isGitOptions(pluginSpec[1]);\n};\n\n/**\n * Determined if the plugin ID in {@link pluginSpec} is {@link GitPluginId}.\n * @param pluginSpec A {@link PluginSpecTuple}\n * @returns `true` if {@link pluginSpec[0]} is {@link GitPluginId}\n */\nfunction isGitPluginSpecTuple<T>(pluginSpec: [string, T]): pluginSpec is [typeof GitPluginId, T] {\n return pluginSpec[0] === GitPluginId;\n}\n\n/**\n * https://github.com/semantic-release/git#options\n *\n * This plugin may be deprecated at a later date.\n * Q: Why would I need to commit during release?\n * A: This is for committing your changelog, README, and/or other files updated during the release procedure.\n * @param plugins An ordered array of {@link PluginSpecTuple PluginSpecTuples}.\n * @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.\n */\nexport function setupGitPluginSpec(plugins: PluginSpecTuple[]): PluginSpecTuple[] {\n /** if Git plugin not in load order, return as-is. */\n const firstGitPluginIndex = plugins.findIndex(plugin => isGitPluginSpecTuple(plugin));\n if (firstGitPluginIndex === -1)\n return plugins;\n\n /**\n * the following two const variables are references--not clones.\n * Modifying them will affect the plugins array.\n */\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const firstGitPlugin = plugins[firstGitPluginIndex]!;\n const firstGitOptions: ReturnType<typeof sanitizeGitOptions> = isGitOptions(firstGitPlugin[1])\n ? sanitizeGitOptions(firstGitPlugin[1])\n : DefaultOptions;\n\n /**\n * remove duplicate Git plugin entries;\n * merge extra options into firstGitPlugin's options\n * if `firstGitOpts.assets === false`, do not change it.\n * All duplicate PluginSpecSRGit entries are then reassigned `undefined` and all\n * `undefined` items are filtered from the plugins array.\n */\n return plugins.map((current: PluginSpecTuple, index): PluginSpecTuple | PluginSpecSRGit | undefined => {\n // skip everything up to and including the first Git PluginSpec\n if (index <= firstGitPluginIndex || !isGitPluginSpecTuple(current))\n return current;\n\n /** if another Git PluginSpec is discovered, copy its options to the first Git PluginSpec and return undefined. */\n if (hasGitOptions(current)) {\n const currentGitOptions = sanitizeGitOptions(current[1]);\n\n if (currentGitOptions.assets === false) {\n firstGitOptions.assets = false;\n }\n else {\n const assets: string[] = gitAssetsToStringArray(currentGitOptions.assets);\n if (Array.isArray(firstGitOptions.assets)) {\n firstGitOptions.assets.push(...assets);\n }\n else {\n firstGitOptions.assets = assets;\n }\n }\n\n if (typeof currentGitOptions.message === 'string')\n firstGitOptions.message = currentGitOptions.message;\n }\n return undefined;\n }).filter(pluginSpec => pluginSpec !== undefined);\n}\n"],"mappings":";;AAKA,MAAa,cAAc;;;;;;AAQ3B,SAAS,WAAW,KAAiC;CACnD,IAAI,OAAO,QAAQ,UACjB,OAAO;CAGT,IAAI,OAAO,QAAQ,YAAY,OAAO,KAAA,KAAa,UAAU,KAC3D,OAAO,OAAO,IAAI,SAAS;CAE7B,OAAO;AACT;;;;;;AAOA,SAAS,uBACP,QACU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