@putout/operator-filesystem 10.1.0 → 10.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 CHANGED
@@ -135,15 +135,21 @@ const {findFile} = operator;
135
135
  const coupleFiles = findFile(ast, ['/home/coderaiser', '/home/coderaiser/putout']);
136
136
  ```
137
137
 
138
- ### `findFirstFileUp(directoryPath: DirectoryPath, name: string): [directory, FilePath]`
138
+ ### `getFile(directoryPath: DirectoryPath, name: string, options?: Options): FilePath`
139
139
 
140
- Find first file with `name` in all parent directories.
140
+ ```ts
141
+ type Options = {
142
+ type?: 'file' | 'directory';
143
+ };
144
+ ```
145
+
146
+ Get file named `name` from `directoryPath`
141
147
 
142
148
  ```js
143
149
  const {operator} = require('putout');
144
- const {findFileUp} = operator;
150
+ const {getFile} = operator;
145
151
 
146
- const [directory, filePath] = findFirstFileUp(ast, 'package.json');
152
+ const filePath = getFile(root, 'package.json');
147
153
  ```
148
154
 
149
155
  ### `getParentDirectory(path: FilePath | DirectoryPath): FilePath | null`
package/lib/filesystem.js CHANGED
@@ -301,6 +301,17 @@ export function readDirectory(dirPath) {
301
301
  }
302
302
 
303
303
  export function createDirectory(dirPath, name) {
304
+ const existed = getFile(dirPath, name);
305
+
306
+ if (existed) {
307
+ const fileType = getFileType(existed);
308
+
309
+ if (fileType === 'directory')
310
+ return existed;
311
+
312
+ removeFile(existed);
313
+ }
314
+
304
315
  const dirPathFiles = getFiles(dirPath);
305
316
  const parentFilename = getFilename(dirPath);
306
317
  const filename = join(parentFilename, name);
@@ -422,6 +433,22 @@ export function getRootDirectory(path) {
422
433
  return prevPath;
423
434
  }
424
435
 
436
+ export function getFile(directoryPath, name, {type} = {}) {
437
+ for (const currentFile of readDirectory(directoryPath)) {
438
+ const currentName = getFilename(currentFile);
439
+
440
+ if (!currentName.endsWith(name))
441
+ continue;
442
+
443
+ if (type && type !== getFileType(currentFile))
444
+ continue;
445
+
446
+ return currentFile;
447
+ }
448
+
449
+ return null;
450
+ }
451
+
425
452
  export const {
426
453
  init,
427
454
  deinit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "10.1.0",
3
+ "version": "10.3.0",
4
4
  "type": "module",
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",