-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathverify.js
More file actions
73 lines (67 loc) · 2.74 KB
/
verify.js
File metadata and controls
73 lines (67 loc) · 2.74 KB
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
var fs = require('fs')
var glob = require('glob')
var REQUIRED_FIELDS = ['name', 'author', 'license', 'vector', 'raster']
var OPTIONAL_FIELDS = ['description', 'order_online_url']
glob('meta/*.json', function (err, files) {
if (err) throw err
files.forEach(function (f) {
var file = JSON.parse(fs.readFileSync(f))
var fields = Object.keys(file)
/**
* Rule: all required fields must be defined
*/
REQUIRED_FIELDS.forEach(function (requiredField) {
if (fields.indexOf(requiredField) === -1) {
console.warn(`Warning: ${f}: Missing required field ${requiredField}`)
}
// FIXME: Some entries lack required fields (particularly `vector`)
})
/**
* Rule: only required and optional fields may appear
*/
fields.forEach(function (field) {
if (REQUIRED_FIELDS.indexOf(field) === -1) {
if (OPTIONAL_FIELDS.indexOf(field) === -1) {
console.warn(`Warning: ${f}: ${field} is not a required or optional field.`)
// FIXME: Some entries have extra fields (particularly `filename`)
}
}
})
/**
* Rule: `name` must be a slug: lowercase alphanumeric plus hyphen only
*/
if (file['name'].match(/[^a-z\d-]/) !== null) {
var properSlug = file['name'].toLowerCase().replace(/[^a-z\d-]+/g, '-')
console.warn(`Warning: ${f}: Invalid 'name' field value '${file['name']}', only alphanumeric characters and hyphens are allowed. Suggested value: '${properSlug}'`)
// FIXME: Many entries have improper `name` fields.
}
/**
* Rule: `vector` and `raster` images should exist and be relative to `hexb.in`
* FIXME: Maybe make this hosting-neutral
* FIXME: Vectors are missing, some are invalid.
*/
// 'http://hexb.in/hexagons/' == 24 characters
// 'http://hexb.in/vector/' == 22 characters
// 'http://hexb.in/' == 15 characters
var rasterUrl = file.raster ? file.raster.slice(0, 24) : ''
var vectorUrl = file.vector ? file.vector.slice(0, 22) : ''
var rasterPath = file.raster ? file.raster.slice(15) : ''
var vectorPath = file.vector ? file.vector.slice(15) : ''
if (rasterUrl !== 'http://hexb.in/hexagons/') {
console.warn(`Warning: ${f}: 'raster' image '${file.raster}' must begin with 'http://hexb.in/hexagons/'`)
}
try {
fs.statSync(rasterPath)
} catch (e) {
console.warn(`Warning: ${f}: Couldn't find raster image at '${rasterPath}': ${e}`)
}
if (vectorUrl !== 'http://hexb.in/vector/') {
console.warn(`Warning: ${f}: 'vector' image '${file.vector}' must begin with 'http://hexb.in/vector/'`)
}
try {
fs.statSync(vectorPath)
} catch (e) {
console.warn(`Warning: ${f}: Couldn't find vector image at '${vectorPath}': ${e}`)
}
})
})