mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-18 01:00:25 +00:00
coding-agent: fix macOS screenshot filenames with unicode spaces (#181)
This commit is contained in:
parent
5c0a84b2d8
commit
9a7bbb2839
9 changed files with 70 additions and 108 deletions
48
packages/coding-agent/src/core/tools/path-utils.ts
Normal file
48
packages/coding-agent/src/core/tools/path-utils.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { accessSync, constants } from "node:fs";
|
||||
import * as os from "node:os";
|
||||
|
||||
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
||||
const NARROW_NO_BREAK_SPACE = "\u202F";
|
||||
|
||||
function normalizeUnicodeSpaces(str: string): string {
|
||||
return str.replace(UNICODE_SPACES, " ");
|
||||
}
|
||||
|
||||
function tryMacOSScreenshotPath(filePath: string): string {
|
||||
return filePath.replace(/ (AM|PM)\./g, `${NARROW_NO_BREAK_SPACE}$1.`);
|
||||
}
|
||||
|
||||
function fileExists(filePath: string): boolean {
|
||||
try {
|
||||
accessSync(filePath, constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function expandPath(filePath: string): string {
|
||||
const normalized = normalizeUnicodeSpaces(filePath);
|
||||
if (normalized === "~") {
|
||||
return os.homedir();
|
||||
}
|
||||
if (normalized.startsWith("~/")) {
|
||||
return os.homedir() + normalized.slice(1);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function resolveReadPath(filePath: string): string {
|
||||
const expanded = expandPath(filePath);
|
||||
|
||||
if (fileExists(expanded)) {
|
||||
return expanded;
|
||||
}
|
||||
|
||||
const macOSVariant = tryMacOSScreenshotPath(expanded);
|
||||
if (macOSVariant !== expanded && fileExists(macOSVariant)) {
|
||||
return macOSVariant;
|
||||
}
|
||||
|
||||
return expanded;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue