mem-fs-editor 2.2.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -0
- package/lib/actions/copy-tpl.js +3 -3
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
mem-fs-editor [](https://travis-ci.org/SBoudrias/mem-fs-editor)
|
|
2
|
+
=============
|
|
3
|
+
|
|
4
|
+
File edition helpers working on top of [mem-fs](https://github.com/SBoudrias/mem-fs)
|
|
5
|
+
|
|
6
|
+
Usage
|
|
7
|
+
-------------
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
var memFs = require('mem-fs');
|
|
11
|
+
var editor = require('mem-fs-editor');
|
|
12
|
+
|
|
13
|
+
var store = memFs.create();
|
|
14
|
+
var fs = editor.create(store);
|
|
15
|
+
|
|
16
|
+
fs.write('somefile.js', 'var a = 1;');
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### `#read(filepath, [options])`
|
|
20
|
+
|
|
21
|
+
Read a file and return its contents as a string.
|
|
22
|
+
|
|
23
|
+
You can alternatively get the raw contents buffer if you pass `options.raw = true`.
|
|
24
|
+
|
|
25
|
+
By default, calling `read()` on a file path that does not exist throws error. You can, however, pass `options.defaults = 'your default content'` to get a default content you pass in, if you prefer to not deal with try/catch.
|
|
26
|
+
|
|
27
|
+
### `#readJSON(filepath, [defaults])`
|
|
28
|
+
|
|
29
|
+
Read a file and parse its contents as JSON.
|
|
30
|
+
|
|
31
|
+
`readJSON()` internally calls `read()` and will throw error if the file path you pass in does not exist. But if you pass in an optional `defaults`, the `defaults` content will be returned in case of the target file is missing, instead of throwing error (error would still be thrown if JSON.parse failed to parse your target file).
|
|
32
|
+
|
|
33
|
+
### `#write(filepath, contents)`
|
|
34
|
+
|
|
35
|
+
Replace the content of a file (existing or new) with a string or a buffer.
|
|
36
|
+
|
|
37
|
+
### `#writeJSON(filepath, contents[, replacer [, space]])`
|
|
38
|
+
|
|
39
|
+
Replace the content of a file (existing or new) with an object that is to be converted by calling `JSON.stringify()`.
|
|
40
|
+
|
|
41
|
+
`contents` should usually be a JSON object, but it can technically be anything that is acceptable by [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
|
|
42
|
+
|
|
43
|
+
Optionally pass `replacer` and `space` as the last two arguments, as defined by [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). `spacer` is used to format the output string (prettify).
|
|
44
|
+
|
|
45
|
+
Default value for `space` is `2`, when not specified.
|
|
46
|
+
|
|
47
|
+
### `#extendJSON(filepath, contents[, replacer [, space]])`
|
|
48
|
+
|
|
49
|
+
Extend the content of an existing JSON file with the partial objects provided as argument.
|
|
50
|
+
|
|
51
|
+
Optionally take the same JSON formatting arguments than `#writeJSON()`.
|
|
52
|
+
|
|
53
|
+
### `#delete(filepath, [options])`
|
|
54
|
+
|
|
55
|
+
Delete a file or a directory.
|
|
56
|
+
|
|
57
|
+
`filePath` can also be a `glob`. If `filePath` is glob, you can optionally pass in an `options.globOptions` object to change its pattern matching behavior. The full list of options are being described [here](https://github.com/isaacs/node-glob#options). The `sync` flag is forced to be `true` in `globOptions`.
|
|
58
|
+
|
|
59
|
+
### `#copy(from, to, [options])`
|
|
60
|
+
|
|
61
|
+
Copy a file from the `from` path to the `to` path.
|
|
62
|
+
|
|
63
|
+
Optionally, pass an `options.process` function (`process(contents)`) returning a string or a buffer who'll become the new file content. The process function will take a single contents argument who is the copied file contents as a `Buffer`.
|
|
64
|
+
|
|
65
|
+
`from` can be a glob pattern that'll be match against the file system. If that's the case, then `to` must be an output directory. For a globified `from`, you can optionally pass in an `options.globOptions` object to change its pattern matching behavior. The full list of options are being described [here](https://github.com/isaacs/node-glob#options). The `nodir` flag is forced to be `true` in `globOptions` to ensure a vinyl object representing each matching directory is marked as `deleted` in the `mem-fs` store.
|
|
66
|
+
|
|
67
|
+
### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`
|
|
68
|
+
|
|
69
|
+
Copy the `from` file and parse its content as an [ejs](http://ejs.co/) template where `context` is the template context (the variable names available inside the template).
|
|
70
|
+
|
|
71
|
+
You can optionally pass a `templateOptions` object. `mem-fs-editor` automatically setup the filename option so you can easily use partials.
|
|
72
|
+
|
|
73
|
+
You can also optionally pass a `copyOptions` object (see [copy() documentation for more details](https://github.com/SBoudrias/mem-fs-editor#copyfrom-to-options).
|
|
74
|
+
|
|
75
|
+
Templates syntax looks like this:
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
<%= value %>
|
|
79
|
+
<%- include('partial.ejs', { name: 'Simon' }) %>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Refer to the [ejs documentation](http://ejs.co/) for more details.
|
|
83
|
+
|
|
84
|
+
### `#move(from, to, [options])`
|
|
85
|
+
|
|
86
|
+
Move/rename a file from the `from` path to the `to` path.
|
|
87
|
+
|
|
88
|
+
`#move` internally uses `#copy` and `#delete`, so `from` can be a glob pattern, and you can provide `options.globOptions` with it.
|
|
89
|
+
|
|
90
|
+
### `#exists(filepath)`
|
|
91
|
+
|
|
92
|
+
Returns `true` if a file exists. Returns `false` if the file is not found or deleted.
|
|
93
|
+
|
|
94
|
+
### `#commit([filters,] callback)`
|
|
95
|
+
|
|
96
|
+
Persist every changes made to files in the mem-fs store to disk.
|
|
97
|
+
|
|
98
|
+
If provided, `filters` is an array of TransformStream to be applied on a stream of vinyl files (like gulp plugins).
|
|
99
|
+
|
|
100
|
+
`callback` is called once the files are updated on disk.
|
package/lib/actions/copy-tpl.js
CHANGED
|
@@ -4,10 +4,10 @@ var path = require('path');
|
|
|
4
4
|
var extend = require('deep-extend');
|
|
5
5
|
var ejs = require('ejs');
|
|
6
6
|
|
|
7
|
-
module.exports = function (from, to, context, tplSettings) {
|
|
7
|
+
module.exports = function (from, to, context, tplSettings, options) {
|
|
8
8
|
context = context || {};
|
|
9
9
|
|
|
10
|
-
this.copy(from, to, {
|
|
10
|
+
this.copy(from, to, extend(options || {}, {
|
|
11
11
|
process: function (contents, filename) {
|
|
12
12
|
return ejs.render(
|
|
13
13
|
contents.toString(),
|
|
@@ -16,5 +16,5 @@ module.exports = function (from, to, context, tplSettings) {
|
|
|
16
16
|
extend({filename: filename}, tplSettings || {})
|
|
17
17
|
);
|
|
18
18
|
}
|
|
19
|
-
});
|
|
19
|
+
}));
|
|
20
20
|
};
|