@ww_nero/mini-cli 1.0.89 → 1.0.90
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/package.json +1 -1
- package/src/tools/edit.js +2 -24
- package/src/utils/helpers.js +25 -0
- package/src/utils/renderer.js +3 -12
package/package.json
CHANGED
package/src/tools/edit.js
CHANGED
|
@@ -1,35 +1,13 @@
|
|
|
1
|
-
const { diffLines } = require('diff');
|
|
2
1
|
const {
|
|
3
2
|
resolveWorkspacePath,
|
|
4
3
|
readTextFile,
|
|
5
4
|
writeTextFile,
|
|
6
5
|
normalizeLineBreaks,
|
|
7
6
|
isCodeIdentical,
|
|
8
|
-
processContent
|
|
7
|
+
processContent,
|
|
8
|
+
computeDiffStats
|
|
9
9
|
} = require('../utils/helpers');
|
|
10
10
|
|
|
11
|
-
const countLines = (text = '') => {
|
|
12
|
-
if (!text) return 0;
|
|
13
|
-
const normalized = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
14
|
-
const lines = normalized.split('\n');
|
|
15
|
-
if (lines[lines.length - 1] === '') {
|
|
16
|
-
lines.pop();
|
|
17
|
-
}
|
|
18
|
-
return lines.length;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const computeDiffStats = (before = '', after = '') => {
|
|
22
|
-
const parts = diffLines(before, after);
|
|
23
|
-
return parts.reduce((acc, part) => {
|
|
24
|
-
if (part.added) {
|
|
25
|
-
acc.added += countLines(part.value);
|
|
26
|
-
} else if (part.removed) {
|
|
27
|
-
acc.removed += countLines(part.value);
|
|
28
|
-
}
|
|
29
|
-
return acc;
|
|
30
|
-
}, { added: 0, removed: 0 });
|
|
31
|
-
};
|
|
32
|
-
|
|
33
11
|
const editFile = async ({ filePath, search, replace } = {}, context = {}) => {
|
|
34
12
|
if (!filePath || typeof filePath !== 'string') {
|
|
35
13
|
return 'filePath 参数不能为空';
|
package/src/utils/helpers.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { diffLines } = require('diff');
|
|
1
2
|
const fsPromises = require('fs/promises');
|
|
2
3
|
const path = require('path');
|
|
3
4
|
|
|
@@ -7,6 +8,28 @@ const normalizeLineBreaks = (text = '') => {
|
|
|
7
8
|
return /^\n*$/.test(normalized) ? '' : normalized;
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
const countLines = (text = '') => {
|
|
12
|
+
if (!text) return 0;
|
|
13
|
+
const normalized = String(text).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
14
|
+
const lines = normalized.split('\n');
|
|
15
|
+
if (lines[lines.length - 1] === '') {
|
|
16
|
+
lines.pop();
|
|
17
|
+
}
|
|
18
|
+
return lines.length;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const computeDiffStats = (before = '', after = '') => {
|
|
22
|
+
const parts = diffLines(String(before || ''), String(after || ''));
|
|
23
|
+
return parts.reduce((acc, part) => {
|
|
24
|
+
if (part.added) {
|
|
25
|
+
acc.added += countLines(part.value);
|
|
26
|
+
} else if (part.removed) {
|
|
27
|
+
acc.removed += countLines(part.value);
|
|
28
|
+
}
|
|
29
|
+
return acc;
|
|
30
|
+
}, { added: 0, removed: 0 });
|
|
31
|
+
};
|
|
32
|
+
|
|
10
33
|
const resolveWorkspacePath = (workspaceRoot, targetPath = '.', options = {}) => {
|
|
11
34
|
if (!workspaceRoot) {
|
|
12
35
|
throw new Error('未找到工作区目录');
|
|
@@ -83,6 +106,8 @@ const safeJsonParse = (text) => {
|
|
|
83
106
|
};
|
|
84
107
|
|
|
85
108
|
module.exports = {
|
|
109
|
+
countLines,
|
|
110
|
+
computeDiffStats,
|
|
86
111
|
normalizeLineBreaks,
|
|
87
112
|
resolveWorkspacePath,
|
|
88
113
|
readTextFile,
|
package/src/utils/renderer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
|
+
const { countLines, computeDiffStats } = require('./helpers');
|
|
2
3
|
|
|
3
4
|
const truncateText = (text, maxLength = 200) => {
|
|
4
5
|
if (!text || typeof text !== 'string') {
|
|
@@ -10,15 +11,6 @@ const truncateText = (text, maxLength = 200) => {
|
|
|
10
11
|
return text.substring(0, maxLength) + '...';
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
const countLines = (text = '') => {
|
|
14
|
-
if (!text) return 0;
|
|
15
|
-
const normalized = text.replace(/\r/g, '');
|
|
16
|
-
const newlineMatches = normalized.match(/\n/g);
|
|
17
|
-
const newlineCount = Array.isArray(newlineMatches) ? newlineMatches.length : 0;
|
|
18
|
-
const endsWithNewline = normalized.endsWith('\n');
|
|
19
|
-
return endsWithNewline ? newlineCount : newlineCount + 1;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
14
|
const formatWriteHeader = (args = {}) => {
|
|
23
15
|
const filePath = args.filePath || '';
|
|
24
16
|
const lineCount = countLines(args.content);
|
|
@@ -27,9 +19,8 @@ const formatWriteHeader = (args = {}) => {
|
|
|
27
19
|
|
|
28
20
|
const formatEditHeader = (args = {}) => {
|
|
29
21
|
const filePath = args.filePath || '';
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
return ` (${filePath}, ${chalk.green(`+${replaceLines}`)} ${chalk.red(`-${searchLines}`)})`;
|
|
22
|
+
const { added, removed } = computeDiffStats(args.search, args.replace);
|
|
23
|
+
return ` (${filePath}, ${chalk.green(`+${added}`)} ${chalk.red(`-${removed}`)})`;
|
|
33
24
|
};
|
|
34
25
|
|
|
35
26
|
const formatHeader = (name, args, options = {}) => {
|