aboutsummaryrefslogtreecommitdiff
path: root/scripts/apply_vimgrep_updates.js
blob: 9707aab0a0156de2b5c17b38cb5a4026e13b5529 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bun

const main = async () => {
  let count = 0
  for await (const line of console) {
    const [_, filepath, row, _col, text] = line.match(/(.*):(\d+):(\d+):(.*)/) ?? []
    if (filepath) {
      const isApplied = await apply_edit(filepath, Number(row), text);
      if (isApplied) count++
    }
  }
  return count
}

const apply_edit = async (filepath, row, text) => {
  if (!Number.isFinite(row) || row <= 0) return false;
  const file = Bun.file(filepath)
  const lines = (await file.text()).split('\n')
  if (lines.length > row) {
    lines[row - 1] = text
    await Bun.write(filepath, lines.join('\n'))
    return true
  }
  return false
}

const changesCount = await main()
console.log(`Applied ${changesCount} changes`)