-
Notifications
You must be signed in to change notification settings - Fork 12
/
generatePageStubs.js
93 lines (78 loc) · 3.96 KB
/
generatePageStubs.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
const fs = require('fs')
const { parse } = require('node-html-parser')
const osArr = require('./grabData/ios')
const deviceObj = require('./grabData/deviceList')
const deviceArr = Object.keys(deviceObj).map(k => deviceObj[k])
const deviceGroupArr = require('./grabData/deviceGroups')
function mkdirSync(dir) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }) } }
mkdirSync('./docs/.vuepress/dist/firmware')
Array.from(new Set(osArr.map(x => x.osStr.replace(/ /g,'-')))).map(x => mkdirSync(`./docs/.vuepress/dist/firmware/${x}`))
mkdirSync('./docs/.vuepress/dist/device')
mkdirSync('./docs/.vuepress/dist/device/identifier')
const page = fs.readFileSync('./docs/.vuepress/dist/404.html', 'utf8')
.replace('<div class="theme-container">','<div class="theme-container no-sidebar">')
.replace('<div class="not-found-section theme-default-content">','<main class="page"><div class="theme-default-content">')
.replace('Take me home</a></div>','Take me home</a></div></main>')
for (const os of osArr) {
let parsedPage = parse(page)
parsedPage.querySelectorAll('meta').find(x => x.rawAttrs.includes('name="description"')).rawAttrs = `name="description" content="Information for ${os.osStr} version ${os.version}"`
parsedPage.querySelector('title').innerHTML = `${os.osStr} ${os.version} ${(os.build && os.build != os.version) ? `(${os.build})` : ''}`
parsedPage.querySelector('h1').innerHTML = `${os.osStr} ${os.version}`
parsedPage.querySelector('blockquote').innerHTML = 'Retrieving data...'
fs.writeFile(`./docs/.vuepress/dist/firmware/${os.osStr.replace(/ /g,'-')}/${os.uniqueBuild}.html`, parsedPage.toString(), (err) => {
if (err) console.log(err)
})
}
function convertDate(date) {
let dateStyle = { dateStyle: 'medium'}
if (date.toString().includes('-')) {
const dateArr = date.split('-')
const dateStyleArr = [{ year: 'numeric' }, { year: 'numeric', month: 'short' }, { dateStyle: 'medium' }]
dateStyle = dateStyleArr[dateArr.length-1]
}
return {
string: new Intl.DateTimeFormat('en-US', dateStyle).format(new Date(date)),
future: new Date() < new Date(date)
}
}
for (const dev of deviceArr.concat(deviceGroupArr)) {
let parsedPage = parse(page)
let showImg = false
let imgKey = dev.key
if (dev.imgCount > 0) showImg = true
if (dev.devices) {
let iterateDevice
for (iterateDevice of dev.devices) if (deviceArr.find(x => x.key == iterateDevice).img.count > 0) {
showImg = true
imgKey = iterateDevice
break
}
}
let released
if (!dev.released) released = undefined
else {
if (Array.isArray(dev.released)) released = dev.released.sort((a,b) => {
const date = [a,b].map(x => new Date(x))
if (date[0] < date[1]) return -1
if (date[0] < date[1]) return 1
return 0
}).map(x => convertDate(x))[0]
else released = convertDate(dev.released)
}
let description = `Information lookup for ${dev.name}.`
if (released) {
let suffix = 'ed'
if (released.future) suffix = 'ing'
description = `Releas${suffix} on ${released.string}`
}
parsedPage.querySelectorAll('meta').find(x => x.rawAttrs.includes('name="description"')).rawAttrs = `name="description" content="${description}"`
parsedPage.querySelector('title').innerHTML = dev.name
parsedPage.querySelector('h1').innerHTML = dev.name
parsedPage.querySelector('blockquote').innerHTML = 'Retrieving data...'
if (showImg) {
parsedPage.querySelectorAll('meta').find(x => x.rawAttrs.includes('property="og:image"')).rawAttrs = `property="og:image" content="https://img.appledb.dev/device@preview/${imgKey}/0.png"`
}
fs.writeFile(`./docs/.vuepress/dist/device${dev.groupKey ? '' : '/identifier'}/${(dev.key || dev.groupKey).replace(/ /g,'-').replace(/\//g,'-')}.html`, parsedPage.toString(), (err) => {
if (err) console.log(err)
})
}