mem-fs-editor 2.0.4 → 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 +10 -2
- package/{actions → lib/actions}/commit.js +5 -2
- package/lib/actions/copy-tpl.js +20 -0
- package/{actions → lib/actions}/copy.js +8 -13
- package/{actions → lib/actions}/delete.js +2 -3
- package/lib/actions/exists.js +7 -0
- package/lib/actions/extend-json.js +10 -0
- package/{actions → lib/actions}/move.js +0 -0
- package/lib/actions/read-json.js +13 -0
- package/{actions → lib/actions}/read.js +3 -3
- package/{actions → lib/actions}/write-json.js +2 -2
- package/{actions → lib/actions}/write.js +7 -3
- package/{index.js → lib/index.js} +1 -0
- package/{util → lib}/util.js +0 -0
- package/package.json +9 -10
- package/actions/copy-tpl.js +0 -21
- package/actions/exists.js +0 -7
- package/actions/read-json.js +0 -13
package/README.md
CHANGED
|
@@ -44,6 +44,12 @@ Optionally pass `replacer` and `space` as the last two arguments, as defined by
|
|
|
44
44
|
|
|
45
45
|
Default value for `space` is `2`, when not specified.
|
|
46
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
|
+
|
|
47
53
|
### `#delete(filepath, [options])`
|
|
48
54
|
|
|
49
55
|
Delete a file or a directory.
|
|
@@ -58,11 +64,13 @@ Optionally, pass an `options.process` function (`process(contents)`) returning a
|
|
|
58
64
|
|
|
59
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.
|
|
60
66
|
|
|
61
|
-
### `#copyTpl(from, to, context, [
|
|
67
|
+
### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`
|
|
62
68
|
|
|
63
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).
|
|
64
70
|
|
|
65
|
-
You can optionally pass a
|
|
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).
|
|
66
74
|
|
|
67
75
|
Templates syntax looks like this:
|
|
68
76
|
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var fs = require('fs');
|
|
4
4
|
var path = require('path');
|
|
5
|
-
var _ = require('lodash');
|
|
6
5
|
var through = require('through2');
|
|
7
6
|
var mkdirp = require('mkdirp');
|
|
8
7
|
var rimraf = require('rimraf');
|
|
@@ -30,9 +29,11 @@ module.exports = function (filters, cb) {
|
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
var modifiedFilter = through.obj(function (file, enc, cb) {
|
|
33
|
-
|
|
32
|
+
// Don't process deleted file who haven't been commited yet.
|
|
33
|
+
if (file.state === 'modified' || (file.state === 'deleted' && !file.isNew)) {
|
|
34
34
|
this.push(file);
|
|
35
35
|
}
|
|
36
|
+
|
|
36
37
|
cb();
|
|
37
38
|
});
|
|
38
39
|
|
|
@@ -43,7 +44,9 @@ module.exports = function (filters, cb) {
|
|
|
43
44
|
} else if (file.state === 'deleted') {
|
|
44
45
|
remove(file);
|
|
45
46
|
}
|
|
47
|
+
|
|
46
48
|
delete file.state;
|
|
49
|
+
delete file.isNew;
|
|
47
50
|
cb();
|
|
48
51
|
});
|
|
49
52
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('path');
|
|
4
|
+
var extend = require('deep-extend');
|
|
5
|
+
var ejs = require('ejs');
|
|
6
|
+
|
|
7
|
+
module.exports = function (from, to, context, tplSettings, options) {
|
|
8
|
+
context = context || {};
|
|
9
|
+
|
|
10
|
+
this.copy(from, to, extend(options || {}, {
|
|
11
|
+
process: function (contents, filename) {
|
|
12
|
+
return ejs.render(
|
|
13
|
+
contents.toString(),
|
|
14
|
+
context,
|
|
15
|
+
// Setting filename by default allow including partials.
|
|
16
|
+
extend({filename: filename}, tplSettings || {})
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
};
|
|
@@ -5,12 +5,12 @@ var fs = require('fs');
|
|
|
5
5
|
var path = require('path');
|
|
6
6
|
var glob = require('glob');
|
|
7
7
|
var globby = require('globby');
|
|
8
|
-
var
|
|
8
|
+
var extend = require('deep-extend');
|
|
9
9
|
var File = require('vinyl');
|
|
10
|
-
var util = require('../util
|
|
10
|
+
var util = require('../util');
|
|
11
11
|
|
|
12
|
-
function applyProcessingFunc(process, contents) {
|
|
13
|
-
var output = process(contents);
|
|
12
|
+
function applyProcessingFunc(process, contents, filename) {
|
|
13
|
+
var output = process(contents, filename);
|
|
14
14
|
return output instanceof Buffer ? output : new Buffer(output);
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -28,7 +28,7 @@ exports.copy = function(from, to, options) {
|
|
|
28
28
|
'When copying with glob patterns, provide a directory as destination'
|
|
29
29
|
);
|
|
30
30
|
|
|
31
|
-
var globOptions =
|
|
31
|
+
var globOptions = extend(options.globOptions || {}, { nodir: true });
|
|
32
32
|
var files = globby.sync(from, globOptions);
|
|
33
33
|
var root = util.getCommonPath(from);
|
|
34
34
|
|
|
@@ -46,15 +46,10 @@ exports._copySingle = function (from, to, options) {
|
|
|
46
46
|
|
|
47
47
|
var file = this.store.get(from);
|
|
48
48
|
|
|
49
|
-
var
|
|
50
|
-
newFile.cwd = process.cwd();
|
|
51
|
-
newFile.base = path.basename(to);
|
|
52
|
-
newFile.path = to;
|
|
53
|
-
newFile.state = 'modified';
|
|
54
|
-
|
|
49
|
+
var contents = file.contents;
|
|
55
50
|
if (options.process) {
|
|
56
|
-
|
|
51
|
+
contents = applyProcessingFunc(options.process, file.contents, file.path);
|
|
57
52
|
}
|
|
58
53
|
|
|
59
|
-
this.
|
|
54
|
+
this.write(to, contents, file.stat);
|
|
60
55
|
};
|
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
var assert = require('assert');
|
|
4
4
|
var path = require('path');
|
|
5
|
-
var _ = require('lodash');
|
|
6
5
|
var globby = require('globby');
|
|
7
6
|
var multimatch = require('multimatch');
|
|
8
|
-
var util = require('../util
|
|
7
|
+
var util = require('../util');
|
|
9
8
|
|
|
10
9
|
function deleteFile(path, store) {
|
|
11
10
|
var file = store.get(path);
|
|
12
11
|
file.state = 'deleted';
|
|
13
|
-
file.contents =
|
|
12
|
+
file.contents = null;
|
|
14
13
|
store.add(file);
|
|
15
14
|
}
|
|
16
15
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var extend = require('deep-extend');
|
|
4
|
+
|
|
5
|
+
module.exports = function (filepath, contents, replacer, space) {
|
|
6
|
+
var originalContent = this.readJSON(filepath, {});
|
|
7
|
+
var newContent = extend({}, originalContent, contents);
|
|
8
|
+
|
|
9
|
+
this.writeJSON(filepath, newContent, replacer, space);
|
|
10
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function (filepath, defaults) {
|
|
4
|
+
if (this.exists(filepath)) {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(this.read(filepath));
|
|
7
|
+
} catch (error) {
|
|
8
|
+
throw new Error('Could not parse JSON in file: ' + filepath + '. Detail: ' + error.message);
|
|
9
|
+
}
|
|
10
|
+
} else {
|
|
11
|
+
return defaults;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
var assert = require('assert');
|
|
4
4
|
|
|
5
|
-
module.exports = function (
|
|
5
|
+
module.exports = function (filepath, options) {
|
|
6
6
|
options = options || { raw: false };
|
|
7
|
-
var file = this.store.get(
|
|
7
|
+
var file = this.store.get(filepath);
|
|
8
8
|
|
|
9
9
|
if (file.state === 'deleted' || file.contents === null) {
|
|
10
10
|
if (typeof options.defaults === 'string' || options.defaults instanceof Buffer) {
|
|
@@ -15,7 +15,7 @@ module.exports = function (path, options) {
|
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
assert(file.contents !== null,
|
|
18
|
+
assert(file.contents !== null, filepath + ' doesn\'t exist');
|
|
19
19
|
|
|
20
20
|
return options.raw ? file.contents : file.contents.toString();
|
|
21
21
|
};
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
var DEFAULT_INDENTATION = 2;
|
|
4
4
|
|
|
5
|
-
module.exports = function (
|
|
5
|
+
module.exports = function (filepath, contents, replacer, space) {
|
|
6
6
|
var jsonStr = JSON.stringify(contents, replacer || null, space || DEFAULT_INDENTATION) + '\n';
|
|
7
7
|
|
|
8
|
-
return this.write(
|
|
8
|
+
return this.write(filepath, jsonStr);
|
|
9
9
|
};
|
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
var assert = require('assert');
|
|
4
4
|
|
|
5
|
-
module.exports = function (
|
|
5
|
+
module.exports = function (filepath, contents, stat) {
|
|
6
6
|
assert(
|
|
7
7
|
typeof contents === 'string' || contents instanceof Buffer,
|
|
8
8
|
'Expected `contents` to be a String or a Buffer'
|
|
9
9
|
);
|
|
10
|
-
|
|
11
|
-
file
|
|
10
|
+
|
|
11
|
+
var file = this.store.get(filepath);
|
|
12
|
+
file.isNew = file.contents === null;
|
|
12
13
|
file.state = 'modified';
|
|
14
|
+
file.contents = typeof contents === 'string' ? new Buffer(contents) : contents;
|
|
15
|
+
file.stat = stat;
|
|
13
16
|
this.store.add(file);
|
|
17
|
+
|
|
14
18
|
return file.contents.toString();
|
|
15
19
|
};
|
|
@@ -9,6 +9,7 @@ EditionInterface.prototype.readJSON = require('./actions/read-json.js');
|
|
|
9
9
|
EditionInterface.prototype.exists = require( './actions/exists' );
|
|
10
10
|
EditionInterface.prototype.write = require('./actions/write.js');
|
|
11
11
|
EditionInterface.prototype.writeJSON = require('./actions/write-json.js');
|
|
12
|
+
EditionInterface.prototype.extendJSON = require('./actions/extend-json.js');
|
|
12
13
|
EditionInterface.prototype.delete = require('./actions/delete.js');
|
|
13
14
|
EditionInterface.prototype.copy = require('./actions/copy.js').copy;
|
|
14
15
|
EditionInterface.prototype._copySingle = require('./actions/copy.js')._copySingle;
|
package/{util → lib}/util.js
RENAMED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mem-fs-editor",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "File edition helpers working on top of mem-fs",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "mocha"
|
|
@@ -8,27 +8,26 @@
|
|
|
8
8
|
"repository": "SBoudrias/mem-fs-editor",
|
|
9
9
|
"author": "Simon Boudrias",
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"main": "lib/index.js",
|
|
11
12
|
"files": [
|
|
12
|
-
"
|
|
13
|
-
"util",
|
|
14
|
-
"index.js"
|
|
13
|
+
"lib"
|
|
15
14
|
],
|
|
16
15
|
"dependencies": {
|
|
17
16
|
"commondir": "^1.0.1",
|
|
17
|
+
"deep-extend": "^0.4.0",
|
|
18
18
|
"ejs": "^2.3.1",
|
|
19
|
-
"glob": "^
|
|
20
|
-
"globby": "^
|
|
21
|
-
"lodash": "^3.6.0",
|
|
19
|
+
"glob": "^7.0.3",
|
|
20
|
+
"globby": "^4.0.0",
|
|
22
21
|
"mkdirp": "^0.5.0",
|
|
23
22
|
"multimatch": "^2.0.0",
|
|
24
23
|
"rimraf": "^2.2.8",
|
|
25
|
-
"sinon": "^1.12.2",
|
|
26
24
|
"through2": "^2.0.0",
|
|
27
|
-
"vinyl": "^
|
|
25
|
+
"vinyl": "^1.1.0"
|
|
28
26
|
},
|
|
29
27
|
"devDependencies": {
|
|
30
28
|
"escape-regexp": "0.0.1",
|
|
31
29
|
"mem-fs": "^1.0.0",
|
|
32
|
-
"mocha": "^2.1.0"
|
|
30
|
+
"mocha": "^2.1.0",
|
|
31
|
+
"sinon": "^1.12.2"
|
|
33
32
|
}
|
|
34
33
|
}
|
package/actions/copy-tpl.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var path = require('path');
|
|
4
|
-
var _ = require('lodash');
|
|
5
|
-
var ejs = require('ejs');
|
|
6
|
-
|
|
7
|
-
module.exports = function (from, to, context, tplSettings) {
|
|
8
|
-
context = context || {};
|
|
9
|
-
tplSettings = tplSettings || {};
|
|
10
|
-
|
|
11
|
-
_.defaults(tplSettings, {
|
|
12
|
-
// Setting filename by default allow including partials.
|
|
13
|
-
filename: from
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
this.copy(from, to, {
|
|
17
|
-
process: function (contents) {
|
|
18
|
-
return ejs.render(contents.toString(), context, tplSettings);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
};
|
package/actions/exists.js
DELETED
package/actions/read-json.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = function (path, defaults) {
|
|
4
|
-
if (this.exists(path)) {
|
|
5
|
-
try {
|
|
6
|
-
return JSON.parse(this.read(path));
|
|
7
|
-
} catch (error) {
|
|
8
|
-
throw new Error('Could not parse JSON in file: ' + path + '. Detail: ' + error.message);
|
|
9
|
-
}
|
|
10
|
-
} else {
|
|
11
|
-
return defaults;
|
|
12
|
-
}
|
|
13
|
-
};
|