UNPKG

6.22 kBMarkdownView Raw
1## Introduction
2This library helps to mask the underlying folder structure and simluates that all the files are stored under a single folder.
3For example:
4```js
5/* test-1
6 |
7 -- a.txt
8 -- b.txt
9
10 test-2
11 |
12 -- c.txt
13 -- d.txt
14 -- sub-dir
15 |
16 -- x.txt
17 -- y.txt
18
19 test-3
20 |
21 -- e.txt
22 -- a.txt
23 */
24```
25
26For the consumer of the library it will look like all the folders are merged from left to right and now under same folder.
27
28```js
29/*
30-- b.txt
31-- c.txt
32-- c.txt
33-- d.txt
34-- sub-dir
35 |
36 -- x.txt
37 -- y.txt
38-- e.txt
39-- a.txt (since we are mergeing from left to right a.txt from test-1 gets overwritten by a.txt of test-3)
40*/
41```
42
43This library simulates the behaviour of the [broccoli-merge-trees](https://github.com/broccolijs/broccoli-merge-trees)
44
45## Usage
46Constructor can take inputs of the type `string`, `BroccoliNode` or FSMerger expected format of Object explained [here](#fsmerger-special-Object)
47
48```js
49let FSMerge = require('fs-merger');
50let fs = new FSMerge(['test-1', 'test-2', 'test-3']);
51/* test-1
52 |
53 -- a.txt
54 -- b.txt
55
56 test-2
57 |
58 -- c.txt
59 -- d.txt
60 -- sub-dir
61 |
62 -- x.txt
63 -- y.txt
64
65 test-3
66 |
67 -- e.txt
68 -- a.txt
69 */
70 let contentB = fs.readFileSync('b.txt'); // content of test-1/b.txt
71 let contentSubDir = fs.readFileSync('sub-dir/x.txt'); //content of test-2/sub-dir/x.txt
72 let contentA = fs.readFileSync('a.txt'); // content of test-3/a.txt; here we merge left to right, duplicate files are overwritten
73 ```
74
75## FSMerger Special Object
76This kind of input is supported only to help broccoli-persistent-filter to reduce the number of merges and funnels needed to be performed before it is passed down to
77persistent filter's constructor is called.
78
79This library will help in avoding unneccesary merge required before calling broccoli-persistent-filter plugin.
80
81For example:
82```js
83// filter.js
84const Filter = require('broccoli-persistent-filter');
85class TestFilter extends Filter {
86 constructor(nodes) {
87 super(nodes);
88 }
89
90 processString(content) {
91 return content.replace(/broccoli/gi, `filter`);
92 }
93};
94```
95```js
96/* input structure
97fixture
98 |
99 -- docs
100 |
101 -- c.txt
102 -- d.txt
103 -- example
104 |
105 -- map.js
106*/
107```
108
109```js
110// BrocFile.js
111const Funnel = require('broccoli-funnel');
112const MergeTree = require('broccoli-merge-trees');
113let mergedTree = new MergeTree([
114 new Funnel('fixture/docs', {
115 destDir: 'documents'
116 }),
117 new Funnel('fixture/example', {
118 getDestinationPath: function (relativePath) {
119 if (relativePath.includes('map.js')) {
120 return 'metal.js';
121 }
122 return relativePath;
123 }
124 }),
125]);
126module.exports = new TestFilter(mergedTree);
127```
128
129```sh
130broccoli build dist
131# output
132# dist
133# |
134# --documents
135# |
136# -- c.txt
137# -- d.txt
138# --example
139# |
140# -- metal.js
141```
142With this new library we can write the same above as following once [PR](https://github.com/stefanpenner/broccoli-persistent-filter/pull/175) is merged into `broccoli-persistent-filter`.
143
144```js
145
146let FSMergerObjectWithPrefix = {
147 root: 'fixture/docs',
148 prefix: 'documents'
149}
150
151let FSMergerObjectWithFileDest = {
152 root: 'fixture/example',
153 getDestinationPath: function (relativePath) {
154 if (relativePath.includes('map.js')) {
155 return 'metal.js';
156 }
157 return relativePath;
158 }
159}
160
161module.exports = new TestFilter([FSMergerObjectWithPrefix, FSMergerObjectWithFileDest]);
162
163```
164
165This new library helped in removing two funnels which where used only for the sake of renaming at the output of persitent filter and mergeTree was performed because persitent filter was restricted to accept only one inputNode.
166
167## FSMerger.fs
168
169`FSMerge.fs` is a proxy for the file operations and few whitelisted fsmerger operations
170
171Following are the operation which `FSMerger.fs` supports
172
173All these are standard `fs` operations. Refer node guide for [file handling](https://nodejs.org/api/fs.html)
174* readFileSync
175* existsSync
176* lstatSync
177* statSync
178* readdirSync
179* readdir
180
181Following are specfic to `FSMerger`
182* readFileMeta
183
184Reads the filemeta passed down while creating the new FSMerger instance for a specific root.
185Ex:
186```js
187/*
188fixture
189 |
190 -- docs
191 |
192 -- c.txt
193 -- d.txt
194*/
195let FSMergerObjectWithPrefix = {
196 root: 'fixture/docs',
197 prefix: 'documents'
198}
199let FSMerge = require('fs-merger');
200let fsmerge = new FSMerge([FSMergerObjectWithPrefix]);
201let filemeta = fsmerge.fs.readFileMeta('c.txt');
202/*
203filemeta will look something like this
204{
205 path: 'fixture/docs/c.txt',
206 prefix: 'document'
207 getDestinationPath: undefined
208}
209*/
210
211```
212* at
213
214This function is used to retrive file from a specfic input path (or root) directory. This function can used when we have same filename in mulitple inputPaths and we want spicific inputPath
215ex:
216```js
217let FSMerge = require('fs-merger');
218let fsmerge = new FSMerge(['test-1', 'test-2', 'test-3']);
219/* test-1
220 |
221 -- a.txt
222 -- b.txt
223
224 test-2
225 |
226 -- c.txt
227 -- d.txt
228 -- sub-dir
229 |
230 -- x.txt
231 -- y.txt
232
233 test-3
234 |
235 -- e.txt
236 -- a.txt
237 */
238 let contentA = fs.readFileSync('a.txt'); // content of test-3/a.txt; here we merge left to right, duplicate files are overwritten
239 let contentB = fsmerge.fs.at(0).readFileSync('a.txt'); // content of test-1/a.txt
240 let contentC = fsmerge.fs.at(2).readFileSync('a.txt'); // content of test-3/a.txt; here we merge left to right, duplicate files are overwritten
241 let contentSubDir = fsmerge.fs.at(1).readFileSync('sub-dir/x.txt'); //content of test-2/sub-dir/x.txt
242 ```
243
244
245* `entries` - performs same functionality as in `walk-sync`. Refer the `walk-sync` [guide here](https://github.com/joliss/node-walk-sync#entries).
246
247* `relativePathTo` - Converts an absolute path into a `relativePath` and an `at` index
248 suitable for use when calling the other FSMerger methods.
\No newline at end of file