This commit improves the handling of paths with spaces or special characters during script execution in the desktop application. Key improvements: - Paths are now quoted for macOS/Linux, addressing issues with whitespace or single quotes. - Windows paths are enclosed in double quotes to handle special characters. Other supporting changes: - Add more documentation for terminal execution commands. - Refactor terminal script file execution into a dedicated file for improved separation of concerns. - Refactor naming of `RuntimeEnvironment` to align with naming conventions (no interface with I prefix) and for clarity. - Refactor `TemporaryFileCodeRunner` to simplify it by removing the `os` parameter and handling OS-specific logic within the filename generator instead. - Refactor `fileName` to `filename` for consistency.
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { chmod, mkdir, writeFile } from 'node:fs/promises';
|
|
import { exec } from 'node:child_process';
|
|
import { SystemOperations } from './SystemOperations';
|
|
|
|
export function createNodeSystemOperations(): SystemOperations {
|
|
return {
|
|
operatingSystem: {
|
|
getTempDirectory: () => tmpdir(),
|
|
},
|
|
location: {
|
|
combinePaths: (...pathSegments) => join(...pathSegments),
|
|
},
|
|
fileSystem: {
|
|
setFilePermissions: (
|
|
filePath: string,
|
|
mode: string | number,
|
|
) => chmod(filePath, mode),
|
|
createDirectory: async (
|
|
directoryPath: string,
|
|
isRecursive?: boolean,
|
|
) => {
|
|
await mkdir(directoryPath, { recursive: isRecursive });
|
|
// Ignoring the return value from `mkdir`, which is the first directory created
|
|
// when `recursive` is true. The function contract is to not return any value,
|
|
// and we avoid handling this inconsistent behavior.
|
|
// See https://github.com/nodejs/node/pull/31530
|
|
},
|
|
writeToFile: (
|
|
filePath: string,
|
|
data: string,
|
|
) => writeFile(filePath, data),
|
|
},
|
|
command: {
|
|
exec: (command) => new Promise((resolve, reject) => {
|
|
exec(command, (error) => {
|
|
if (error) {
|
|
reject(error);
|
|
}
|
|
resolve();
|
|
});
|
|
}),
|
|
},
|
|
};
|
|
}
|