MCPcopy
hub / github.com/infinitered/ignite / updateFiles

Function updateFiles

src/tools/markup.ts:219–296  ·  view source on GitHub ↗
({
  filePaths,
  markupPrefix,
  dryRun = true,
  removeMarkupOnly = false,
}: {
  filePaths: string[]
  markupPrefix: string
  dryRun?: boolean
  removeMarkupOnly?: boolean
})

Source from the content-addressed store, hash-verified

217}
218
219export 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

Callers 2

remove-demo.tsFile · 0.90

Calls 5

markupCommentFunction · 0.85
sanitizeFunction · 0.85
markupRegexFunction · 0.85
updateFileFunction · 0.85
filterMethod · 0.80

Tested by

no test coverage detected