@putout/operator-filesystem 3.0.2 → 3.2.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 CHANGED
@@ -53,7 +53,7 @@ const [dirPath] = findFile(ast, 'hello');
53
53
  const newDirectoryPath = createDirectory(dirPath, 'world');
54
54
  ```
55
55
 
56
- ### `findFile(path: Path, name: string | string[]): FilePath[]`
56
+ ### `findFile(filePath: Path|FilePath, name: string | string[]): FilePath[]`
57
57
 
58
58
  ```js
59
59
  const {operator} = require('putout');
package/lib/filesystem.js CHANGED
@@ -11,6 +11,7 @@ const {
11
11
  } = require('@putout/operate');
12
12
 
13
13
  const maybeFS = require('./maybe-fs');
14
+ const isString = (a) => typeof a === 'string';
14
15
  const {isArray} = Array;
15
16
  const maybeArray = (a) => isArray(a) ? a : [a];
16
17
 
@@ -43,6 +44,8 @@ module.exports.getParentDirectory = (filePath) => {
43
44
  module.exports.findFile = findFile;
44
45
 
45
46
  function findFile(node, name) {
47
+ checkName(name);
48
+
46
49
  const filePaths = [];
47
50
  const names = maybeArray(name);
48
51
 
@@ -58,6 +61,11 @@ function findFile(node, name) {
58
61
  return filePaths;
59
62
  }
60
63
 
64
+ function checkName(name) {
65
+ if (!isString(name) && !isArray(name))
66
+ throw Error(`☝️ Looks like you forget to pass the 'name' of a file to 'findFile(filePath: Path|FilePath, name: string | string[]): FilePath'`);
67
+ }
68
+
61
69
  function getFilenamePath(filePath) {
62
70
  const filenamePath = getProperty(filePath, 'filename');
63
71
  return filenamePath.get('value');
@@ -68,10 +76,12 @@ function getFilename(filePath) {
68
76
  return value;
69
77
  }
70
78
 
71
- module.exports.getFileType = (filePath) => {
79
+ module.exports.getFileType = getFileType;
80
+
81
+ function getFileType(filePath) {
72
82
  const typePath = getProperty(filePath, 'type');
73
83
  return typePath.node.value.value;
74
- };
84
+ }
75
85
 
76
86
  function getFileContent(filePath) {
77
87
  const content = getProperty(filePath, 'content');
@@ -227,6 +237,11 @@ const createContentProperty = (content) => {
227
237
  };
228
238
 
229
239
  module.exports.readFileContent = (filePath) => {
240
+ const fileType = getFileType(filePath);
241
+
242
+ if (fileType === 'directory')
243
+ return '';
244
+
230
245
  const [hasContent, content] = getFileContent(filePath);
231
246
 
232
247
  if (hasContent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "3.0.2",
3
+ "version": "3.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout operator adds ability to filesystem referenced variables that was not defined",