UNPKG

7.44 kBMarkdownView Raw
1# mako-tree
2
3> The build tree structure used internally by mako
4
5[![npm version][npm-badge]][npm]
6[![npm dependencies][david-badge]][david]
7[![npm dev dependencies][david-dev-badge]][david-dev]
8
9## API
10
11The `Tree` constructor (documented below) is the primary export for the module. It must be used
12with the `new` keyword.
13
14```js
15var Tree = require('mako-tree');
16var tree = new Tree();
17```
18
19### Tree([root]) *(constructor)*
20
21Each instance represents a build tree. Internally, a graph is used to manage the relationships
22between all the files being tracked.
23
24The `root` is a project root that will determine all `file.base` properties. Only 1 root is
25supported per tree. Also, this value will override any `base` parameter you specify in when
26adding files.
27
28### Tree[@@iterable]()
29
30This class implements the `Iterable` interface, which iterates the files in the tree in topological
31order. (see `Tree#getFiles()` for more information)
32
33```js
34for (const file of tree) {
35 // iterate files in topological order
36}
37```
38
39This sugar allows you to treat the tree itself as an iterable, which can be useful in interacting
40with other APIs.
41
42### Tree#hasFile(file)
43
44Returns a `Boolean` reflecting if the given `file` exists in the tree.
45
46### Tree#addFile(params)
47
48Creates a file with the given `params` and adds it to the tree.
49
50### Tree#getFile(file)
51
52Returns the `File` instance for the given `file` ID.
53
54### Tree#findFile(path)
55
56Searches the tree for a file that has the given `path`. (either currently, or at any point in
57it's history) If none is found, it simply returns `undefined`.
58
59### Tree#getFiles([options])
60
61Returns an `Array` of all the `File` objects in this graph.
62
63If `options.topological` is set, the returned list will be in
64[topological order](https://en.wikipedia.org/wiki/Topological_sorting), which respects all
65dependencies so processing is safe where order matters.
66
67### Tree#removeFile(file, [options])
68
69Removes the given `file` from the tree. It will throw an exception if that file has any current
70dependency links.
71
72If `options.force` is set, it will forcefully remove the file, as well as any remaining links.
73
74### Tree#hasDependency(parent, child, [options])
75
76Returns a `Boolean` reflecting if the dependency relationship between `parent` and `child` already
77exists in the tree.
78
79If `options.recursive` is `true`, it will check the dependency tree recursively.
80
81### Tree#addDependency(parent, child)
82
83Adds a new dependency relationship to the graph setting `parent` as depending on `child`.
84
85If either `parent` or `child` are not already in the graph, it will throw.
86
87### Tree#removeDependency(parent, child)
88
89Removes the dependency link between `parent` and `child`.
90
91If this link does not already exist, this will throw.
92
93### Tree#dependenciesOf(file, [options])
94
95Returns an `Array` of files that are direct dependencies of the given `file`.
96
97If `options.recursive` is set, it will return all the files **down** the entire dependency chain.
98
99### Tree#hasDependant(child, parent, [options])
100
101Returns a `Boolean` reflecting if the dependency relationship between `child` and `parent` already
102exists in the tree.
103
104If `options.recursive` is `true`, it will check the dependency tree recursively.
105
106### Tree#addDependant(child, parent)
107
108Adds a new dependency relationship to the graph setting `child` as depended on by `parent`.
109
110If either `parent` or `child` are not already in the graph, it will throw.
111
112### Tree#removeDependant(child, parent)
113
114Removes the dependency link between `parent` and `child`.
115
116If this link does not already exist, this will throw.
117
118
119### Tree#dependantsOf(file, [options])
120
121Returns an `Array` of files that directly depend on the given `file`.
122
123If `options.recursive` is set, it will return all the files **up** the entire dependency chain.
124
125### Tree#size()
126
127Returns the number of files in the tree.
128
129### Tree#clone()
130
131Returns a new `Tree` object that is an effective clone of the original.
132
133### Tree#prune([anchors])
134
135Removes any files from the graph that are unaccessible by any of the provided `anchors` files.
136
137### Tree#removeCycles()
138
139Removes any cycles found in the tree. This is only a last-ditch effort before attempting
140topological sorting, so it makes no guarantees about where it breaks cycles. (circular dependencies
141should work, but that doesn't change the fact that they should be avoided if possible)
142
143### Tree#toJSON()
144
145Returns a trimmed object that can be serialized as JSON. (it should be possible to reconstruct the
146tree from the output)
147
148### Tree#toString([space])
149
150Serializes the tree into a JSON string, which can be written to disk (and then read back) to help
151reduce the amount of duplicate work between different runs of mako.
152
153The `space` parameter is there if you want to "pretty-print" the JSON output.
154
155### Tree.fromString(input)
156
157Unserializes a JSON string into a `Tree` instance. (see `Tree#toJSON()`)
158
159
160### File(params, tree) *(constructor)*
161
162This file class extends [vinyl](https://www.npmjs.com/package/vinyl). The `params` will be passed
163directly to that constructor. (except where `params` is a string, then it will be passed as
164`{ path: params }`)
165
166### File#type
167
168A getter/setter for the extension name. (without a leading `.`)
169
170### File#initialPath
171
172A getter that retrieves the original path for this file.
173
174### File#initialType
175
176A getter that retrieves the original type for this file. (without a leading `.`)
177
178### File#contents
179
180A `Buffer` containing the contents for this file.
181
182**NOTE:** using strings is no longer supported for this property as Vinyl only supports `Buffer`
183and `Stream` values.
184
185### File#hasDependency(child)
186
187Short-hand for `tree.hasDependency(file.path, child)`.
188
189### File#addDependency(child)
190
191Short-hand for `tree.addDependency(file.path, child)`.
192
193### File#removeDependency(child)
194
195Short-hand for `tree.removeDependency(file.path, child)`.
196
197### File#dependencies([options])
198
199Short-hand for `tree.dependenciesOf(file.path, options)`.
200
201### File#hasDependant(parent)
202
203Short-hand for `tree.hasDependant(file.path, parent)`.
204
205### File#addDependant(parent)
206
207Short-hand for `tree.addDependency(file.path, parent)`.
208
209### File#removeDependant(parent)
210
211Short-hand for `tree.removeDependant(file.path, parent)`.
212
213### File#dependants([options])
214
215Short-hand for `tree.dependantsOf(file.path, options)`.
216
217### File#reset()
218
219Used by mako to reset a file enough that it can be safely processed again.
220
221### File#clone()
222
223Creates a clone of this file, such as when cloning the parent `Tree`.
224
225### File#copy(newPath, [options])
226
227Copies this file to a `newPath` (relative to current path) with a new ID that
228can be added to a `Tree` as a distinct file.
229
230Available `options`:
231 - `resetPath` when enabled, the path history will only contain the `newPath`
232
233### File#toJSON()
234
235Returns a cloned object that can be JSON-serialized.
236
237### File#toString()
238
239Returns a string representation via `Vinyl#inspect()` useful for logging.
240
241### File.fromObject(input, tree)
242
243Takes a plain object and converts it into a `File` instance.
244
245### File.id()
246
247The id generator for files, exposed here to allow public use and customization.
248
249
250[david-badge]: https://img.shields.io/david/makojs/tree.svg
251[david-dev-badge]: https://img.shields.io/david/dev/makojs/tree.svg
252[david-dev]: https://david-dm.org/makojs/tree#info=devDependencies
253[david]: https://david-dm.org/makojs/tree
254[npm-badge]: https://img.shields.io/npm/v/mako-tree.svg
255[npm]: https://www.npmjs.com/package/mako-tree