create = function (store) { return new EditionInterface(store); }
...
Usage
-------------
```js
var memFs = require('mem-fs');
var editor = require('mem-fs-editor');
var store = memFs.create();
var fs = editor.create(store);
fs.write('somefile.js', 'var a = 1;');
```
### `#read(filepath, [options])`
...
copy = function (from, to, options) { to = path.resolve(to); options = options || {}; var fromGlob = util.globify(from); var globOptions = extend(options.globOptions || {}, {nodir: true}); var diskFiles = globby.sync(fromGlob, globOptions); var storeFiles = []; this.store.each(file => { if (multimatch([file.path], fromGlob).length !== 0) { storeFiles.push(file.path); } }); var files = diskFiles.concat(storeFiles); var generateDestination = () => to; if (Array.isArray(from) || !this.exists(from) || glob.hasMagic(from)) { assert( !this.exists(to) || fs.statSync(to).isDirectory(), 'When copying multiple files, provide a directory as destination' ); var root = util.getCommonPath(from); generateDestination = filepath => { var toFile = path.relative(root, filepath); return path.join(to, toFile); }; } // Sanity checks: Makes sure we copy at least one file. assert(files.length > 0, 'Trying to copy from a source that does not exist: ' + from); files.forEach(file => { this._copySingle(file, generateDestination(file), options); }); }
...
var path = require('path');
var extend = require('deep-extend');
var ejs = require('ejs');
module.exports = function (from, to, context, tplSettings, options) {
context = context || {};
this.copy(from, to, extend(options || {}, {
process: function (contents, filename) {
return ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings || {})
);
...
_copySingle = function (from, to, options) { options = options || {}; assert(this.exists(from), 'Trying to copy from a source that does not exist: ' + from); var file = this.store.get(from); var contents = file.contents; if (options.process) { contents = applyProcessingFunc(options.process, file.contents, file.path); } this.write(to, contents, file.stat); }
...
};
}
// Sanity checks: Makes sure we copy at least one file.
assert(files.length > 0, 'Trying to copy from a source that does not exist: ' + from);
files.forEach(file => {
this._copySingle(file, generateDestination(file), options);
});
};
exports._copySingle = function (from, to, options) {
options = options || {};
assert(this.exists(from), 'Trying to copy from a source that does not exist: ' + from);
...
getCommonPath = function (filePath) { if (Array.isArray(filePath)) { filePath = filePath .filter(notNullOrExclusion) .map(this.getCommonPath.bind(this)); return commondir(filePath); } var globStartIndex = filePath.indexOf('*'); if (globStartIndex !== -1) { filePath = filePath.substring(0, globStartIndex + 1); } else if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) { return filePath; } return path.dirname(filePath); }
...
var generateDestination = () => to;
if (Array.isArray(from) || !this.exists(from) || glob.hasMagic(from)) {
assert(
!this.exists(to) || fs.statSync(to).isDirectory(),
'When copying multiple files, provide a directory as destination'
);
var root = util.getCommonPath(from);
generateDestination = filepath => {
var toFile = path.relative(root, filepath);
return path.join(to, toFile);
};
}
// Sanity checks: Makes sure we copy at least one file.
...
globify = function (filePath) { if (Array.isArray(filePath)) { return filePath.reduce((memo, pattern) => memo.concat(this.globify(pattern)), []); } if (glob.hasMagic(filePath)) { return filePath; } else if (!fs.existsSync(filePath)) { // The target of a pattern who's not a glob and doesn't match an existing // entity on the disk is ambiguous. As such, match both files and directories. return [ filePath, path.join(filePath, '**') ]; } var fsStats = fs.statSync(filePath); if (fsStats.isFile()) { return filePath; } else if (fsStats.isDirectory()) { return path.join(filePath, '**'); } throw new Error('Only file path or directory path are supported.'); }
...
}
return path.dirname(filePath);
};
exports.globify = function (filePath) {
if (Array.isArray(filePath)) {
return filePath.reduce((memo, pattern) => memo.concat(this.globify(pattern)), []);
}
if (glob.hasMagic(filePath)) {
return filePath;
} else if (!fs.existsSync(filePath)) {
// The target of a pattern who's not a glob and doesn't match an existing
// entity on the disk is ambiguous. As such, match both files and directories.
...