@putout/operator-rename-files 6.0.3 → 6.1.1
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 +14 -0
- package/lib/rename-file-by-mask.js +32 -0
- package/lib/rename-file-with-fn.js +63 -0
- package/lib/rename-files.js +33 -68
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,20 @@ module.exports = renameFiles({
|
|
|
56
56
|
});
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
+
### renameFiles(mask, from, to)
|
|
60
|
+
|
|
61
|
+
You can pass `from` and `to` instead of `rename` for declarative renaming.
|
|
62
|
+
|
|
63
|
+
export const {
|
|
64
|
+
report,
|
|
65
|
+
fix,
|
|
66
|
+
scan,
|
|
67
|
+
} = renameFiles({
|
|
68
|
+
mask: '*.spec.*',
|
|
69
|
+
from: 'spec',
|
|
70
|
+
to: 'test',
|
|
71
|
+
});
|
|
72
|
+
|
|
59
73
|
You can also use [`redlint`](https://github.com/putoutjs/redlint) directly.
|
|
60
74
|
|
|
61
75
|
## License
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {renameFile, getFilename} from '@putout/operator-filesystem';
|
|
2
|
+
|
|
3
|
+
export const report = (path, {mask, from, to}) => {
|
|
4
|
+
if (!mask)
|
|
5
|
+
return `Rename '${from}' to '${to}'`;
|
|
6
|
+
|
|
7
|
+
return `Rename '${mask}' to '${mask.replace(from, to)}'`;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const fix = (path, {from, to}) => {
|
|
11
|
+
const filename = getFilename(path);
|
|
12
|
+
const newFilename = filename.replace(from, to);
|
|
13
|
+
|
|
14
|
+
renameFile(path, newFilename);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const createScan = (baseOptions) => (rootPath, {push, options, trackFile}) => {
|
|
18
|
+
const from = options.from || baseOptions.from;
|
|
19
|
+
const to = options.to || baseOptions.to;
|
|
20
|
+
const mask = options.mask || baseOptions.mask;
|
|
21
|
+
|
|
22
|
+
if (!from || !to)
|
|
23
|
+
return {};
|
|
24
|
+
|
|
25
|
+
for (const file of trackFile(rootPath, mask || from)) {
|
|
26
|
+
push(file, {
|
|
27
|
+
from,
|
|
28
|
+
to,
|
|
29
|
+
mask,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {join} from 'node:path';
|
|
2
|
+
import {
|
|
3
|
+
getParentDirectory,
|
|
4
|
+
getFilename,
|
|
5
|
+
readFileContent,
|
|
6
|
+
findFile,
|
|
7
|
+
renameFile,
|
|
8
|
+
} from '@putout/operator-filesystem';
|
|
9
|
+
|
|
10
|
+
const {parse} = JSON;
|
|
11
|
+
|
|
12
|
+
export const report = (file, {from, to}) => `Rename '${from}' to '${to}'`;
|
|
13
|
+
|
|
14
|
+
export const fix = (file, {to}) => {
|
|
15
|
+
renameFile(file, to);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const createScan = ({type, mask, rename} = {}) => (path, {push, trackFile}) => {
|
|
19
|
+
for (const file of trackFile(path, mask)) {
|
|
20
|
+
if (type && !checkType(type, file))
|
|
21
|
+
continue;
|
|
22
|
+
|
|
23
|
+
const from = getFilename(file);
|
|
24
|
+
const to = rename(from);
|
|
25
|
+
|
|
26
|
+
push(file, {
|
|
27
|
+
from,
|
|
28
|
+
to,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function checkType(type, file) {
|
|
34
|
+
const packagePath = findUpPackage(file);
|
|
35
|
+
|
|
36
|
+
if (type === 'commonjs' && !packagePath)
|
|
37
|
+
return true;
|
|
38
|
+
|
|
39
|
+
if (!packagePath)
|
|
40
|
+
return false;
|
|
41
|
+
|
|
42
|
+
const packageContent = readFileContent(packagePath);
|
|
43
|
+
|
|
44
|
+
if (!packageContent)
|
|
45
|
+
return false;
|
|
46
|
+
|
|
47
|
+
const info = parse(packageContent);
|
|
48
|
+
const infoType = info.type || 'commonjs';
|
|
49
|
+
|
|
50
|
+
return infoType === type;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function findUpPackage(file) {
|
|
54
|
+
let packageJSON;
|
|
55
|
+
let dirPath = getParentDirectory(file);
|
|
56
|
+
|
|
57
|
+
do {
|
|
58
|
+
const dir = getFilename(dirPath);
|
|
59
|
+
[packageJSON] = findFile(dirPath, join(dir, 'package.json'));
|
|
60
|
+
} while (!packageJSON && (dirPath = getParentDirectory(dirPath)));
|
|
61
|
+
|
|
62
|
+
return packageJSON;
|
|
63
|
+
}
|
package/lib/rename-files.js
CHANGED
|
@@ -1,73 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
getParentDirectory,
|
|
4
|
-
getFilename,
|
|
5
|
-
readFileContent,
|
|
6
|
-
findFile,
|
|
7
|
-
renameFile,
|
|
8
|
-
} from '@putout/operator-filesystem';
|
|
1
|
+
import * as renameFileWithFn from './rename-file-with-fn.js';
|
|
2
|
+
import * as renameFileByMask from './rename-file-by-mask.js';
|
|
9
3
|
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
mask,
|
|
18
|
-
rename,
|
|
19
|
-
}),
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const report = (file, {from, to}) => `Rename '${from}' to '${to}'`;
|
|
23
|
-
|
|
24
|
-
const fix = (file, {to}) => {
|
|
25
|
-
renameFile(file, to);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const scan = ({type, mask, rename}) => (path, {push, trackFile}) => {
|
|
29
|
-
for (const file of trackFile(path, mask)) {
|
|
30
|
-
if (type && !checkType(type, file))
|
|
31
|
-
continue;
|
|
4
|
+
export const renameFiles = ({type, mask, rename, from, to} = {}) => {
|
|
5
|
+
if (rename) {
|
|
6
|
+
const {
|
|
7
|
+
report,
|
|
8
|
+
fix,
|
|
9
|
+
createScan,
|
|
10
|
+
} = renameFileWithFn;
|
|
32
11
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
12
|
+
return {
|
|
13
|
+
report,
|
|
14
|
+
fix,
|
|
15
|
+
scan: createScan({
|
|
16
|
+
type,
|
|
17
|
+
mask,
|
|
18
|
+
rename,
|
|
19
|
+
}),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
report,
|
|
25
|
+
fix,
|
|
26
|
+
createScan,
|
|
27
|
+
} = renameFileByMask;
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
fix,
|
|
31
|
+
report,
|
|
32
|
+
scan: createScan({
|
|
33
|
+
mask,
|
|
37
34
|
from,
|
|
38
35
|
to,
|
|
39
|
-
})
|
|
40
|
-
}
|
|
36
|
+
}),
|
|
37
|
+
};
|
|
41
38
|
};
|
|
42
|
-
|
|
43
|
-
function checkType(type, file) {
|
|
44
|
-
const packagePath = findUpPackage(file);
|
|
45
|
-
|
|
46
|
-
if (type === 'commonjs' && !packagePath)
|
|
47
|
-
return true;
|
|
48
|
-
|
|
49
|
-
if (!packagePath)
|
|
50
|
-
return false;
|
|
51
|
-
|
|
52
|
-
const packageContent = readFileContent(packagePath);
|
|
53
|
-
|
|
54
|
-
if (!packageContent)
|
|
55
|
-
return false;
|
|
56
|
-
|
|
57
|
-
const info = parse(packageContent);
|
|
58
|
-
const infoType = info.type || 'commonjs';
|
|
59
|
-
|
|
60
|
-
return infoType === type;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function findUpPackage(file) {
|
|
64
|
-
let packageJSON;
|
|
65
|
-
let dirPath = getParentDirectory(file);
|
|
66
|
-
|
|
67
|
-
do {
|
|
68
|
-
const dir = getFilename(dirPath);
|
|
69
|
-
[packageJSON] = findFile(dirPath, join(dir, 'package.json'));
|
|
70
|
-
} while (!packageJSON && (dirPath = getParentDirectory(dirPath)));
|
|
71
|
-
|
|
72
|
-
return packageJSON;
|
|
73
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-rename-files",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.1",
|
|
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",
|