mem-fs-editor 4.0.2 → 6.0.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 +16 -10
- package/lib/actions/commit.js +1 -0
- package/lib/actions/copy-tpl.js +16 -8
- package/lib/actions/copy.js +11 -5
- package/lib/actions/delete.js +1 -0
- package/lib/util.js +3 -0
- package/package.json +17 -19
package/README.md
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
mem-fs-editor [](https://travis-ci.org/SBoudrias/mem-fs-editor) [](http://badge.fury.io/js/mem-fs-editor) [](https://coveralls.io/github/SBoudrias/mem-fs-editor)
|
|
2
|
-
=============
|
|
1
|
+
# mem-fs-editor [](https://travis-ci.org/SBoudrias/mem-fs-editor) [](http://badge.fury.io/js/mem-fs-editor) [](https://coveralls.io/github/SBoudrias/mem-fs-editor)
|
|
3
2
|
|
|
4
3
|
File edition helpers working on top of [mem-fs](https://github.com/SBoudrias/mem-fs)
|
|
5
4
|
|
|
6
|
-
Usage
|
|
7
|
-
-------------
|
|
5
|
+
## Usage
|
|
8
6
|
|
|
9
7
|
```js
|
|
10
|
-
var memFs = require(
|
|
11
|
-
var editor = require(
|
|
8
|
+
var memFs = require("mem-fs");
|
|
9
|
+
var editor = require("mem-fs-editor");
|
|
12
10
|
|
|
13
11
|
var store = memFs.create();
|
|
14
12
|
var fs = editor.create(store);
|
|
15
13
|
|
|
16
|
-
fs.write(
|
|
14
|
+
fs.write("somefile.js", "var a = 1;");
|
|
17
15
|
```
|
|
18
16
|
|
|
19
17
|
### `#read(filepath, [options])`
|
|
@@ -61,15 +59,17 @@ Optionally take the same JSON formatting arguments than `#writeJSON()`.
|
|
|
61
59
|
|
|
62
60
|
Delete a file or a directory.
|
|
63
61
|
|
|
64
|
-
`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/
|
|
62
|
+
`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/mrmlnc/fast-glob#options-1). The `sync` flag is forced to be `true` in `globOptions`.
|
|
65
63
|
|
|
66
|
-
### `#copy(from, to, [options])`
|
|
64
|
+
### `#copy(from, to, [options], context[, templateOptions ])`
|
|
67
65
|
|
|
68
66
|
Copy a file from the `from` path to the `to` path.
|
|
69
67
|
|
|
70
68
|
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`.
|
|
71
69
|
|
|
72
|
-
`
|
|
70
|
+
`option.ignoreNoMatch` can be used to silence the error thrown if no files match the `from` pattern.
|
|
71
|
+
|
|
72
|
+
`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/mrmlnc/fast-glob#options-1). 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.
|
|
73
73
|
|
|
74
74
|
### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`
|
|
75
75
|
|
|
@@ -86,6 +86,12 @@ Templates syntax looks like this:
|
|
|
86
86
|
<%- include('partial.ejs', { name: 'Simon' }) %>
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
+
Dir syntax looks like this:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
/some/path/dir<%= value %>/...
|
|
93
|
+
```
|
|
94
|
+
|
|
89
95
|
Refer to the [ejs documentation](http://ejs.co/) for more details.
|
|
90
96
|
|
|
91
97
|
### `#move(from, to, [options])`
|
package/lib/actions/commit.js
CHANGED
package/lib/actions/copy-tpl.js
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
var extend = require('deep-extend');
|
|
4
4
|
var ejs = require('ejs');
|
|
5
|
-
var
|
|
5
|
+
var isBinaryFileSync = require('isbinaryfile').isBinaryFileSync;
|
|
6
6
|
|
|
7
7
|
function render(contents, filename, context, tplSettings) {
|
|
8
8
|
let result;
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const contentsBuffer = Buffer.from(contents, 'binary');
|
|
11
|
+
if (isBinaryFileSync(contentsBuffer, contentsBuffer.length)) {
|
|
12
|
+
result = contentsBuffer;
|
|
12
13
|
} else {
|
|
13
14
|
result = ejs.render(
|
|
14
15
|
contents.toString(),
|
|
@@ -23,10 +24,17 @@ function render(contents, filename, context, tplSettings) {
|
|
|
23
24
|
|
|
24
25
|
module.exports = function (from, to, context, tplSettings, options) {
|
|
25
26
|
context = context || {};
|
|
27
|
+
tplSettings = tplSettings || {};
|
|
26
28
|
|
|
27
|
-
this.copy(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
29
|
+
this.copy(
|
|
30
|
+
from,
|
|
31
|
+
to,
|
|
32
|
+
extend(options || {}, {
|
|
33
|
+
process: function (contents, filename) {
|
|
34
|
+
return render(contents, filename, context, tplSettings);
|
|
35
|
+
}
|
|
36
|
+
}),
|
|
37
|
+
context,
|
|
38
|
+
tplSettings
|
|
39
|
+
);
|
|
32
40
|
};
|
package/lib/actions/copy.js
CHANGED
|
@@ -7,6 +7,7 @@ var glob = require('glob');
|
|
|
7
7
|
var globby = require('globby');
|
|
8
8
|
var extend = require('deep-extend');
|
|
9
9
|
var multimatch = require('multimatch');
|
|
10
|
+
var ejs = require('ejs');
|
|
10
11
|
var util = require('../util');
|
|
11
12
|
|
|
12
13
|
function applyProcessingFunc(process, contents, filename) {
|
|
@@ -14,7 +15,7 @@ function applyProcessingFunc(process, contents, filename) {
|
|
|
14
15
|
return output instanceof Buffer ? output : Buffer.from(output);
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
exports.copy = function (from, to, options) {
|
|
18
|
+
exports.copy = function (from, to, options, context, tplSettings) {
|
|
18
19
|
to = path.resolve(to);
|
|
19
20
|
options = options || {};
|
|
20
21
|
var fromGlob = util.globify(from);
|
|
@@ -23,7 +24,8 @@ exports.copy = function (from, to, options) {
|
|
|
23
24
|
var diskFiles = globby.sync(fromGlob, globOptions);
|
|
24
25
|
var storeFiles = [];
|
|
25
26
|
this.store.each(file => {
|
|
26
|
-
|
|
27
|
+
// The store may have a glob path and when we try to copy it will fail because not real file
|
|
28
|
+
if (!glob.hasMagic(file.path) && multimatch([file.path], fromGlob).length !== 0) {
|
|
27
29
|
storeFiles.push(file.path);
|
|
28
30
|
}
|
|
29
31
|
});
|
|
@@ -44,14 +46,14 @@ exports.copy = function (from, to, options) {
|
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
// Sanity checks: Makes sure we copy at least one file.
|
|
47
|
-
assert(files.length > 0, 'Trying to copy from a source that does not exist: ' + from);
|
|
49
|
+
assert(options.ignoreNoMatch || files.length > 0, 'Trying to copy from a source that does not exist: ' + from);
|
|
48
50
|
|
|
49
51
|
files.forEach(file => {
|
|
50
|
-
this._copySingle(file, generateDestination(file), options);
|
|
52
|
+
this._copySingle(file, generateDestination(file), options, context, tplSettings);
|
|
51
53
|
});
|
|
52
54
|
};
|
|
53
55
|
|
|
54
|
-
exports._copySingle = function (from, to, options) {
|
|
56
|
+
exports._copySingle = function (from, to, options, context, tplSettings) {
|
|
55
57
|
options = options || {};
|
|
56
58
|
|
|
57
59
|
assert(this.exists(from), 'Trying to copy from a source that does not exist: ' + from);
|
|
@@ -63,5 +65,9 @@ exports._copySingle = function (from, to, options) {
|
|
|
63
65
|
contents = applyProcessingFunc(options.process, file.contents, file.path);
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
if (context) {
|
|
69
|
+
to = ejs.render(to, context, tplSettings);
|
|
70
|
+
}
|
|
71
|
+
|
|
66
72
|
this.write(to, contents, file.stat);
|
|
67
73
|
};
|
package/lib/actions/delete.js
CHANGED
package/lib/util.js
CHANGED
|
@@ -36,6 +36,7 @@ exports.globify = function (filePath) {
|
|
|
36
36
|
if (glob.hasMagic(filePath)) {
|
|
37
37
|
return filePath;
|
|
38
38
|
}
|
|
39
|
+
|
|
39
40
|
if (!fs.existsSync(filePath)) {
|
|
40
41
|
// The target of a pattern who's not a glob and doesn't match an existing
|
|
41
42
|
// entity on the disk is ambiguous. As such, match both files and directories.
|
|
@@ -49,8 +50,10 @@ exports.globify = function (filePath) {
|
|
|
49
50
|
if (fsStats.isFile()) {
|
|
50
51
|
return filePath;
|
|
51
52
|
}
|
|
53
|
+
|
|
52
54
|
if (fsStats.isDirectory()) {
|
|
53
55
|
return path.join(filePath, '**');
|
|
54
56
|
}
|
|
57
|
+
|
|
55
58
|
throw new Error('Only file path or directory path are supported.');
|
|
56
59
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mem-fs-editor",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "File edition helpers working on top of mem-fs",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"pretest": "eslint
|
|
7
|
-
"test": "jest"
|
|
8
|
-
"prepublishOnly": "nsp check"
|
|
6
|
+
"pretest": "eslint .",
|
|
7
|
+
"test": "jest"
|
|
9
8
|
},
|
|
10
9
|
"repository": "SBoudrias/mem-fs-editor",
|
|
11
10
|
"author": "Simon Boudrias",
|
|
@@ -16,26 +15,25 @@
|
|
|
16
15
|
],
|
|
17
16
|
"dependencies": {
|
|
18
17
|
"commondir": "^1.0.1",
|
|
19
|
-
"deep-extend": "^0.
|
|
20
|
-
"ejs": "^2.
|
|
21
|
-
"glob": "^7.
|
|
22
|
-
"globby": "^
|
|
23
|
-
"isbinaryfile": "^
|
|
18
|
+
"deep-extend": "^0.6.0",
|
|
19
|
+
"ejs": "^2.6.1",
|
|
20
|
+
"glob": "^7.1.4",
|
|
21
|
+
"globby": "^9.2.0",
|
|
22
|
+
"isbinaryfile": "^4.0.0",
|
|
24
23
|
"mkdirp": "^0.5.0",
|
|
25
|
-
"multimatch": "^
|
|
26
|
-
"rimraf": "^2.
|
|
27
|
-
"through2": "^
|
|
28
|
-
"vinyl": "^2.0
|
|
24
|
+
"multimatch": "^4.0.0",
|
|
25
|
+
"rimraf": "^2.6.3",
|
|
26
|
+
"through2": "^3.0.1",
|
|
27
|
+
"vinyl": "^2.2.0"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"coveralls": "^3.0.
|
|
30
|
+
"coveralls": "^3.0.3",
|
|
32
31
|
"escape-regexp": "0.0.1",
|
|
33
|
-
"eslint": "^
|
|
34
|
-
"eslint-config-xo-space": "^0.
|
|
35
|
-
"jest": "^
|
|
32
|
+
"eslint": "^5.16.0",
|
|
33
|
+
"eslint-config-xo-space": "^0.21.0",
|
|
34
|
+
"jest": "^24.8.0",
|
|
36
35
|
"mem-fs": "^1.0.0",
|
|
37
|
-
"
|
|
38
|
-
"sinon": "^5.0.0"
|
|
36
|
+
"sinon": "^7.3.2"
|
|
39
37
|
},
|
|
40
38
|
"jest": {
|
|
41
39
|
"collectCoverage": true,
|