forked from breck7/pldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollSet.js
122 lines (101 loc) · 3.53 KB
/
ScrollSet.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const path = require("path")
const lodash = require("lodash")
const { TreeNode } = require("jtree/products/TreeNode.js")
const { Utils } = require("jtree/products/Utils.js")
const { Disk } = require("jtree/products/Disk.node.js")
class ScrollSetCLI {
constructor() {
this.quickCache = {}
}
importCommand(filename) {
// todo: add support for updating as well
const processEntry = (node, index) => {
const id = node.get("id")
node.delete("id")
const target = this.makeFilePath(id)
Disk.write(target, new TreeNode(Disk.read(target)).patch(node).toString())
console.log(`Processed ${filename}`)
}
const extension = filename.split(".").pop()
if (extension === "csv") TreeNode.fromCsv(Disk.read(filename)).forEach(processEntry)
if (extension === "tsv") TreeNode.fromTsv(Disk.read(filename)).forEach(processEntry)
if (extension === "tree") TreeNode.fromDisk(filename).forEach(processEntry)
}
get searchIndex() {
if (!this.quickCache.searchIndex) this.quickCache.searchIndex = this.makeNameSearchIndex()
return this.quickCache.searchIndex
}
makeFilePath(id) {
return path.join(this.conceptsFolder, id + ".scroll")
}
getTree(file) {
return new TreeNode(Disk.read(this.makeFilePath(file.id)))
}
setAndSave(file, measurementPath, measurementValue) {
const tree = this.getTree(file)
tree.set(measurementPath, measurementValue)
return this.save(file, tree)
}
save(file, tree) {
const dest = this.makeFilePath(file.id)
return Disk.write(dest, tree.toString())
}
makeNameSearchIndex(files = this.concepts.slice(0).reverse()) {
const map = new Map()
files.forEach(parsedConcept =>
this.makeNames(parsedConcept).forEach(name => map.set(name.toLowerCase(), parsedConcept))
)
return map
}
makeNames(concept) {
return [concept.id]
}
searchForConcept(query) {
if (query === undefined || query === "") return
const { searchIndex } = this
return (
searchIndex.get(query) || searchIndex.get(query.toLowerCase()) || searchIndex.get(Utils.titleToPermalink(query))
)
}
searchForConceptCommand(query) {
console.log(lodash.pickBy(this.searchForConcept(query), lodash.identity))
}
grammarFile = ""
scrollSetName = "myScrollSet"
get concepts() {
return require(this.compiledConcepts)
}
async updateIdsCommand() {
this.concepts.forEach(file => {
const dest = path.join(this.conceptsFolder, file.filename)
const tree = new TreeNode(Disk.read(dest))
const newTree = tree.toString().replace(
`import ../code/conceptPage.scroll
id `,
`import ../code/conceptPage.scroll
id ${file.filename.replace(".scroll", "")}
name `
)
Disk.write(dest, newTree.toString())
})
}
buildGrammarFileCommand() {
const code = `node_modules/scroll-cli/grammar/cellTypes.grammar
node_modules/scroll-cli/grammar/root.grammar
node_modules/scroll-cli/grammar/comments.grammar
node_modules/scroll-cli/grammar/blankLine.grammar
node_modules/scroll-cli/grammar/measures.grammar
node_modules/scroll-cli/grammar/import.grammar
node_modules/scroll-cli/grammar/errors.grammar
${this.grammarFile}`
.trim()
.split("\n")
.map(filepath => Disk.read(path.join(__dirname, filepath)))
.join("\n\n")
.replace("catchAllParser catchAllParagraphParser", "catchAllParser errorParser")
.replace(/^importOnly\n/gm, "")
.replace(/^import .+/gm, "")
Disk.write(path.join(__dirname, `${this.scrollSetName}.grammar`), code)
}
}
module.exports = { ScrollSetCLI }