forked from transloadit/uppy
/
autoFixConflicts.js
executable file
·56 lines (47 loc) · 1.65 KB
/
autoFixConflicts.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env node
// Usage: autoFixConflicts.js | sh
import { createInterface as readLines } from 'node:readline'
import { spawn } from 'node:child_process'
const VERSION_URL = /(?<=https:\/\/\S+\/v)\d+\.\d+\.\d+(?:-(?:alpha|beta)(?:[.-]\d+)?)?(?=\/)/
const gitStatus = spawn('git', ['status', '--porcelain'])
for await (const line of readLines(gitStatus.stdout)) {
// eslint-disable-next-line no-continue
if (!line.startsWith('UU ')) continue
const file = line.slice(3)
if (file === 'yarn.lock') {
console.log('corepack yarn install')
console.log('git add yarn.lock')
// eslint-disable-next-line no-continue
continue
}
if (file.endsWith('/package.json')) {
console.log(`git checkout --ours ${file}`)
console.log(`git add ${file}`)
// eslint-disable-next-line no-continue
continue
}
const gitDiff = spawn('git', ['--no-pager', 'diff', '--', file])
let conflictHasStarted = false
let containsCDNChanges = true
let currentConflictContainsCDNChanges = false
// eslint-disable-next-line no-shadow
for await (const line of readLines(gitDiff.stdout)) {
if (conflictHasStarted) {
if (line.startsWith('++>>>>>>>')) {
conflictHasStarted = false
containsCDNChanges &&= currentConflictContainsCDNChanges
currentConflictContainsCDNChanges = false
} else {
currentConflictContainsCDNChanges ||= VERSION_URL.test(line)
}
} else if (line === '++<<<<<<< HEAD') {
conflictHasStarted = true
}
}
if (containsCDNChanges) {
console.log(`git checkout --ours ${file}`)
console.log(`git add ${file}`)
// eslint-disable-next-line no-continue
continue
}
}