forked from lessweb/deepcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-utils.ts
More file actions
143 lines (116 loc) · 3.82 KB
/
file-utils.ts
File metadata and controls
143 lines (116 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as fs from "fs";
import * as path from "path";
import type { FileState, FileLineEnding } from "./state";
export type FileReadMetadata = {
content: string;
encoding: BufferEncoding;
lineEndings: FileLineEnding;
timestamp: number;
};
export function normalizeContent(value: string): string {
return value.replace(/\r\n/g, "\n");
}
export function detectLineEndings(value: string): FileLineEnding {
return value.includes("\r\n") ? "CRLF" : "LF";
}
export function detectEncoding(buffer: Buffer): BufferEncoding {
if (buffer.length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
return "utf16le";
}
return "utf8";
}
export function readTextFileWithMetadata(filePath: string): FileReadMetadata {
const buffer = fs.readFileSync(filePath);
const stat = fs.statSync(filePath);
const encoding = detectEncoding(buffer);
const raw = buffer.toString(encoding);
return {
content: normalizeContent(raw),
encoding,
lineEndings: detectLineEndings(raw),
timestamp: Math.floor(stat.mtimeMs),
};
}
export function writeTextFile(
filePath: string,
content: string,
encoding: BufferEncoding,
lineEndings: FileLineEnding
): number {
const normalized = normalizeContent(content);
const toWrite = lineEndings === "CRLF" ? normalized.replace(/\n/g, "\r\n") : normalized;
fs.writeFileSync(filePath, toWrite, { encoding });
return Buffer.byteLength(toWrite, encoding === "utf16le" ? "utf16le" : "utf8");
}
export function ensureParentDirectory(filePath: string): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
export function hasFileChangedSinceState(filePath: string, state: FileState): boolean {
const current = readTextFileWithMetadata(filePath);
if (current.timestamp <= state.timestamp) {
return false;
}
const isFullRead = !state.isPartialView && typeof state.offset === "undefined" && typeof state.limit === "undefined";
return !(isFullRead && current.content === state.content);
}
export function buildDiffPreview(
filePath: string,
originalContent: string | null,
updatedContent: string,
maxLines = 40
): string | null {
const original = originalContent === null ? null : normalizeContent(originalContent);
const updated = normalizeContent(updatedContent);
if (original !== null && original === updated) {
return null;
}
const oldLines = toDiffLines(original);
const newLines = toDiffLines(updated);
let prefix = 0;
while (prefix < oldLines.length && prefix < newLines.length && oldLines[prefix] === newLines[prefix]) {
prefix += 1;
}
let suffix = 0;
while (
suffix < oldLines.length - prefix &&
suffix < newLines.length - prefix &&
oldLines[oldLines.length - 1 - suffix] === newLines[newLines.length - 1 - suffix]
) {
suffix += 1;
}
const oldChanged = oldLines.slice(prefix, oldLines.length - suffix);
const newChanged = newLines.slice(prefix, newLines.length - suffix);
const oldStart = original === null ? 0 : prefix + 1;
const newStart = prefix + 1;
const previewLines = [
`--- ${original === null ? "/dev/null" : `a/${filePath}`}`,
`+++ b/${filePath}`,
`@@ -${oldStart},${oldChanged.length} +${newStart},${newChanged.length} @@`,
];
if (prefix > 0) {
previewLines.push(` ${oldLines[prefix - 1]}`);
}
for (const line of oldChanged) {
previewLines.push(`-${line}`);
}
for (const line of newChanged) {
previewLines.push(`+${line}`);
}
if (suffix > 0) {
previewLines.push(` ${oldLines[oldLines.length - suffix]}`);
}
if (previewLines.length > maxLines) {
return `${previewLines.slice(0, maxLines).join("\n")}\n...`;
}
return previewLines.join("\n");
}
function toDiffLines(content: string | null): string[] {
if (!content) {
return [];
}
const lines = content.split("\n");
if (lines[lines.length - 1] === "") {
lines.pop();
}
return lines;
}