({
filePaths,
markupPrefix,
dryRun = true,
removeMarkupOnly = false,
}: {
filePaths: string[]
markupPrefix: string
dryRun?: boolean
removeMarkupOnly?: boolean
})
| 217 | } |
| 218 | |
| 219 | export async function updateFiles({ |
| 220 | filePaths, |
| 221 | markupPrefix, |
| 222 | dryRun = true, |
| 223 | removeMarkupOnly = false, |
| 224 | }: { |
| 225 | filePaths: string[] |
| 226 | markupPrefix: string |
| 227 | dryRun?: boolean |
| 228 | removeMarkupOnly?: boolean |
| 229 | }) { |
| 230 | const sanitize = (contents: string) => { |
| 231 | return contents.replace(markupRegex(markupPrefix), "") |
| 232 | } |
| 233 | |
| 234 | // Go through every file path and handle the operation for each comment |
| 235 | const commentResults = await Promise.allSettled( |
| 236 | filePaths.map(async (path) => { |
| 237 | const { exists, update } = patching |
| 238 | const { read } = filesystem |
| 239 | |
| 240 | const comments: string[] = [] |
| 241 | |
| 242 | // remove files first |
| 243 | if (await exists(path, markupComment(markupPrefix, MarkupComments.RemoveFile))) { |
| 244 | console.log("removing file", path) |
| 245 | if (!dryRun) { |
| 246 | if (removeMarkupOnly) { |
| 247 | const contents = read(path) |
| 248 | const sanitized = sanitize(contents) |
| 249 | filesystem.write(path, sanitized) |
| 250 | } else { |
| 251 | filesystem.remove(path) |
| 252 | } |
| 253 | } |
| 254 | comments.push(MarkupComments.RemoveFile) |
| 255 | return { path, comments } |
| 256 | } |
| 257 | |
| 258 | // filter out RemoveFile (weve already handled it above) |
| 259 | // and create a regex for the remaining comment types |
| 260 | const operationComments = Object.keys(MarkupComments) |
| 261 | .filter((key) => MarkupComments[key] !== MarkupComments.RemoveFile) |
| 262 | .map((key) => markupComment(markupPrefix, MarkupComments[key])) |
| 263 | |
| 264 | const shouldUpdate = removeMarkupOnly |
| 265 | ? markupRegex(markupPrefix) |
| 266 | : RegExp(operationComments.join("|"), "g") |
| 267 | |
| 268 | if (await exists(path, shouldUpdate)) { |
| 269 | const before = read(path) |
| 270 | |
| 271 | operationComments.forEach((operation) => { |
| 272 | if (before.includes(operation)) { |
| 273 | comments.push(operation) |
| 274 | } |
| 275 | }) |
| 276 |
no test coverage detected