-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildDeviceLog.js
204 lines (188 loc) · 4.46 KB
/
buildDeviceLog.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const fs = require(`fs`)
const diff = require(`diff-arrays-of-objects`)
const csv = require(`csv-parse/sync`)
const { exec } = require(`child_process`)
const ManualPromise = require(`manual-promise`).default
const dir = `src/data/devices.csv`
function encode(string) {
return encodeURIComponent(string)
.replace(/%20/g, ` `)
.replace(/%[0-9A-Fa-f]{2}/g, `_`)
}
const discards = [
`Win7`,
`Win8`,
`Win10 14939`,
`Win10 15063`,
`macOS`,
`Linux`,
`Android`,
`ChromeOS`,
`iOS`,
`Notes`,
`Inputs`,
`Outputs`,
`Detail`,
`In Possession`,
`Connection`,
`Class`,
`Anatomy`,
`Thermometers`,
`Pressure`,
`Position`,
`Camera`,
`Buttons`,
`Accelerometers`,
`Camera`,
`Estim`,
`Grips Expanders`,
`Heaters`,
`Lights`,
`Linear Actuators`,
`Linear Actuators (Oscillating)`,
`Linear Actuators (Positional)`,
`Speaker`,
`Suction`,
`Rotators`,
`Oscillators`,
`Vibrators`,
`Pump`,
`Protocols`,
`Buttplug Support Notes`,
`XToys Support Notes`,
`Affiliate Link`,
]
async function getBlob(blobSha) {
const execDone = new ManualPromise()
let data = ``
exec(`git cat-file blob ${blobSha}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
data = stdout
execDone.resolve()
})
await execDone
return data
}
async function getBlobSha(commit, path) {
const execDone = new ManualPromise()
let data = ``
exec(
`git ls-tree --object-only ${commit} ${path}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
data = stdout.split(/$/)[0]
execDone.resolve()
}
)
await execDone
return data
}
async function getFileContent(commit, parent) {
let blob1 = ``
let blob2 = ``
const blobsha1 = await getBlobSha(commit, dir)
if (blobsha1 === null) {
return null
}
if (parent !== undefined) {
const blobsha2 = await getBlobSha(parent, dir)
if (blobsha1 == blobsha2) {
return null
}
if (blobsha2 !== null) {
blob2 = await getBlob(blobsha2)
}
}
blob1 = await getBlob(blobsha1)
const entries1 = csv.parse(blob1, { columns: true }).map((e) => {
e.path = `/devices/` + encode(e.Brand) + `/` + encode(e.Device)
for (const prop of discards) {
delete e[prop]
}
return e
})
const entries2 = csv.parse(blob2, { columns: true }).map((e) => {
e.path = `/devices/` + encode(e.Brand) + `/` + encode(e.Device)
for (const prop of discards) {
delete e[prop]
}
return e
})
console.log(entries1.length, entries2.length)
return diff(entries2, entries1, `path`, { updatedValues: 4 })
}
async function buildDeltaFile() {
const deltas =
JSON.parse(fs.readFileSync(__dirname + `/src/data/deltas.json`)) ?? []
let since = ``
if (deltas.length >= 1) {
since = `--since=${deltas[deltas.length - 1].date}`
}
let commits = []
const execDone = new ManualPromise()
exec(
`git log ${since} --format="%H %ct %P" -- ${dir}`,
(error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
commits = stdout
.split(`\n`)
.filter((value) => value.match(/([a-f0-9]{40})/))
.map((value) => {
const matches = value.match(/([a-f0-9]{40}) (\d+) ([a-f0-9]{40})?/)
return {
commit: matches[1],
timestamp: matches[2],
parent: matches[3],
}
})
execDone.resolve()
}
)
await execDone
console.log(`Found ${commits.length} commits`)
for (const c of commits.reverse()) {
const res = await getFileContent(c.commit, c.parent)
if (
res === null ||
res.added.length + res.removed.length + res.updated.length === 0
) {
console.log(`Skipping no-op diff`)
} else {
console.log(
`Found diff: ${res.added.length} added, ${res.removed.length} removed, ${res.updated.length} updated`
)
deltas.push({
date: c.timestamp,
added: res.added,
removed: res.removed,
updated: res.updated,
})
}
}
fs.writeFileSync(
__dirname + `/src/data/deltas.json`,
JSON.stringify(deltas, null, 4)
)
}
buildDeltaFile().finally()