-
Notifications
You must be signed in to change notification settings - Fork 14
/
createTable.js
272 lines (245 loc) · 7.53 KB
/
createTable.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
let fs = require('fs')
let os = require('os')
let path = require('path')
let https = require('https')
let npm = require('npm-programmatic')
let buildJSON = require('./buildJSON.js')
const homeDir = os.homedir()
const dataDir = path.join(homeDir, '/.compat-data')
const tmpDataDir = path.join(homeDir, '/.compat-data/tmp')
const tmpHtmlTable = path.join(homeDir, '/.compat-data/tmp/htmlTable.json')
const tmpES6Data = path.join(homeDir, '/.compat-data/tmp/data-es6.js')
const tmpES2016PlusData = path.join(homeDir, '/.compat-data/tmp/data-es2016plus.js')
const tmpEnvironmentsData = path.join(homeDir, '/.compat-data/tmp/environments.json')
const tmpCommonData = path.join(homeDir, '/.compat-data/tmp/data-common.json')
const htmlDataURL = 'https://raw.githubusercontent.com/Fyrd/caniuse/master/data.json'
const es6DataURL = 'https://raw.githubusercontent.com/kangax/compat-table/gh-pages/data-es6.js'
const es2016PlusDataURL = 'https://raw.githubusercontent.com/kangax/compat-table/gh-pages/data-es2016plus.js'
const environmentsURL = 'https://raw.githubusercontent.com/kangax/compat-table/gh-pages/environments.json'
const commonDataURL = 'https://raw.githubusercontent.com/kangax/compat-table/gh-pages/data-common.json'
const shaFile = path.join(homeDir, '/.compat-data/shas.json')
function githubAPIOptions (path) {
return {
host: 'api.github.com',
path: path,
headers: {
'user-agent': 'node.js',
'Accept': 'application/vnd.github.v3.sha'
}
}
}
function downloadFile (url, filePath) {
return new Promise((resolve, reject) => {
let file = fs.createWriteStream(filePath)
file.on('open', () => {
https.get(url, (res) => {
res.pipe(file)
file.on('finish', () => {
file.close(resolve)
})
})
.on('error', (err) => {
return reject(new Error('Failed to download file at ' + url + ' (' + err + ')'))
})
})
})
}
function downloadFiles (urls, folderPath) {
return new Promise((resolve, reject) => {
urls.forEach((url) => {
downloadFile(url, folderPath + url.slice(url.lastIndexOf('/')))
.catch((err) => {
reject(err)
})
})
resolve()
})
}
function collapseStats (statsObj) {
let collapsedObj = {}
Object.keys(statsObj).forEach((envKey) => {
let envObj = statsObj[envKey]
Object.keys(envObj).forEach((subEnvKey) => {
let value = envObj[subEnvKey]
if (value.startsWith('a')) {
collapsedObj[envKey + subEnvKey] = 'p'
} else {
collapsedObj[envKey + subEnvKey] = envObj[subEnvKey]
}
})
})
return collapsedObj
}
function createHTMLTable () {
return new Promise((resolve, reject) => {
downloadFile(htmlDataURL, tmpHtmlTable)
.then(() => {
let obj = require(tmpHtmlTable)
let data = obj.data
let agents = obj.agents
let compat = {}
let envs = []
Object.keys(data).forEach((featureKey) => {
let feature = data[featureKey]
compat[feature.title] = collapseStats(feature.stats)
})
Object.keys(agents).forEach((agentKey) => {
let env = agents[agentKey]
env.versions.forEach((version) => {
if (version !== null) {
envs.push(agentKey + version)
}
})
})
resolve({
envs: envs,
compat: compat
})
})
.catch((err) => {
reject(err)
})
})
}
function installJSDeps () {
return npm.install(['object.assign'], {
cwd: tmpDataDir,
save: false
})
}
// until compat-table offers its data in a JSON format, it
// has to built manually by pulling JS files from the repo
// and installing dependencies manually
function createJSTable () {
return new Promise((resolve, reject) => {
downloadFiles([es6DataURL, es2016PlusDataURL, commonDataURL, environmentsURL], tmpDataDir)
.then(() => {
installJSDeps()
.then(() => {
let jsTable = buildJSON.buildTable(tmpES6Data, tmpES2016PlusData, tmpEnvironmentsData)
resolve(jsTable)
})
.catch((err) => {
reject(err)
})
})
.catch((err) => {
reject(err)
})
})
}
function getLatestCommitSHA (owner, repo, ref) {
return new Promise((resolve, reject) => {
const path = '/repos/' + owner + '/' + repo + '/commits/' + ref
https.get(githubAPIOptions(path), (res) => {
if (res.statusCode === 200) {
resolve(res.headers.etag)
} else {
reject(new Error('Failed to load latest commit'))
}
})
.on('error', (err) => {
reject(new Error('Failed to load commit SHA at path ' + path + ' (' + err + ')'))
})
})
}
function isUpToDate (compatTableLocation) {
return new Promise((resolve, reject) => {
let currentSHAs
if (!fs.existsSync(shaFile) || !fs.existsSync(compatTableLocation)) {
currentSHAs = {
js: '',
html: ''
}
} else {
currentSHAs = require(shaFile)
}
getLatestCommitSHA('kangax', 'compat-table', 'heads/gh-pages')
.then((jsSHA) => {
getLatestCommitSHA('fyrd', 'caniuse', 'heads/master')
.then((htmlSHA) => {
if (jsSHA !== currentSHAs.js ||
htmlSHA !== currentSHAs.html
) {
fs.writeFileSync(shaFile, JSON.stringify({
js: jsSHA,
html: htmlSHA
}, null, 2))
resolve(false)
} else {
resolve(true)
}
})
})
.catch((err) => {
reject(err)
})
})
}
function createDirectoryStructure () {
return new Promise((resolve, reject) => {
fs.mkdir(dataDir, (err) => {
if (err && err.code !== 'EEXIST') {
reject(new Error('Error creating folder ' + dataDir + ' (' + err + ')'))
} else {
fs.mkdir(tmpDataDir, (err) => {
if (err && err.code !== 'EEXIST') {
reject(new Error('Error creating folder ' + tmpDataDir + ' (' + err + ')'))
} else {
resolve()
}
})
}
})
})
}
function deleteIfExists (path) {
if (fs.existsSync(path)) {
fs.unlinkSync(path)
}
}
function cleanup () {
deleteIfExists(tmpHtmlTable)
deleteIfExists(tmpES6Data)
deleteIfExists(tmpES2016PlusData)
deleteIfExists(tmpCommonData)
deleteIfExists(tmpEnvironmentsData)
}
module.exports.createTable = (compatTableLocation) => {
return new Promise((resolve, reject) => {
createDirectoryStructure().then(() => {
isUpToDate(compatTableLocation).then((upToDate) => {
if (upToDate) {
resolve(false)
} else {
createHTMLTable()
.then((htmlTable) => {
createJSTable().then((jsTable) => {
let fullTable = {
htmlEnvs: htmlTable.envs,
jsEnvs: jsTable.envs,
compat: Object.assign({}, htmlTable.compat, jsTable.compat)
}
fs.writeFileSync(compatTableLocation, JSON.stringify(fullTable, null, 2))
cleanup()
resolve(true)
})
.catch((err) => {
cleanup()
reject(new Error('Failed to create JS table: ' + err))
})
})
.catch((err) => {
cleanup()
reject(new Error('Failed to create HTML table: ' + err))
})
}
}).catch((err) => {
reject(new Error(err))
})
})
.catch((err) => {
reject(new Error(err))
})
})
}