@putout/operator-rename-files 6.1.2 → 6.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
@@ -56,19 +56,35 @@ module.exports = renameFiles({
56
56
  });
57
57
  ```
58
58
 
59
- ### renameFiles(mask, from, to)
59
+ ### renameFiles({mask, from, to})
60
60
 
61
61
  You can pass `from` and `to` instead of `rename` for declarative renaming.
62
62
 
63
63
  ```js
64
64
  export const {
65
- report,
66
- fix,
67
- scan,
65
+ report,
66
+ fix,
67
+ scan,
68
+ } = renameFiles({
69
+ mask: '*.spec.*',
70
+ from: 'spec',
71
+ to: 'test',
72
+ });
73
+ ```
74
+
75
+ ### renameFiles({from, to, near})
76
+
77
+ Rename files located near `package.json`:
78
+
79
+ ```js
80
+ export const {
81
+ report,
82
+ fix,
83
+ scan,
68
84
  } = renameFiles({
69
- mask: '*.spec.*',
70
- from: 'spec',
71
- to: 'test',
85
+ from: 'madrun.js',
86
+ to: '.madrun.js',
87
+ near: 'package.json',
72
88
  });
73
89
  ```
74
90
 
@@ -1,4 +1,13 @@
1
- import {renameFile, getFilename} from '@putout/operator-filesystem';
1
+ import {basename} from 'node:path';
2
+ import {
3
+ readDirectory,
4
+ renameFile,
5
+ getFilename,
6
+ getFileType,
7
+ getParentDirectory,
8
+ } from '@putout/operator-filesystem';
9
+
10
+ const returns = (a) => () => a;
2
11
 
3
12
  export const report = (path, {mask, from, to}) => {
4
13
  if (!mask)
@@ -18,11 +27,13 @@ export const createScan = (baseOptions) => (rootPath, {push, options, trackFile}
18
27
  const from = options.from || baseOptions.from;
19
28
  const to = options.to || baseOptions.to;
20
29
  const mask = options.mask || baseOptions.mask;
30
+ const near = options.near || baseOptions.near;
31
+ const checkNear = near ? createCheckNear(near) : returns(true);
21
32
 
22
33
  if (!from || !to)
23
34
  return {};
24
35
 
25
- for (const file of trackFile(rootPath, mask || from)) {
36
+ for (const file of trackFile(rootPath, mask || from).filter(checkNear)) {
26
37
  push(file, {
27
38
  from,
28
39
  to,
@@ -30,3 +41,22 @@ export const createScan = (baseOptions) => (rootPath, {push, options, trackFile}
30
41
  });
31
42
  }
32
43
  };
44
+
45
+ const createCheckNear = (near) => (file) => {
46
+ const parentDirectory = getParentDirectory(file);
47
+
48
+ for (const currentFile of readDirectory(parentDirectory)) {
49
+ const type = getFileType(currentFile);
50
+
51
+ if (type !== 'file')
52
+ continue;
53
+
54
+ const name = getFilename(currentFile);
55
+ const base = basename(name);
56
+
57
+ if (base === near)
58
+ return true;
59
+ }
60
+
61
+ return false;
62
+ };
@@ -1,7 +1,7 @@
1
1
  import * as renameFileWithFn from './rename-file-with-fn.js';
2
2
  import * as renameFileByMask from './rename-file-by-mask.js';
3
3
 
4
- export const renameFiles = ({type, mask, rename, from, to} = {}) => {
4
+ export const renameFiles = ({type, mask, rename, from, to, near} = {}) => {
5
5
  if (rename) {
6
6
  const {
7
7
  report,
@@ -33,6 +33,7 @@ export const renameFiles = ({type, mask, rename, from, to} = {}) => {
33
33
  mask,
34
34
  from,
35
35
  to,
36
+ near,
36
37
  }),
37
38
  };
38
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-rename-files",
3
- "version": "6.1.2",
3
+ "version": "6.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 rename files to plugins",