forked from hgwood/hash-code-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.js
46 lines (41 loc) · 1.32 KB
/
read.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
const _ = require("lodash/fp");
const assert = require("assert");
const debug = require("debug")("read");
const fs = require("fs");
const jolicitron = require("jolicitron");
module.exports = function read(filePath) {
const cachedFile = `${filePath.split(".")[0]}.in.json`;
try {
fs.accessSync(cachedFile);
debug(`using cached ${cachedFile}`);
} catch (err) {
if (err.code === "ENOENT") {
debug(
`not using cached input file because it does not exist at: ${cachedFile}`
);
} else {
debug(`not using cached input file because:`, err);
}
const textFromInputFile = fs.readFileSync(filePath, "utf8");
debug(`read ${textFromInputFile.length} chars from ${filePath}`);
const result = module.exports.parse(textFromInputFile);
fs.writeFileSync(cachedFile, JSON.stringify(result));
debug(`written cached input file to ${cachedFile}`);
return result;
}
return require(`./${cachedFile}`);
};
const parse = inputText => {
const parse = jolicitron((save, n) => []);
const { parsedValue, remaining } = parse(inputText);
assert.equal(remaining.trim(), "");
debug("end");
return parsedValue;
};
const assertValid = _.tap(parserOutput => {});
const parseAndValidate = _.flow(
parse,
assertValid,
_.tap(() => debug("parsing completed"))
);
module.exports.parse = parse;