{"version":3,"file":"insertPlugins.mjs","names":[],"sources":["../src/insertPlugins.ts"],"sourcesContent":["import type {\n  PluginSpec,\n  Options as SemanticReleaseOptions,\n} from 'semantic-release';\n\n/**\n * @since 3.0.0\n * @deprecated EXPERIMENTAL\n */\ninterface PluginInsertRuleSet {\n  Id: string;\n  BeforeIds: string[];\n  AfterIds: string[];\n}\n\n/**\n * [W.I.P. STUB]\n *\n * Given an array of {@link PluginInsertRuleSet}s, insert plugins into a\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone structured clone}\n * (i.e. deep-copy) of the {@link plugins} array, sort the plugin entries, and\n * return the modified array copy.\n * @todo implement\n * @param plugins Your {@link SemanticReleaseOptions.plugins} array.\n * @param inserts An array of {@link PluginInsertRuleSet}s.\n *\n * If a plugin is already in the plugins array, inserting is skipped. The\n * existing entry is sorted according to its ruleset.\n *\n * If any rulesets conflict, an error will be thrown.\n * - error: A must precede B; B must precede A.\n * - pass: C must precede D and E; D must precede E and follow B. Add ruleset\n *   interface: {Id:string, BeforeIds:string[], AfterIds:string[] }\n * @returns A deep-cloned, modified copy of {@link plugins} with plugins\n * inserted and sorted where needed.\n * @since 3.0.0\n * @deprecated EXPERIMENTAL\n */\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\ndeclare function insertAndSortPlugins(\n  plugins: Exclude<SemanticReleaseOptions['plugins'], undefined>,\n  // eslint-disable-next-line @typescript-eslint/no-deprecated\n  inserts: PluginInsertRuleSet[],\n): PluginSpec[];\n\n/**\n * @param plugins {@link SemanticReleaseOptions.plugins}\n * @param afterPluginsIDs The `string` IDs of plugins that the {@link insertPluginIDs} must follow.\n * @param insertPluginIDs The `string` IDs of plugins to insert into a copy of the {@link plugins} array.\n * @param beforePluginsIDs The `string` IDs of plugins that the {@link insertPluginIDs} must precede.\n * @returns\n * A modified copy of {@link plugins} with the given plugins inserted\n * before {@link beforePluginsIDs} and after {@link afterPluginsIDs}.\n *\n * Inserted plugins will be a `[string, {}]` tuple.\n * @throws {ReferenceError} This should never occur. One or more of the\n * {@link afterPluginsIDs} had been found in {@link plugins}, but a `.find` call\n * to get the highest index of the found plugins had returned `undefined`\n * @throws {AggregateError} One or more error occurred when inserting plugins: `insertPlugin was instructed to insert one or more plugins after [${afterPluginsIDs.map(v => '\"' + v + '\"').join(', ')}] and before [${beforePluginsIDs.map(v => `\"${v}\"`).join(', ')}], but ${JSON.stringify(pluginIDs[indexOfLastAfter])} comes after ${JSON.stringify(pluginIDs[index])}!`\n */\nexport function insertPlugin(\n  plugins: Exclude<PluginSpec, string>[],\n  afterPluginsIDs: string[],\n  insertPluginIDs: string[],\n  beforePluginsIDs: string[],\n): Exclude<PluginSpec, string>[] {\n  const pluginIDs = plugins.map(v =>\n    typeof v === 'string' ? v : v[0],\n  );\n\n  // if any beforePluginIDs are ordered before the last afterPlugin, throw. Impossible to sort.\n\n  const indexOfLastAfter = afterPluginsIDs\n    .filter(v => pluginIDs.includes(v))\n    .map(v => pluginIDs.indexOf(v))\n    .sort((a, b) => a - b)\n    .find((_v, index, object) => index === object.length - 1);\n  if (undefined === indexOfLastAfter)\n    throw new ReferenceError(\n      'An attempt to get the last element of an array returned undefined.',\n    );\n\n  const indicesOfBefore = beforePluginsIDs\n    .filter(v => pluginIDs.includes(v))\n    .map(v => pluginIDs.indexOf(v))\n    .sort((a, b) => a - b);\n\n  // This for-of collects *all* sorting errors. The resulting AggregateError\n  // notifies the API user of *all* errors in the order rather than just the\n  // first error encountered.\n  const errors: Error[] = [];\n  for (const index of indicesOfBefore) {\n    if (index <= indexOfLastAfter) {\n      errors.push(\n        new Error(\n          `insertPlugin was instructed to insert one or more plugins after [${afterPluginsIDs.map(v => '\"' + v + '\"').join(', ')}] and before [${beforePluginsIDs.map(v => `\"${v}\"`).join(', ')}], but ${JSON.stringify(pluginIDs[indexOfLastAfter])} comes after ${JSON.stringify(pluginIDs[index])}!`,\n        ),\n      );\n    }\n  }\n  if (errors.length > 0)\n    throw new AggregateError(errors, 'One or more errors occurred while inserting plugin configs into the Semantic Release config!');\n\n  // insert plugin(s)\n\n  const beforeInsert = plugins.slice(0, indexOfLastAfter + 1);\n  const afterInsert = plugins.slice(indexOfLastAfter + 1, plugins.length + 1);\n\n  return [\n    ...beforeInsert,\n    ...insertPluginIDs.map(id =>\n      [id, {}] as [string, unknown],\n    ),\n    ...afterInsert,\n  ];\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA4DA,SAAgB,aACd,SACA,iBACA,iBACA,kBAC+B;CAC/B,MAAM,YAAY,QAAQ,KAAI,MAC5B,OAAO,MAAM,WAAW,IAAI,EAAE,EAChC;CAIA,MAAM,mBAAmB,gBACtB,QAAO,MAAK,UAAU,SAAS,CAAC,CAAC,CAAC,CAClC,KAAI,MAAK,UAAU,QAAQ,CAAC,CAAC,CAAC,CAC9B,MAAM,GAAG,MAAM,IAAI,CAAC,CAAC,CACrB,MAAM,IAAI,OAAO,WAAW,UAAU,OAAO,SAAS,CAAC;CAC1D,IAAI,KAAA,MAAc,kBAChB,MAAM,IAAI,eACR,oEACF;CAEF,MAAM,kBAAkB,iBACrB,QAAO,MAAK,UAAU,SAAS,CAAC,CAAC,CAAC,CAClC,KAAI,MAAK,UAAU,QAAQ,CAAC,CAAC,CAAC,CAC9B,MAAM,GAAG,MAAM,IAAI,CAAC;CAKvB,MAAM,SAAkB,CAAC;CACzB,KAAK,MAAM,SAAS,iBAClB,IAAI,SAAS,kBACX,OAAO,qBACL,IAAI,MACF,oEAAoE,gBAAgB,KAAI,MAAK,OAAM,IAAI,IAAG,CAAC,CAAC,KAAK,IAAI,EAAE,gBAAgB,iBAAiB,KAAI,MAAK,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,IAAI,EAAE,SAAS,KAAK,UAAU,UAAU,iBAAiB,EAAE,eAAe,KAAK,UAAU,UAAU,MAAM,EAAE,EAC7R,CACF;CAGJ,IAAI,OAAO,SAAS,GAClB,MAAM,IAAI,eAAe,QAAQ,8FAA8F;CAIjI,MAAM,eAAe,QAAQ,MAAM,GAAG,mBAAmB,CAAC;CAC1D,MAAM,cAAc,QAAQ,MAAM,mBAAmB,GAAG,QAAQ,SAAS,CAAC;CAE1E,OAAO;EACL,GAAG;EACH,GAAG,gBAAgB,KAAI,OACrB,CAAC,IAAI,CAAC,CAAC,CACT;EACA,GAAG;CACL;AACF"}