-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-cars.js
66 lines (61 loc) · 2.18 KB
/
build-cars.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
const { execSync } = require('child_process')
const { writeFileSync, existsSync } = require('fs')
const CSV = require('csv-string')
const mkdirp = require('mkdirp')
const fetch = require('node-fetch')
const additions = require('./cars/additions.json')
const logos = require('./node_modules/@avto-dev/vehicle-logotypes/src/vehicle-logotypes.json')
process.stdout.write('• Updating car database... ')
process.stdout.write('Updating logos... ')
mkdirp.sync('./cars/logos')
const promises = []
Object.keys(logos).forEach((l) => {
const logo = logos[l]
promises.push(
fetch(decodeURI(logo.logotype.uri))
.then((r) => r.buffer())
.then((img) => {
const ext = logos[l].logotype.mime.split('/')[1]
const file = `${l}.${ext}`
writeFileSync(`./cars/logos/${file}`, img)
})
)
})
Promise.all(promises)
.then(() => {
process.stdout.write('Updating json... ')
const csv = execSync('cd ./node_modules/us-car-models-data && tail -n +2 -q *.csv').toString('UTF-8').trim()
const records = CSV.parse(csv)
const cars = {}
records.forEach((r) => {
const [year, make, model, typeRaw] = r
const years = Array.from(new Set([...(cars[make]?.[model]?.years || []), Number.parseInt(year)]))
const type = JSON.parse(typeRaw)
if (!cars[make]) {
cars[make] = {}
}
cars[make][model] = { type, years }
const logoName = `${make.toLowerCase().replace(' ', '-')}.png`
const logo = existsSync(`./cars/logos/${logoName}`) ? logoName : undefined
if (logo) {
cars[make][model].logo = logo
}
})
Object.keys(additions).forEach((make) => {
Object.keys(additions[make]).forEach((model) => {
if (!cars[make]) {
cars[make] = {}
}
cars[make][model] = additions[make][model]
const logoName = `${make.toLowerCase().replace(' ', '-')}.png`
const logo = additions[make][model].logo || (existsSync(`./cars/logos/${logoName}`) ? logoName : undefined)
if (logo) {
cars[make][model].logo = logo
}
})
})
writeFileSync('./cars/index.json', JSON.stringify(cars))
})
.then(() => {
process.stdout.write('Done.\n')
})