-
Notifications
You must be signed in to change notification settings - Fork 2
/
open-data.js
172 lines (158 loc) Β· 4.52 KB
/
open-data.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
const fs = require('fs')
const { join } = require('path')
console.log('Setting up open data')
fs.mkdirSync(join('.', 'src', 'data', 'data'), { recursive: true })
fs.copyFileSync(
join('.', 'open-data', 'summary.json'),
join('.', 'src', 'data', 'data', 'summary.json')
)
fs.writeFileSync(
join('.', 'src', 'data', 'data', 'donations.json'),
JSON.stringify(
JSON.parse(
fs.readFileSync(join('.', 'open-data', 'donations.json'))
).sort(
(a, b) =>
new Date(b.date || 100).getTime() -
new Date(a.date || 100).getTime()
)
)
)
fs.copyFileSync(
join('.', 'open-data', 'volunteers.json'),
join('.', 'src', 'data', 'data', 'volunteers.json')
)
fs.readdirSync(join('.', 'open-data', 'guides')).forEach(file =>
fs.copyFileSync(
join('.', 'open-data', 'guides', file),
join('.', 'src', 'guides', file)
)
)
const socialMediaData = JSON.parse(
fs.readFileSync(join('.', 'open-data', 'social-media-outreach.json'))
)
const websiteData = socialMediaData.filter(i =>
['Publish to website'].includes(i.status)
)
websiteData.forEach(update => {
let updatePhotos = [
...(update.cdnImages || '').split(',').map(i => i.trim())
]
if (update.pictures) {
updatePhotos.push(
...update.pictures.sort((a, b) =>
a.filename.localeCompare(b.filename)
)
)
}
let smoImage = 'https://cdn.karuna2020.org/logo-vertical.svg'
try {
if (updatePhotos.length) smoImage = updatePhotos[0]
} catch (error) {}
const distributions = update.linkedDistribution
if (distributions && distributions.length) {
distributions.forEach(distribution => {
try {
const result = JSON.parse(
fs.readFileSync(join('.', 'open-data', 'distribution.json'))
).filter(d => d._id === distribution)[0]
update.distribution = { ...result }
} catch (error) {}
})
}
if ((update.distribution || {}).deliveredOn || update.date)
console.log('Adding update', update.event)
if ((update.distribution || {}).deliveredOn || update.date)
fs.writeFileSync(
join(
'.',
'src',
'updates',
`${update.event
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')}.md`
),
`---
date: ${(update.distribution || {}).deliveredOn || update.date}
title: ${update.event}
coverPhoto: ${smoImage}
---
${update.notes}
<div class="mainImages">
${updatePhotos
.filter(i => typeof i === 'string' || i.thumbnails)
.map(
i =>
`<img alt="" src="${
typeof i === 'string' ? i : i.thumbnails.large.url
}">`
)
.join('\n')}
</div>
${
[
...((update.distribution || {}).cdnImages || '')
.split(',')
.map(i => i.trim())
].filter(i => i).length
? `
## Distribution
<div class="distributionimages">
${[
...((update.distribution || {}).cdnImages || '')
.split(',')
.map(i => i.trim())
]
.filter(i => i)
.filter(i => typeof i === 'string' || i.thumbnails)
.map(
i =>
`<img alt="" src="${
typeof i === 'string' ? i : i.thumbnails.large.url
}">`
)
.join('\n')}
</div>
`
: ''
}
`
)
})
const partnersData = JSON.parse(
fs.readFileSync(join('.', 'open-data', 'partners.json'))
)
partnersData.forEach(partner => {
console.log('Adding partner', partner.brandName)
let logo = ''
try {
logo = partner.logo[0].thumbnails.large.url
} catch (error) {}
try {
const cdnImage = partner.cdnImages
if (cdnImage) logo = cdnImage
} catch (error) {}
fs.writeFileSync(
join(
'.',
'src',
'partners',
`${partner.brandName
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')}.md`
),
`---
title: ${partner.brandName}${logo ? `\nlogo: ${logo}` : ''}
---
${partner.description || ''}
<a class="cta" href="${partner.website}">Visit ${partner.brandName} β</a>`
)
})