Skip to content

Commit 419b652

Browse files
committed
feature: @putout/plugin-filesystem: rename-test-to-spec: simplify
1 parent f34de24 commit 419b652

File tree

3 files changed

+19
-43
lines changed

3 files changed

+19
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
const {
5-
renameFile,
6-
findFile,
7-
getFilename,
8-
__filesystem,
9-
} = operator;
4+
const {renameFile, getFilename} = operator;
105

11-
const report = ({mask, from, to}) => {
6+
const report = (path, {mask, from, to}) => {
127
if (!mask)
138
return `Rename '${from}' to '${to}'`;
149

@@ -19,41 +14,34 @@ module.exports.renameFileByMask = ({mask, from, to} = {}) => {
1914
return {
2015
fix,
2116
report,
22-
traverse: traverse({
17+
scan: createScan({
2318
mask,
2419
from,
2520
to,
2621
}),
2722
};
2823
};
2924

30-
function fix({path, from, to}) {
25+
function fix(path, {from, to}) {
3126
const filename = getFilename(path);
3227
const newFilename = filename.replace(from, to);
3328

3429
renameFile(path, newFilename);
3530
}
3631

37-
const traverse = (baseOptions) => ({push, options}) => {
32+
const createScan = (baseOptions) => (rootPath, {push, options, trackFile}) => {
3833
const from = options.from || baseOptions.from;
3934
const to = options.to || baseOptions.to;
4035
const mask = options.mask || baseOptions.mask;
4136

4237
if (!from || !to)
4338
return {};
4439

45-
return {
46-
[__filesystem](path) {
47-
const files = findFile(path, mask || from);
48-
49-
for (const file of files) {
50-
push({
51-
path: file,
52-
from,
53-
to,
54-
mask,
55-
});
56-
}
57-
},
58-
};
40+
for (const file of trackFile(rootPath, mask || from)) {
41+
push(file, {
42+
from,
43+
to,
44+
mask,
45+
});
46+
}
5947
};
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
'use strict';
22

3-
const {operator} = require('putout');
4-
const {
5-
findFile,
6-
renameFile,
7-
getFilename,
8-
} = operator;
3+
const {renameFileByMask} = require('../rename-file-by-mask');
94

10-
module.exports.report = () => `Rename '*.test.*' to '.spec.*'`;
11-
12-
module.exports.fix = (path) => {
13-
const filename = getFilename(path);
14-
const newFilename = filename.replace('.test.', '.spec.');
15-
16-
renameFile(path, newFilename);
17-
};
18-
19-
module.exports.scan = (path, {push}) => {
20-
findFile(path, '*.test.*').map(push);
21-
};
5+
module.exports = renameFileByMask({
6+
mask: '*.test.*',
7+
from: 'test',
8+
to: 'spec',
9+
});

packages/plugin-filesystem/lib/rename-test-to-spec/index.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const test = createTest(__dirname, {
1010
});
1111

1212
test('packages: rename-test-to-spec: report', (t) => {
13-
t.report('rename-test-to-spec', `Rename '*.test.*' to '.spec.*'`);
13+
t.report('rename-test-to-spec', `Rename '*.test.*' to '*.spec.*'`);
1414
t.end();
1515
});
1616

0 commit comments

Comments
 (0)