forked from breck7/pldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollSet.js
129 lines (108 loc) · 3.95 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
123
124
125
126
127
128
129
const path = require("path")
const lodash = require("lodash")
const { TreeNode } = require("scrollsdk/products/TreeNode.js")
const { Utils } = require("scrollsdk/products/Utils.js")
const { Disk } = require("scrollsdk/products/Disk.node.js")
const { ScrollFile, ScrollFileSystem } = require("scroll-cli")
const scrollFs = new ScrollFileSystem()
class ScrollSetCLI {
constructor() {
this.quickCache = {}
}
importCommand(filename) {
const extension = filename.split(".").pop()
if (extension === "csv") TreeNode.fromCsv(Disk.read(filename)).forEach(patch => this.patchAndSave(patch))
if (extension === "tsv") TreeNode.fromTsv(Disk.read(filename)).forEach(patch => this.patchAndSave(patch))
if (extension === "tree") TreeNode.fromDisk(filename).forEach(patch => this.patchAndSave(patch))
}
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)))
}
patchAndSave(patch) {
const id = patch.get("id")
patch.delete("id")
const target = this.makeFilePath(id)
if (!Disk.exists(target)) {
console.log(`Now match for ${id}`)
return
}
console.log(`Patching ${id}`)
return new ScrollFile(new TreeNode(Disk.read(target)).patch(patch).toString(), target, scrollFs).formatAndSave()
}
setAndSave(file, measurementPath, measurementValue) {
const tree = this.getTree(file)
tree.set(measurementPath, measurementValue)
return this.formatAndSave(file, tree)
}
formatAndSave(file, tree = this.getTree(file)) {
const formatted = new ScrollFile(tree.toString(), this.makeFilePath(file.id), scrollFs).formatted
// force a write
return scrollFs.write(this.makeFilePath(file.id), formatted)
}
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 === "" || !query.toLowerCase) 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))
}
parsersFile = ""
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())
})
}
buildParsersFileCommand() {
const code = `node_modules/scroll-cli/parsers/cellTypes.parsers
node_modules/scroll-cli/parsers/root.parsers
node_modules/scroll-cli/parsers/build.parsers
node_modules/scroll-cli/parsers/comments.parsers
node_modules/scroll-cli/parsers/blankLine.parsers
node_modules/scroll-cli/parsers/measures.parsers
node_modules/scroll-cli/parsers/import.parsers
node_modules/scroll-cli/parsers/errors.parsers
${this.parsersFile}`
.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}.parsers`), code)
}
}
module.exports = { ScrollSetCLI }