-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrewrite.js
70 lines (61 loc) · 2.05 KB
/
rewrite.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs-extra')
const glob = require('glob')
const config = require('./config')
const replaceAll = (str, obj) => {
let newStr = str
let i = 0
let lastTime = new Date()
const total = Object.keys(obj).length
let lastPercent = 0
for (let key in obj) {
newStr = newStr.replace(new RegExp(key, 'g'), obj[key])
const percent = Math.round(i / total * 100)
if (lastPercent != percent) {
if (new Date() - 1000 > lastTime) {
console.log(`${percent}% Done`)
lastTime = new Date()
}
lastPercent = percent
}
i++
}
return newStr
}
const getHashMap = async (repo) => {
// get the mapping of all commits
const hashFilename = glob.sync(`./${config.source.repo}*/**/object-id-map.old-new.txt`).pop()
let hashMap = {}
console.log(hashFilename)
if (hashFilename) {
const contents = (await fs.readFile(hashFilename)).toString().split('\n')
hashMap = contents.reduce((result, line) => {
const [oldHash, newHash] = line.split(' ')
result[oldHash] = newHash
return result
}, {})
}
return hashMap
}
const rewrite = async (hashMap, name) => {
if (!await fs.pathExists(`${config.source.repo}/${name}.bak.json`)) {
console.log(`Rewriting ${name}.json`)
const commentsStr = await fs.readFile(`${config.source.repo}/${name}.json`)
await fs.writeFile(`${config.source.repo}/${name}.bak.json`, commentsStr)
const newCommentsStr = replaceAll(commentsStr.toString(), hashMap)
await fs.writeFile(`${config.source.repo}/${name}.json`, newCommentsStr)
} else {
console.log(`${name}.json already processed`)
}
}
const main = async () => {
const hashMap = await getHashMap(config.source.repo)
const issues = glob.sync(`${config.source.repo}/issues/issue-+([0-9]).json`)
// await rewrite(hashMap, 'comments')
// await rewrite(hashMap, 'issue-comments')
// await rewrite(hashMap, 'pull-comments')
// await rewrite(hashMap, 'commits')
for (let issue of issues) {
await rewrite(hashMap, `issues/${issue.match(/(issue-[0-9]+)\.json/)[1]}`)
}
}
main()