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