mem-fs-editor 5.0.0 → 7.0.1

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 CHANGED
@@ -1,19 +1,17 @@
1
- mem-fs-editor [![Build Status](https://api.travis-ci.org/SBoudrias/mem-fs-editor.svg?branch=master)](https://travis-ci.org/SBoudrias/mem-fs-editor) [![NPM version](https://badge.fury.io/js/mem-fs-editor.svg)](http://badge.fury.io/js/mem-fs-editor) [![Coverage Status](https://coveralls.io/repos/github/SBoudrias/mem-fs-editor/badge.svg)](https://coveralls.io/github/SBoudrias/mem-fs-editor)
2
- =============
1
+ # mem-fs-editor [![Build Status](https://api.travis-ci.org/SBoudrias/mem-fs-editor.svg?branch=master)](https://travis-ci.org/SBoudrias/mem-fs-editor) [![NPM version](https://badge.fury.io/js/mem-fs-editor.svg)](http://badge.fury.io/js/mem-fs-editor) [![Coverage Status](https://coveralls.io/repos/github/SBoudrias/mem-fs-editor/badge.svg)](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('mem-fs');
11
- var editor = require('mem-fs-editor');
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('somefile.js', 'var a = 1;');
14
+ fs.write("somefile.js", "var a = 1;");
17
15
  ```
18
16
 
19
17
  ### `#read(filepath, [options])`
@@ -61,7 +59,7 @@ 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/isaacs/node-glob#options). The `sync` flag is forced to be `true` in `globOptions`.
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
64
  ### `#copy(from, to, [options], context[, templateOptions ])`
67
65
 
@@ -69,7 +67,11 @@ 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
- `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.
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
+
74
+ Optionally, when `from` is a glob pattern, pass an `options.processDestinationPath` function (`processDestinationPath(destinationFile)`) returning a string who'll become the new file name.
73
75
 
74
76
  ### `#copyTpl(from, to, context[, templateOptions [, copyOptions]])`
75
77
 
@@ -85,7 +87,9 @@ Templates syntax looks like this:
85
87
  <%= value %>
86
88
  <%- include('partial.ejs', { name: 'Simon' }) %>
87
89
  ```
90
+
88
91
  Dir syntax looks like this:
92
+
89
93
  ```
90
94
  /some/path/dir<%= value %>/...
91
95
  ```
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var extend = require('deep-extend');
4
3
  var EOL = require('os').EOL;
5
4
 
6
5
  module.exports = function (to, contents, options) {
7
- options = extend({
6
+ options = {
8
7
  trimEnd: true,
9
- separator: EOL
10
- }, options || {});
8
+ separator: EOL,
9
+ ...options
10
+ };
11
11
 
12
12
  var currentContents = this.read(to);
13
13
  if (options.trimEnd) {
@@ -11,6 +11,7 @@ function write(file) {
11
11
  if (!fs.existsSync(dir)) {
12
12
  mkdirp.sync(dir);
13
13
  }
14
+
14
15
  fs.writeFileSync(file.path, file.contents, {
15
16
  mode: file.stat ? file.stat.mode : null
16
17
  });
@@ -1,21 +1,20 @@
1
1
  'use strict';
2
2
 
3
- var extend = require('deep-extend');
4
3
  var ejs = require('ejs');
5
- var isBinaryFile = require('isbinaryfile');
4
+ var isBinaryFileSync = require('isbinaryfile').isBinaryFileSync;
6
5
 
7
6
  function render(contents, filename, context, tplSettings) {
8
7
  let result;
9
8
 
10
9
  const contentsBuffer = Buffer.from(contents, 'binary');
11
- if (isBinaryFile.sync(contentsBuffer, contentsBuffer.length)) {
10
+ if (isBinaryFileSync(contentsBuffer, contentsBuffer.length)) {
12
11
  result = contentsBuffer;
13
12
  } else {
14
13
  result = ejs.render(
15
14
  contents.toString(),
16
15
  context,
17
16
  // Setting filename by default allow including partials.
18
- extend({filename: filename}, tplSettings)
17
+ {filename: filename, ...tplSettings}
19
18
  );
20
19
  }
21
20
 
@@ -26,10 +25,17 @@ module.exports = function (from, to, context, tplSettings, options) {
26
25
  context = context || {};
27
26
  tplSettings = tplSettings || {};
28
27
 
29
- this.copy(from, to, extend(options || {}, {
30
- process: function (contents, filename) {
31
- return render(contents, filename, context, tplSettings);
32
- }
33
- }),
34
- context, tplSettings);
28
+ this.copy(
29
+ from,
30
+ to,
31
+ {
32
+ processDestinationPath: path => path.replace(/.ejs$/, ''),
33
+ ...options,
34
+ process: function (contents, filename) {
35
+ return render(contents, filename, context, tplSettings);
36
+ }
37
+ },
38
+ context,
39
+ tplSettings
40
+ );
35
41
  };
@@ -5,7 +5,6 @@ var fs = require('fs');
5
5
  var path = require('path');
6
6
  var glob = require('glob');
7
7
  var globby = require('globby');
8
- var extend = require('deep-extend');
9
8
  var multimatch = require('multimatch');
10
9
  var ejs = require('ejs');
11
10
  var util = require('../util');
@@ -20,11 +19,12 @@ exports.copy = function (from, to, options, context, tplSettings) {
20
19
  options = options || {};
21
20
  var fromGlob = util.globify(from);
22
21
 
23
- var globOptions = extend(options.globOptions || {}, {nodir: true});
22
+ var globOptions = {...options.globOptions, nodir: true};
24
23
  var diskFiles = globby.sync(fromGlob, globOptions);
25
24
  var storeFiles = [];
26
25
  this.store.each(file => {
27
- if (multimatch([file.path], fromGlob).length !== 0) {
26
+ // The store may have a glob path and when we try to copy it will fail because not real file
27
+ if (!glob.hasMagic(file.path) && multimatch([file.path], fromGlob).length !== 0) {
28
28
  storeFiles.push(file.path);
29
29
  }
30
30
  });
@@ -37,15 +37,16 @@ exports.copy = function (from, to, options, context, tplSettings) {
37
37
  'When copying multiple files, provide a directory as destination'
38
38
  );
39
39
 
40
+ const processDestinationPath = options.processDestinationPath || (path => path);
40
41
  var root = util.getCommonPath(from);
41
42
  generateDestination = filepath => {
42
43
  var toFile = path.relative(root, filepath);
43
- return path.join(to, toFile);
44
+ return processDestinationPath(path.join(to, toFile));
44
45
  };
45
46
  }
46
47
 
47
48
  // Sanity checks: Makes sure we copy at least one file.
48
- 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);
49
50
 
50
51
  files.forEach(file => {
51
52
  this._copySingle(file, generateDestination(file), options, context, tplSettings);
@@ -63,6 +64,7 @@ exports._copySingle = function (from, to, options, context, tplSettings) {
63
64
  if (options.process) {
64
65
  contents = applyProcessingFunc(options.process, file.contents, file.path);
65
66
  }
67
+
66
68
  if (context) {
67
69
  to = ejs.render(to, context, tplSettings);
68
70
  }
@@ -16,6 +16,7 @@ module.exports = function (paths, options) {
16
16
  if (!Array.isArray(paths)) {
17
17
  paths = [paths];
18
18
  }
19
+
19
20
  paths = paths.map(function (filePath) {
20
21
  return path.resolve(filePath);
21
22
  });
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,6 +1,6 @@
1
1
  {
2
2
  "name": "mem-fs-editor",
3
- "version": "5.0.0",
3
+ "version": "7.0.1",
4
4
  "description": "File edition helpers working on top of mem-fs",
5
5
  "scripts": {
6
6
  "pretest": "eslint .",
@@ -16,28 +16,31 @@
16
16
  "dependencies": {
17
17
  "commondir": "^1.0.1",
18
18
  "deep-extend": "^0.6.0",
19
- "ejs": "^2.5.9",
20
- "glob": "^7.0.3",
21
- "globby": "^8.0.1",
22
- "isbinaryfile": "^3.0.2",
23
- "mkdirp": "^0.5.0",
24
- "multimatch": "^2.0.0",
25
- "rimraf": "^2.2.8",
26
- "through2": "^2.0.0",
27
- "vinyl": "^2.0.1"
19
+ "ejs": "^3.0.1",
20
+ "glob": "^7.1.4",
21
+ "globby": "^9.2.0",
22
+ "isbinaryfile": "^4.0.0",
23
+ "mkdirp": "^1.0.0",
24
+ "multimatch": "^4.0.0",
25
+ "rimraf": "^3.0.0",
26
+ "through2": "^3.0.1",
27
+ "vinyl": "^2.2.0"
28
28
  },
29
29
  "devDependencies": {
30
- "coveralls": "^3.0.0",
30
+ "coveralls": "^3.0.3",
31
31
  "escape-regexp": "0.0.1",
32
- "eslint": "^5.0.1",
33
- "eslint-config-xo-space": "^0.19.0",
34
- "jest": "^23.2.0",
32
+ "eslint": "^6.0.0",
33
+ "eslint-config-xo-space": "^0.21.0",
34
+ "jest": "^24.8.0",
35
35
  "mem-fs": "^1.0.0",
36
- "sinon": "^6.0.1"
36
+ "sinon": "^8.0.0"
37
37
  },
38
38
  "jest": {
39
39
  "collectCoverage": true,
40
40
  "coverageDirectory": "coverage",
41
41
  "testEnvironment": "node"
42
+ },
43
+ "engines": {
44
+ "node": ">=10.0.0"
42
45
  }
43
46
  }