-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathparseLocals.js
More file actions
52 lines (41 loc) · 1.11 KB
/
parseLocals.js
File metadata and controls
52 lines (41 loc) · 1.11 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
const fs = require('fs')
const path = require('path')
const colors = require('colors/safe')
let lastLocalJsonPath
function logError(error, message) {
console.error(error)
console.error(colors.red(message))
}
function readJsonFileAsString(jsonPath) {
try {
return fs.readFileSync(jsonPath, { encoding: 'utf-8' })
} catch (error) {
logError(error, `ReLaXed error: Could not read .json file at: ${jsonPath}`)
}
}
function parseJson(str) {
try {
return JSON.parse(str)
} catch (error) {
logError(error, 'ReLaXed error: Could not parse locals JSON, see error above.')
}
}
function isPathToJsonFile(filePath) {
return path.extname(filePath) === ".json"
}
function isLastLocalJsonPath(filePath) {
return lastLocalJsonPath === filePath
}
function parseLocals(locals, inputDir) {
if (!locals) {
return
}
jsonString = locals
if (isPathToJsonFile(locals)) {
lastLocalJsonPath = path.join(inputDir, locals)
jsonString = readJsonFileAsString(lastLocalJsonPath)
}
return parseJson(jsonString)
}
exports.isLastLocalJsonPath = isLastLocalJsonPath
exports.parseLocals = parseLocals