@putout/operator-filesystem 1.4.0 → 1.5.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
@@ -13,6 +13,16 @@ npm i putout @putout/operator-filesystem
13
13
 
14
14
  ## API
15
15
 
16
+ ## `createDirectory(directoryPath: Path, name: string)`
17
+
18
+ ```js
19
+ const {operator} = require('putout');
20
+ const {createDirectory} = operator;
21
+
22
+ const [dirPath] = findFile(ast, 'hello');
23
+ createDirectory(dirPath, 'world');// returns directoryPath
24
+ ```
25
+
16
26
  ## `findFile(path: Path, name: string)`
17
27
 
18
28
  ```js
package/lib/filesystem.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {types} = require('putout');
3
4
  const {
4
5
  setLiteralValue,
5
6
  getProperty,
@@ -8,6 +9,13 @@ const {
8
9
 
9
10
  const maybeFS = require('./maybe-fs');
10
11
 
12
+ const {
13
+ ObjectExpression,
14
+ ArrayExpression,
15
+ StringLiteral,
16
+ ObjectProperty,
17
+ } = types;
18
+
11
19
  const getRegExp = (wildcard) => {
12
20
  const escaped = wildcard
13
21
  .replace(/\./g, '\\.')
@@ -82,5 +90,31 @@ module.exports.moveFile = (filePath, dirPath) => {
82
90
  maybeFS.renameFile(filename, newname);
83
91
  };
84
92
 
93
+ const createType = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
94
+ const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
95
+ const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
96
+
97
+ module.exports.createDirectory = (dirPath, name) => {
98
+ const dirPathFiles = getProperty(dirPath, 'files');
99
+ const parentFilename = getFilename(dirPath);
100
+ const filename = `${parentFilename}/${name}`;
101
+
102
+ const typeProperty = createType('directory');
103
+ const filesProperty = createFiles([]);
104
+ const filenameProperty = createFilename(filename);
105
+
106
+ dirPathFiles.node.value.elements.push(ObjectExpression([
107
+ typeProperty,
108
+ filenameProperty,
109
+ filesProperty,
110
+ ]));
111
+
112
+ maybeFS.createDirectory(filename);
113
+
114
+ return dirPathFiles
115
+ .get('value.elements')
116
+ .at(-1);
117
+ };
118
+
85
119
  module.exports.init = maybeFS.init;
86
120
  module.exports.deinit = maybeFS.deinit;
package/lib/maybe-fs.js CHANGED
@@ -6,6 +6,7 @@ const noop = () => {};
6
6
  const defaultFS = {
7
7
  renameFile: noop,
8
8
  removeFile: noop,
9
+ createDirectory: noop,
9
10
  };
10
11
 
11
12
  const maybeFS = assign({}, defaultFS);
@@ -18,6 +19,10 @@ module.exports.removeFile = (name) => {
18
19
  maybeFS.removeFile(name);
19
20
  };
20
21
 
22
+ module.exports.createDirectory = (name) => {
23
+ maybeFS.createDirectory(name);
24
+ };
25
+
21
26
  module.exports.init = (fsDriver) => {
22
27
  assign(maybeFS, fsDriver);
23
28
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "1.4.0",
3
+ "version": "1.5.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",