1 |
|
2 | ;
|
3 |
|
4 | let debug = require('debug')('mako-file');
|
5 | let extension = require('file-extension');
|
6 | let path = require('path');
|
7 |
|
8 | const pwd = process.cwd();
|
9 | const relative = abs => path.relative(pwd, abs);
|
10 |
|
11 |
|
12 | /**
|
13 | * Represents a file within a build tree.
|
14 | *
|
15 | * @class
|
16 | */
|
17 | class File {
|
18 | /**
|
19 | * Sets up the instance.
|
20 | *
|
21 | * @param {String} location The absolute path to the file.
|
22 | * @param {Tree} tree The parent build tree.
|
23 | * @param {Boolean} [entry] If the file is an entry file.
|
24 | */
|
25 | constructor(location, tree, entry) {
|
26 | debug('initialize %s', relative(location));
|
27 | this.path = location;
|
28 | this.entry = !!entry;
|
29 | this.tree = tree;
|
30 | this.analyzing = false;
|
31 | this.dirty();
|
32 | }
|
33 |
|
34 | /**
|
35 | * Tells us if the file is an entry.
|
36 | *
|
37 | * @return {Boolean}
|
38 | */
|
39 | isEntry() {
|
40 | return !!this.entry;
|
41 | }
|
42 |
|
43 | /**
|
44 | * Check to see if the `child` file is a dependency of this file.
|
45 | *
|
46 | * @see Tree#hasDependency()
|
47 | * @param {String} child The absolute path to the dependency.
|
48 | * @return {Boolean}
|
49 | */
|
50 | hasDependency(child) {
|
51 | return this.tree.hasDependency(this.path, child);
|
52 | }
|
53 |
|
54 | /**
|
55 | * Adds the `child` as a dependency of this file. Returns the new `File`
|
56 | * instance.
|
57 | *
|
58 | * @see Tree#addDependency()
|
59 | * @param {String} child The absolute path to the dependency.
|
60 | * @return {File}
|
61 | */
|
62 | addDependency(child) {
|
63 | return this.tree.addDependency(this.path, child);
|
64 | }
|
65 |
|
66 | /**
|
67 | * Removes the `child` as a dependency of this file.
|
68 | *
|
69 | * @see Tree#removeDependency()
|
70 | * @param {String} child The absolute path to the dependency.
|
71 | */
|
72 | removeDependency(child) {
|
73 | this.tree.removeDependency(this.path, child);
|
74 | }
|
75 |
|
76 | /**
|
77 | * Find the dependencies of this file.
|
78 | *
|
79 | * @see Tree#dependencies()
|
80 | * @param {Object} [options] The search criteria.
|
81 | * @return {Array}
|
82 | */
|
83 | dependencies(options) {
|
84 | return this.tree.dependenciesOf(this.path, options);
|
85 | }
|
86 |
|
87 | /**
|
88 | * Check to see if the `parent` file is a dependant of this file.
|
89 | *
|
90 | * @see Tree#hasDependant()
|
91 | * @param {String} parent The absolute path to the dependant.
|
92 | * @return {Boolean}
|
93 | */
|
94 | hasDependant(parent) {
|
95 | return this.tree.hasDependant(this.path, parent);
|
96 | }
|
97 |
|
98 | /**
|
99 | * Adds the `parent` as a dependant of this file. Returns the new `File`
|
100 | * instance.
|
101 | *
|
102 | * @see Tree#addDependant()
|
103 | * @param {String} parent The absolute path to the dependant.
|
104 | * @return {File}
|
105 | */
|
106 | addDependant(parent) {
|
107 | return this.tree.addDependant(this.path, parent);
|
108 | }
|
109 |
|
110 | /**
|
111 | * Removes the `parent` as a dependant of this file.
|
112 | *
|
113 | * @see Tree#removeDependant()
|
114 | * @param {String} parent The absolute path to the dependant.
|
115 | */
|
116 | removeDependant(parent) {
|
117 | this.tree.removeDependant(this.path, parent);
|
118 | }
|
119 |
|
120 | /**
|
121 | * Find the dependants of this file.
|
122 | *
|
123 | * @see Tree#dependants()
|
124 | * @param {Object} [options] The search criteria.
|
125 | * @return {Array}
|
126 | */
|
127 | dependants(options) {
|
128 | return this.tree.dependantsOf(this.path, options);
|
129 | }
|
130 |
|
131 | /**
|
132 | * Flags the file so it will be analyzed by mako.
|
133 | */
|
134 | dirty() {
|
135 | this.type = this.initialType();
|
136 | this.analyzed = false;
|
137 | }
|
138 |
|
139 | /**
|
140 | * Determine the original file type for this file (as if no transformations
|
141 | * have been run)
|
142 | *
|
143 | * @return {String}
|
144 | */
|
145 | initialType() {
|
146 | return extension(this.path);
|
147 | }
|
148 |
|
149 | /**
|
150 | * Create a clone of this instance.
|
151 | *
|
152 | * @param {Tree} tree The new build tree to attach the clone to.
|
153 | * @return {File}
|
154 | */
|
155 | clone(tree) {
|
156 | debug('cloning file', relative(this.path));
|
157 | let file = new File(this.path);
|
158 | Object.assign(file, this);
|
159 | file.tree = tree;
|
160 | debug('done cloning file', relative(this.path));
|
161 | return file;
|
162 | }
|
163 | }
|
164 |
|
165 |
|
166 | // single export
|
167 | module.exports = File;
|