@putout/operator-filesystem 10.0.4 → 10.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
@@ -102,7 +102,7 @@ readDirectory(dirPath);
102
102
 
103
103
  ```js
104
104
  const {operator} = require('putout');
105
- const {finedFiles, findFile} = operator;
105
+ const {findFile} = operator;
106
106
 
107
107
  const [filePath] = findFile(ast, 'hello');
108
108
  ```
@@ -135,6 +135,23 @@ const {findFile} = operator;
135
135
  const coupleFiles = findFile(ast, ['/home/coderaiser', '/home/coderaiser/putout']);
136
136
  ```
137
137
 
138
+ ### `getFile(directoryPath: DirectoryPath, name: string, options?: Options): FilePath`
139
+
140
+ ```ts
141
+ type Options = {
142
+ type?: 'file' | 'directory';
143
+ };
144
+ ```
145
+
146
+ Get file named `name` from `directoryPath`
147
+
148
+ ```js
149
+ const {operator} = require('putout');
150
+ const {getFile} = operator;
151
+
152
+ const filePath = getFile(root, 'package.json');
153
+ ```
154
+
138
155
  ### `getParentDirectory(path: FilePath | DirectoryPath): FilePath | null`
139
156
 
140
157
  ```js
package/lib/filesystem.js CHANGED
@@ -137,9 +137,17 @@ export function getFileContent(filePath) {
137
137
  ];
138
138
  }
139
139
 
140
+ function checkRenameFileName(name) {
141
+ if (!isString(name))
142
+ throw Error(`☝️ Looks like you forget to pass the 'name' of a file to 'renameFile(filePath: FilePath, name: string)'`);
143
+ }
144
+
140
145
  export const renameFile = (filePath, name) => {
146
+ checkRenameFileName(name);
147
+
141
148
  const oldName = getFilename(filePath);
142
149
  const valuePath = getFilenamePath(filePath);
150
+
143
151
  const baseName = oldName
144
152
  .split('/')
145
153
  .pop();
@@ -293,6 +301,17 @@ export function readDirectory(dirPath) {
293
301
  }
294
302
 
295
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
+
296
315
  const dirPathFiles = getFiles(dirPath);
297
316
  const parentFilename = getFilename(dirPath);
298
317
  const filename = join(parentFilename, name);
@@ -414,6 +433,22 @@ export function getRootDirectory(path) {
414
433
  return prevPath;
415
434
  }
416
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
+
417
452
  export const {
418
453
  init,
419
454
  deinit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "10.0.4",
3
+ "version": "10.2.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",
@@ -37,13 +37,13 @@
37
37
  ],
38
38
  "devDependencies": {
39
39
  "@putout/engine-parser": "^15.0.1",
40
- "@putout/eslint-flat": "^3.0.0",
40
+ "@putout/eslint-flat": "^4.0.0",
41
41
  "@putout/operator-json": "^3.0.0",
42
42
  "@putout/test": "^15.0.0",
43
43
  "c8": "^10.0.0",
44
44
  "eslint": "^10.0.0-alpha.0",
45
45
  "eslint-plugin-n": "^17.0.0",
46
- "eslint-plugin-putout": "^29.0.0",
46
+ "eslint-plugin-putout": "^30.0.0",
47
47
  "madrun": "^12.0.0",
48
48
  "montag": "^1.2.1",
49
49
  "nodemon": "^3.0.1",