-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcommons.js
181 lines (146 loc) · 5.62 KB
/
commons.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
var fs = require('fs-extra')
var downloadFile = require('download-file')
var Papa = require('papaparse')
var utils = require('./utils')
var args = process.argv.slice(2)
switch (args[0]) {
case 'download':
download()
break
case 'deploy':
deploy()
break
default:
createProjects()
createOrgs()
createUsers()
}
// Download the data
function download () {
console.log('Downloads started')
var files = [
{
url: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQ4gM-ByCoxuTAlX4qtRHn05IfPgjBB_pPk6aGfjkRFhYl_IFx9__s9NUfxJKnj3HvtkIWhBvoMLLei/pub?gid=129503185&single=true&output=csv',
filename: 'categories-projects.csv'
},
{
url: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQ4gM-ByCoxuTAlX4qtRHn05IfPgjBB_pPk6aGfjkRFhYl_IFx9__s9NUfxJKnj3HvtkIWhBvoMLLei/pub?gid=297467500&single=true&output=csv',
filename: 'categories-organisations.csv'
},
{
url: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQ4gM-ByCoxuTAlX4qtRHn05IfPgjBB_pPk6aGfjkRFhYl_IFx9__s9NUfxJKnj3HvtkIWhBvoMLLei/pub?gid=0&single=true&output=csv',
filename: 'projects.csv'
},
{
url: 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQ4gM-ByCoxuTAlX4qtRHn05IfPgjBB_pPk6aGfjkRFhYl_IFx9__s9NUfxJKnj3HvtkIWhBvoMLLei/pub?gid=577365421&single=true&output=csv',
filename: 'organisations.csv'
},
{
url: 'https://raw.githubusercontent.com/CodeForAfricaLabs/github-africa/master/step4.json',
filename: 'users.json'
}
]
for (var i = files.length - 1; i >= 0; i--) {
console.log('Downloading ' + files[i].filename)
var options = {
directory: './dist/_data',
filename: files[i].filename
}
downloadFile(files[i].url, options, function (err, path) {
if (err) throw err
console.log('Download complete - ' + path)
// TODO: Figure out files.pipe callback so as not to have to run `yarn commons`
// switch (path) {
// case options.directory + '/projects.csv':
// create_projects()
// break
// case options.directory + '/users.json':
// create_users()
// break
// case options.directory + '/organisations.csv':
// create_orgs()
// break
// }
})
}
console.log('IMPORTANT: Please run `yarn commons` to process downloaded files.')
}
// Create the projects' files
function createProjects () {
console.log('Processing projects.')
var data = fs.readFileSync('./dist/_data/projects.csv', 'utf8')
// Clean the projects folder first
fs.emptyDirSync('./dist/_projects')
var projects = Papa.parse(data, {'header': true}).data
// create the files
for (var i = 0; i <= projects.length - 1; i++) {
var content = ''
content = '---\n'
content += 'layout: item\n'
content += 'body_class: item\n'
content += 'title: ' + projects[i]['PROJECT NAME'] + '\n'
content += 'origin: ' + projects[i]['ORIGIN COUNTRY'] + '\n'
content += 'countries: ' + projects[i]['COUNTRIES WHERE DEPLOYED'] + '\n'
content += 'category: ' + projects[i]['SECTOR'] + '\n'
content += 'site_url: ' + projects[i]['PROJECT WEBSITE'] + '\n'
content += 'github_url: ' + projects[i]['PROJECT GITHUB REPO'] + '\n'
content += 'related: ' + projects[i]['RELATED PROJECTS'] + '\n'
content += 'organisations: ' + projects[i]['ORGANISATIONS'] + '\n'
content += 'description: >\n ' + projects[i]['PROJECT DESCRIPTION'].replace('\n', '\n ') + '\n'
content += '---\n'
fs.outputFileSync('./dist/_projects/' + utils.slugify(projects[i]['PROJECT NAME']) + '.md', content)
}
console.log('Finished processing ' + projects.length + ' projects.')
}
// Create the organisations' files
function createOrgs () {
console.log('Processing organisations.')
var data = fs.readFileSync('./dist/_data/organisations.csv', 'utf8')
// Clean the organisations folder first
fs.emptyDirSync('./dist/_organisations')
var orgs = Papa.parse(data, {'header': true}).data
// create the files
for (var i = 0; i <= orgs.length - 1; i++) {
var content = ''
content = '---\n'
content += 'layout: item\n'
content += 'body_class: item\n'
content += 'title: ' + orgs[i]['Name'] + '\n'
content += 'countries: ' + orgs[i]['Country'] + '\n'
content += 'category: ' + orgs[i]['Entity Type'] + '\n'
content += 'site_url: ' + orgs[i]['Url'] + '\n'
content += 'github_url: ' + orgs[i]['Github'] + '\n'
content += 'related: ' + orgs[i]['Related'] + '\n'
content += 'description: >\n ' + orgs[i]['Description'].replace('\n', '\n ') + '\n'
content += '---\n'
fs.outputFileSync('./dist/_organisations/' + utils.slugify(orgs[i].Name) + '.md', content)
}
console.log('Finished processing ' + orgs.length + ' organisations.')
}
// Create shuffled users CSV to be processed by Jekyll
// NOTE: Jekyll will limit the shown images to 80
function createUsers () {
console.log('Processing users.')
// Move file because of UTF8 issues with Jekyll
var fileToMove = './dist/_data/users.json'
var filePath = './dist/js/data/users.json'
if (fs.existsSync(fileToMove)) {
fs.removeSync(filePath)
fs.moveSync(fileToMove, filePath)
}
var users = fs.readJsonSync(filePath)
// Shuffle the users
users = utils.shuffleArray(users)
// Save to CSV
fs.outputFileSync('./dist/_data/users.csv', Papa.unparse(users))
console.log('Finished processing ' + users.length + ' users.')
}
// Publish to gh-pages
function deploy () {
console.log('Starting Github Pages publish...')
var ghpages = require('gh-pages')
ghpages.publish('dist', function (err) {
if (err) throw err
console.log('Completed Github pages publish.')
})
}