redlint 3.21.2 → 3.22.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/ChangeLog +11 -0
- package/lib/edit/README.md +1 -0
- package/lib/edit/edit.js +6 -1
- package/lib/edit/read-directory/get-filenames/index.js +15 -15
- package/lib/edit/rename-files/index.js +33 -7
- package/lib/edit/rename-files/rename-files-full-plugin/index.js +102 -0
- package/lib/test.test.js +15 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/lib/edit/README.md
CHANGED
|
@@ -2,3 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
- ✅[read-directory](https://putout.cloudcmd.io/#/gist/df334cfe8c45c273c4745f0d97f123c7/956fd7607dafc3431e7d531d33810286e5ecbdc5);
|
|
4
4
|
- ✅[rename-files](https://putout.cloudcmd.io/#/gist/eea361b01e553f5938faba49998225e3/1bbd9b4b61758a0c15d0331d8159eb0996e592cc);
|
|
5
|
+
- ✅[rename-files-full](https://putout.cloudcmd.io/#/gist/b0414b00022526750e94f0d5d0a5c1d3/867ea6341bc41efa581d44cecf638ee74da68793);
|
package/lib/edit/edit.js
CHANGED
|
@@ -36,12 +36,17 @@ export const edit = (filesystem, {dir, recursive, full}, overrides = {}) => {
|
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
const to = readFileContent(tmpFile, 'utf8');
|
|
39
|
-
|
|
39
|
+
|
|
40
|
+
const newNames = to
|
|
41
|
+
.split('\n')
|
|
42
|
+
.filter(Boolean);
|
|
40
43
|
|
|
41
44
|
removeTmpFile();
|
|
45
|
+
|
|
42
46
|
renameFiles(filesystem, {
|
|
43
47
|
dir,
|
|
44
48
|
from: names,
|
|
45
49
|
to: newNames,
|
|
50
|
+
full,
|
|
46
51
|
});
|
|
47
52
|
};
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
import {basename
|
|
1
|
+
import {basename} from 'node:path';
|
|
2
2
|
import {operator} from 'putout';
|
|
3
3
|
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
getFilename,
|
|
6
|
+
readDirectory,
|
|
7
|
+
findFile,
|
|
8
|
+
} = operator;
|
|
5
9
|
|
|
6
10
|
export const report = (path, {name}) => name;
|
|
7
11
|
export const fix = () => {};
|
|
8
|
-
export const scan = (
|
|
12
|
+
export const scan = (rootPath, {push, options}) => {
|
|
13
|
+
console.time('scan');
|
|
14
|
+
|
|
9
15
|
const {
|
|
10
16
|
dir = '/',
|
|
11
17
|
full,
|
|
@@ -13,28 +19,22 @@ export const scan = (path, {push, trackFile, options}) => {
|
|
|
13
19
|
} = options;
|
|
14
20
|
|
|
15
21
|
const names = [];
|
|
22
|
+
const [dirPath] = findFile(rootPath, dir);
|
|
23
|
+
|
|
24
|
+
const files = recursive ? findFile(dirPath, '*') : readDirectory(dirPath);
|
|
16
25
|
|
|
17
|
-
for (const file of
|
|
26
|
+
for (const file of files) {
|
|
18
27
|
const path = getFilename(file);
|
|
19
|
-
const currentDir = dirname(path);
|
|
20
28
|
|
|
21
29
|
if (dir === path)
|
|
22
30
|
continue;
|
|
23
31
|
|
|
24
|
-
if (!recursive && currentDir !== dir)
|
|
25
|
-
continue;
|
|
26
|
-
|
|
27
32
|
if (!full && recursive) {
|
|
28
33
|
const name = path
|
|
29
34
|
.replace(dir, '')
|
|
30
35
|
.replace(/^\//, '');
|
|
31
36
|
|
|
32
|
-
|
|
33
|
-
continue;
|
|
34
|
-
|
|
35
|
-
push(file, {
|
|
36
|
-
name,
|
|
37
|
-
});
|
|
37
|
+
names.push(name);
|
|
38
38
|
continue;
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -46,7 +46,7 @@ export const scan = (path, {push, trackFile, options}) => {
|
|
|
46
46
|
const sorted = names.sort();
|
|
47
47
|
|
|
48
48
|
for (const name of sorted) {
|
|
49
|
-
push(
|
|
49
|
+
push(dirPath, {
|
|
50
50
|
name,
|
|
51
51
|
});
|
|
52
52
|
}
|
|
@@ -8,8 +8,9 @@ import {
|
|
|
8
8
|
merge as originalMerge,
|
|
9
9
|
} from '@putout/processor-filesystem';
|
|
10
10
|
import * as renameFilesPlugin from './rename-files-plugin/index.js';
|
|
11
|
+
import * as renameFilesFullPlugin from './rename-files-full-plugin/index.js';
|
|
11
12
|
|
|
12
|
-
export const renameFiles = (filesystem, {dir, from, to}, overrides = {}) => {
|
|
13
|
+
export const renameFiles = (filesystem, {full, dir, from, to}, overrides = {}) => {
|
|
13
14
|
const {
|
|
14
15
|
branch = originalBranch,
|
|
15
16
|
merge = originalMerge,
|
|
@@ -18,10 +19,39 @@ export const renameFiles = (filesystem, {dir, from, to}, overrides = {}) => {
|
|
|
18
19
|
const [{source}] = branch(filesystem);
|
|
19
20
|
|
|
20
21
|
const ast = parse(source);
|
|
22
|
+
const options = getOptions({
|
|
23
|
+
dir,
|
|
24
|
+
from,
|
|
25
|
+
to,
|
|
26
|
+
full,
|
|
27
|
+
});
|
|
21
28
|
|
|
22
29
|
transform(ast, filesystem, {
|
|
30
|
+
...options,
|
|
23
31
|
fix: true,
|
|
24
32
|
fixCount: 1,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const code = print(ast);
|
|
36
|
+
|
|
37
|
+
return merge(filesystem, [code]);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function getOptions({full, from, to, dir}) {
|
|
41
|
+
if (full)
|
|
42
|
+
return {
|
|
43
|
+
rules: {
|
|
44
|
+
'rename-files-full': ['on', {
|
|
45
|
+
from,
|
|
46
|
+
to,
|
|
47
|
+
}],
|
|
48
|
+
},
|
|
49
|
+
plugins: [
|
|
50
|
+
['rename-files-full', renameFilesFullPlugin],
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return {
|
|
25
55
|
rules: {
|
|
26
56
|
'rename-files': ['on', {
|
|
27
57
|
dir,
|
|
@@ -32,9 +62,5 @@ export const renameFiles = (filesystem, {dir, from, to}, overrides = {}) => {
|
|
|
32
62
|
plugins: [
|
|
33
63
|
['rename-files', renameFilesPlugin],
|
|
34
64
|
],
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const code = print(ast);
|
|
38
|
-
|
|
39
|
-
return merge(filesystem, [code]);
|
|
40
|
-
};
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
join,
|
|
3
|
+
basename,
|
|
4
|
+
dirname,
|
|
5
|
+
} from 'node:path';
|
|
6
|
+
import {operator} from 'putout';
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
createNestedDirectory,
|
|
10
|
+
moveFile,
|
|
11
|
+
renameFile,
|
|
12
|
+
getFilename,
|
|
13
|
+
} = operator;
|
|
14
|
+
|
|
15
|
+
export const report = (filePath, {from, to}) => `Rename '${from}' to '${to}'`;
|
|
16
|
+
|
|
17
|
+
export const fix = (filePath, {to}) => {
|
|
18
|
+
const filename = getFilename(filePath);
|
|
19
|
+
const dirFrom = dirname(filename);
|
|
20
|
+
const dirTo = dirname(to);
|
|
21
|
+
const nameTo = basename(to);
|
|
22
|
+
const nameFrom = basename(filename);
|
|
23
|
+
|
|
24
|
+
if (nameFrom !== nameTo)
|
|
25
|
+
renameFile(filePath, nameTo);
|
|
26
|
+
|
|
27
|
+
if (dirFrom !== dirTo) {
|
|
28
|
+
const currentDirPath = createNestedDirectory(filePath, dirTo);
|
|
29
|
+
|
|
30
|
+
moveFile(filePath, currentDirPath);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const addFromTo = (namesFrom, namesTo) => (a, i) => [
|
|
35
|
+
namesFrom[i],
|
|
36
|
+
namesTo[i],
|
|
37
|
+
a,
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
export const scan = (path, {push, trackFile, options}) => {
|
|
41
|
+
const {
|
|
42
|
+
from = [],
|
|
43
|
+
to = [],
|
|
44
|
+
dir = '/',
|
|
45
|
+
} = options;
|
|
46
|
+
|
|
47
|
+
if (from.length !== to.length)
|
|
48
|
+
return;
|
|
49
|
+
|
|
50
|
+
if (isEqual(from, to))
|
|
51
|
+
return;
|
|
52
|
+
|
|
53
|
+
const [fullNames, fromNames, toNames] = getRenamedFiles(dir, from, to);
|
|
54
|
+
const convertToTuple = addFromTo(fromNames, toNames);
|
|
55
|
+
const trackFileIterator = trackFile(path, fullNames).map(convertToTuple);
|
|
56
|
+
|
|
57
|
+
for (const [from, to, currentFile] of trackFileIterator) {
|
|
58
|
+
push(currentFile, {
|
|
59
|
+
from,
|
|
60
|
+
to,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
function getRenamedFiles(dir, a, b) {
|
|
66
|
+
const from = [];
|
|
67
|
+
const to = [];
|
|
68
|
+
const full = [];
|
|
69
|
+
const n = a.length;
|
|
70
|
+
let i = -1;
|
|
71
|
+
|
|
72
|
+
while (++i < n) {
|
|
73
|
+
if (a[i] !== b[i]) {
|
|
74
|
+
const currentFrom = a[i];
|
|
75
|
+
const currentTo = b[i];
|
|
76
|
+
|
|
77
|
+
full.push(join(
|
|
78
|
+
dir,
|
|
79
|
+
currentFrom,
|
|
80
|
+
));
|
|
81
|
+
from.push(currentFrom);
|
|
82
|
+
to.push(currentTo);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return [
|
|
87
|
+
full,
|
|
88
|
+
from,
|
|
89
|
+
to,
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isEqual(a, b) {
|
|
94
|
+
let i = a.length;
|
|
95
|
+
|
|
96
|
+
while (--i >= 0) {
|
|
97
|
+
if (a[i] !== b[i])
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return true;
|
|
102
|
+
}
|
package/lib/test.test.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__filesystem,
|
|
3
|
+
fromJS,
|
|
4
|
+
toJS,
|
|
5
|
+
} from '@putout/operator-json';
|
|
6
|
+
|
|
7
|
+
export const branch = (raw) => {
|
|
8
|
+
const source = toJS(raw, __filesystem);
|
|
9
|
+
|
|
10
|
+
return [{
|
|
11
|
+
source,
|
|
12
|
+
}];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const merge = (raw, [source]) => fromJS(source, __filesystem);
|