( baseDirUri: string, blockType: BlockType, fileExists: (uri: string) => Promise<boolean>, extension?: string, isGlobal?: boolean, baseFilenameOverride?: string, )
| 112 | } |
| 113 | |
| 114 | export async function findAvailableFilename( |
| 115 | baseDirUri: string, |
| 116 | blockType: BlockType, |
| 117 | fileExists: (uri: string) => Promise<boolean>, |
| 118 | extension?: string, |
| 119 | isGlobal?: boolean, |
| 120 | baseFilenameOverride?: string, |
| 121 | ): Promise<string> { |
| 122 | const fileExtension = extension ?? getFileExtension(blockType); |
| 123 | let baseFilename = ""; |
| 124 | |
| 125 | const trimmedOverride = baseFilenameOverride?.trim(); |
| 126 | if (trimmedOverride) { |
| 127 | if (blockType === "rules") { |
| 128 | const withoutExtension = trimmedOverride.replace(/\.[^./\\]+$/, ""); |
| 129 | const sanitized = sanitizeRuleName(withoutExtension); |
| 130 | baseFilename = sanitized; |
| 131 | } else { |
| 132 | baseFilename = trimmedOverride; |
| 133 | } |
| 134 | } |
| 135 | if (!baseFilename) { |
| 136 | baseFilename = |
| 137 | blockType === "rules" && isGlobal |
| 138 | ? "global-rule" |
| 139 | : `new-${BLOCK_TYPE_CONFIG[blockType]?.filename}`; |
| 140 | } |
| 141 | |
| 142 | let counter = 0; |
| 143 | let fileUri: string; |
| 144 | |
| 145 | do { |
| 146 | const suffix = counter === 0 ? "" : `-${counter}`; |
| 147 | fileUri = joinPathsToUri( |
| 148 | baseDirUri, |
| 149 | `${baseFilename}${suffix}.${fileExtension}`, |
| 150 | ); |
| 151 | counter++; |
| 152 | } while (await fileExists(fileUri)); |
| 153 | |
| 154 | return fileUri; |
| 155 | } |
| 156 | |
| 157 | export async function createNewWorkspaceBlockFile( |
| 158 | ide: IDE, |
no test coverage detected