-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread.js
44 lines (39 loc) · 1.22 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
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 value = jolicitron([], inputText);
debug("end");
return value;
};
const assertValid = _.tap(parserOutput => {});
const parseAndValidate = _.flow(
parse,
assertValid,
_.tap(() => debug("parsing completed"))
);
module.exports.parse = parse;