(name: string, existingIds: Iterable<string>)
| 35 | } |
| 36 | |
| 37 | export function nextProjectScriptId(name: string, existingIds: Iterable<string>): string { |
| 38 | const taken = new Set(Array.from(existingIds)); |
| 39 | const baseId = normalizeScriptId(name); |
| 40 | if (!taken.has(baseId)) return baseId; |
| 41 | |
| 42 | let suffix = 2; |
| 43 | while (suffix < 10_000) { |
| 44 | const candidate = `${baseId}-${suffix}`; |
| 45 | const safeCandidate = |
| 46 | candidate.length <= MAX_SCRIPT_ID_LENGTH |
| 47 | ? candidate |
| 48 | : `${baseId.slice(0, Math.max(1, MAX_SCRIPT_ID_LENGTH - String(suffix).length - 1))}-${suffix}`; |
| 49 | if (!taken.has(safeCandidate)) { |
| 50 | return safeCandidate; |
| 51 | } |
| 52 | suffix += 1; |
| 53 | } |
| 54 | |
| 55 | // This last-resort fallback only triggers after exhausting thousands of suffixes. |
| 56 | return `${baseId}-${Date.now()}`.slice(0, MAX_SCRIPT_ID_LENGTH); |
| 57 | } |
| 58 | |
| 59 | export function primaryProjectScript(scripts: ReadonlyArray<ProjectScript>): ProjectScript | null { |
| 60 | const regular = scripts.find((script) => !script.runOnWorktreeCreate); |
no test coverage detected