-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimport-from-devices.js
108 lines (94 loc) · 2.89 KB
/
import-from-devices.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
#!/usr/bin/env node
import { readFileSync } from 'fs'
import { fileURLToPath } from 'node:url'
import { join, resolve } from 'path'
import { db } from '../db/db.js'
import { options } from '../db/options.js'
import { dateStringToDate } from '../lib/date-utils'
async function insertROMS(devices) {
const romsToCreate = new Set()
devices.forEach(x => {
if (x.rom.androidVersion && x.rom.androidVersion.length) {
x.rom.androidVersion.forEach(version => {
if (version && version !== 'N/A')
romsToCreate.add(x.rom.name + '---' + version)
else romsToCreate.add(x.rom.name)
})
} else {
romsToCreate.add(x.rom.name)
}
})
const uniqueRoms = Object.values(
Object.fromEntries(romsToCreate.entries())
).map(k => {
const ks = k.split('---')
return {
name: ks[0],
android_version: ks[1] || undefined,
}
})
await db.batchInsert('roms', uniqueRoms, 10).returning('id')
}
async function insertDevices(devices) {
const devicesToCreate = []
const mappingsToCreate = []
const promises = devices.map(async device => {
const basename = device.deviceName
const codename = device.codename
const releasedOn =
typeof device.releasedOn === 'string' ||
typeof device.releasedOn === 'number'
? dateStringToDate(device.releasedOn)
: null
const version =
device.rom.androidVersion && device.rom.androidVersion.length
? device.rom.androidVersion[0] || null
: null
const status =
(options.STATUS[device.rom.status.toLowerCase()] &&
options.STATUS[device.rom.status.toLowerCase()].value) ||
options.STATUS.unknown.value
const rom = await db('roms')
.where({
name: device.rom.name,
android_version: version,
})
.first()
.select('id')
const insertedDevice = await db('devices')
.insert({
basename,
codename,
released_on: new Date(releasedOn),
})
.returning(['id'])
if (rom && insertDevices.length) {
await db('roms_devices_mapping').insert({
device_id: insertedDevice[0].id,
rom_id: rom.id,
status,
})
}
return true
})
await Promise.all(promises)
await db.batchInsert('devices', devicesToCreate, 10).returning('id')
await db
.batchInsert('roms_devices_mapping', mappingsToCreate, 10)
.returning('id')
}
async function importFromDevicesJSON() {
const deviceJson = resolve(join(__dirname, '..', 'db', 'devices.json'))
let baseData = readFileSync(deviceJson, 'utf-8')
baseData = JSON.parse(baseData)
await insertROMS(baseData.devices)
await insertDevices(baseData.devices)
}
export const importFromDevicesJSON = importFromDevicesJSON
if (fileURLToPath(import.meta.url) === process.argv[1])
importFromDevicesJSON()
.then(x => process.exit(0))
.catch(err => {
console.error(err)
process.exit(1)
})