-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
34 lines (29 loc) · 978 Bytes
/
index.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
// validates and parses hyperform.json
const path = require('path')
const { amazonSchema, googleSchema } = require('../schemas/index')
let parsedHyperformJson
/**
* @description Parses v,alidates, and returns contents of "dir"/hyperform.json
* @param {string} dir Directory where to look for hyperform.json
* @param {string} platform Whether to expect 'amazon' or 'google' content
*
*/
function getParsedHyperformJson(dir, platform) {
if (parsedHyperformJson == null) {
const json = require(path.join(dir, 'hyperform.json'))
// validate its schema
let schema
if (platform === 'amazon') schema = amazonSchema
if (platform === 'google') schema = googleSchema
// throws if platform is not 'amazon' or 'google'
const { error, value } = schema.validate(json)
if (error) {
throw new Error(`${error} ${value}`)
}
parsedHyperformJson = json
}
return parsedHyperformJson
}
module.exports = {
getParsedHyperformJson,
}