This commit improves the management of script execution process by enhancing the way terminal commands are handled, paving the way for easier future modifications and providing clearer feedback to users when scripts are cancelled. Previously, the UI displayed a generic error message which could lead to confusion if the user intentionally cancelled the script execution. Now, a specific error dialog will appear, improving the user experience by accurately reflecting the action taken by the user. This change affects code execution on Linux where closing GNOME terminal returns exit code `137` which is then treated by script cancellation by privacy.sexy to show the accurate error dialog. It does not affect macOS and Windows as curret commands result in success (`0`) exit code on cancellation. Additionally, this update encapsulates OS-specific logic into dedicated classes, promoting better separation of concerns and increasing the modularity of the codebase. This makes it simpler to maintain and extend the application. Key changes: - Display a specific error message for script cancellations. - Refactor command execution into dedicated classes. - Improve file permission setting flexibility and avoid setting file permissions on Windows as it's not required to execute files. - Introduce more granular error types for script execution. - Increase logging for shell commands to aid in debugging. - Expand test coverage to ensure reliability. - Fix error dialogs not showing the error messages due to incorrect propagation of errors. Other supported changes: - Update `SECURITY.md` with details on script readback and verification. - Fix a typo in `IpcRegistration.spec.ts`. - Document antivirus scans in `desktop-vs-web-features.md`.
72 lines
2.5 KiB
TypeScript
72 lines
2.5 KiB
TypeScript
import type { Logger } from '@/application/Common/Log/Logger';
|
|
import { ElectronLogger } from '@/infrastructure/Log/ElectronLogger';
|
|
import { OsSpecificTerminalLaunchCommandFactory } from './CommandDefinition/Factory/OsSpecificTerminalLaunchCommandFactory';
|
|
import { ExecutableFileShellCommandDefinitionRunner } from './CommandDefinition/Runner/ExecutableFileShellCommandDefinitionRunner';
|
|
import type { ScriptFileExecutionOutcome, ScriptFileExecutor } from './ScriptFileExecutor';
|
|
import type { CommandDefinitionFactory } from './CommandDefinition/Factory/CommandDefinitionFactory';
|
|
import type { CommandDefinitionRunner } from './CommandDefinition/Runner/CommandDefinitionRunner';
|
|
import type { CommandDefinition } from './CommandDefinition/CommandDefinition';
|
|
|
|
export class VisibleTerminalFileRunner implements ScriptFileExecutor {
|
|
constructor(
|
|
private readonly logger: Logger = ElectronLogger,
|
|
private readonly commandFactory: CommandDefinitionFactory
|
|
= new OsSpecificTerminalLaunchCommandFactory(),
|
|
private readonly commandRunner: CommandDefinitionRunner
|
|
= new ExecutableFileShellCommandDefinitionRunner(),
|
|
) { }
|
|
|
|
public async executeScriptFile(
|
|
filePath: string,
|
|
): Promise<ScriptFileExecutionOutcome> {
|
|
this.logger.info(`Executing script file: ${filePath}.`);
|
|
const outcome = await this.findAndExecuteCommand(filePath);
|
|
this.logOutcome(outcome);
|
|
return outcome;
|
|
}
|
|
|
|
private async findAndExecuteCommand(
|
|
filePath: string,
|
|
): Promise<ScriptFileExecutionOutcome> {
|
|
try {
|
|
let commandDefinition: CommandDefinition;
|
|
try {
|
|
commandDefinition = this.commandFactory.provideCommandDefinition();
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: {
|
|
type: 'UnsupportedPlatform',
|
|
message: `Error finding command: ${error.message}`,
|
|
},
|
|
};
|
|
}
|
|
const runOutcome = await this.commandRunner.runCommandDefinition(
|
|
commandDefinition,
|
|
filePath,
|
|
);
|
|
return runOutcome;
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error: {
|
|
type: 'FileExecutionError',
|
|
message: `Unexpected error: ${error.message}`,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
private logOutcome(outcome: ScriptFileExecutionOutcome) {
|
|
if (outcome.success) {
|
|
this.logger.info('Executed script file in terminal successfully.');
|
|
return;
|
|
}
|
|
this.logger.error(
|
|
'Failed to execute the script file in terminal.',
|
|
outcome.error.type,
|
|
outcome.error.message,
|
|
);
|
|
}
|
|
}
|