Refactor text utilities and expand their usage

This commit refactors existing text utility functions into the
application layer for broad reuse and integrates them across
the codebase. Initially, these utilities were confined to test
code, which limited their application.

Changes:

- Move text utilities to the application layer.
- Centralize text utilities into dedicated files for better
  maintainability.
- Improve robustness of utility functions with added type checks.
- Replace duplicated logic with centralized utility functions
  throughout the codebase.
- Expand unit tests to cover refactored code parts.
This commit is contained in:
undergroundwires
2024-07-18 20:49:21 +02:00
parent 8d7a7eb434
commit 851917e049
45 changed files with 563 additions and 117 deletions

View File

@@ -1,4 +1,5 @@
import { indentText, splitTextIntoLines } from '@tests/shared/Text';
import { indentText } from '@/application/Common/Text/IndentText';
import { splitTextIntoLines } from '@/application/Common/Text/SplitTextIntoLines';
import { log, die } from '../utils/log';
import { readAppLogFile } from './app-logs';
import { STDERR_IGNORE_PATTERNS } from './error-ignore-patterns';
@@ -172,5 +173,5 @@ function describeError(
function getNonEmptyLines(text: string) {
return splitTextIntoLines(text)
.filter((line) => line?.trim().length > 0);
.filter((line) => line.trim().length > 0);
}

View File

@@ -1,4 +1,5 @@
import { filterEmpty } from '@tests/shared/Text';
import { splitTextIntoLines } from '@/application/Common/Text/SplitTextIntoLines';
import { filterEmptyStrings } from '@/application/Common/Text/FilterEmptyStrings';
import { runCommand } from '../../utils/run-command';
import { log, LogLevel } from '../../utils/log';
import { SupportedPlatform, CURRENT_PLATFORM } from '../../utils/platform';
@@ -56,7 +57,7 @@ async function captureTitlesOnLinux(processId: number): Promise<string[]> {
return [];
}
const windowIds = windowIdsOutput.trim().split('\n');
const windowIds = splitTextIntoLines(windowIdsOutput.trim());
const titles = await Promise.all(windowIds.map(async (windowId) => {
const { stdout: titleOutput, error: titleError } = await runCommand(
@@ -68,7 +69,7 @@ async function captureTitlesOnLinux(processId: number): Promise<string[]> {
return titleOutput.trim();
}));
return filterEmpty(titles);
return filterEmptyStrings(titles);
}
let hasAssistiveAccessOnMac = true;
@@ -78,7 +79,7 @@ async function captureTitlesOnMac(processId: number): Promise<string[]> {
if (!hasAssistiveAccessOnMac) {
return [];
}
const script = `
const command = constructAppleScriptCommand(`
tell application "System Events"
try
set targetProcess to first process whose unix id is ${processId}
@@ -93,13 +94,8 @@ async function captureTitlesOnMac(processId: number): Promise<string[]> {
return allWindowNames
end tell
end tell
`;
const argument = script.trim()
.split(/[\r\n]+/)
.map((line) => `-e '${line.trim()}'`)
.join(' ');
const { stdout: titleOutput, error } = await runCommand(`osascript ${argument}`);
`);
const { stdout: titleOutput, error } = await runCommand(command);
if (error) {
let errorMessage = '';
if (error.includes('-25211')) {
@@ -116,3 +112,13 @@ async function captureTitlesOnMac(processId: number): Promise<string[]> {
}
return [title];
}
function constructAppleScriptCommand(appleScriptCode: string): string {
const scriptLines = splitTextIntoLines(appleScriptCode.trim());
const trimmedLines = scriptLines.map((line) => line.trim());
const nonEmptyLines = filterEmptyStrings(trimmedLines);
const formattedArguments = nonEmptyLines
.map((line) => `-e '${line.trim()}'`)
.join(' ');
return `osascript ${formattedArguments}`;
}