refactor extra code, duplicates, complexity

- refactor array equality check and add tests
- remove OperatingSystem.Unknown causing extra logic, return undefined instead
- refactor enum validation to share same logic
- refactor scripting language factories to share same logic
- refactor too many args in runCodeAsync
- refactor ScriptCode constructor to reduce complexity
- fix writing useless write to member object since another property write always override it
This commit is contained in:
undergroundwires
2021-04-11 14:37:02 +01:00
parent 3e9c99f5f8
commit 00d8e551db
37 changed files with 512 additions and 233 deletions

View File

@@ -5,16 +5,20 @@ import fs from 'fs';
import child_process from 'child_process';
import { OperatingSystem } from '@/domain/OperatingSystem';
export async function runCodeAsync(
code: string, folderName: string, fileExtension: string,
node = getNodeJs(), environment = Environment.CurrentEnvironment): Promise<void> {
const dir = node.path.join(node.os.tmpdir(), folderName);
await node.fs.promises.mkdir(dir, {recursive: true});
const filePath = node.path.join(dir, `run.${fileExtension}`);
await node.fs.promises.writeFile(filePath, code);
await node.fs.promises.chmod(filePath, '755');
const command = getExecuteCommand(filePath, environment);
node.child_process.exec(command);
export class CodeRunner {
constructor(
private readonly node = getNodeJs(),
private readonly environment = Environment.CurrentEnvironment) {
}
public async runCodeAsync(code: string, folderName: string, fileExtension: string): Promise<void> {
const dir = this.node.path.join(this.node.os.tmpdir(), folderName);
await this.node.fs.promises.mkdir(dir, {recursive: true});
const filePath = this.node.path.join(dir, `run.${fileExtension}`);
await this.node.fs.promises.writeFile(filePath, code);
await this.node.fs.promises.chmod(filePath, '755');
const command = getExecuteCommand(filePath, this.environment);
this.node.child_process.exec(command);
}
}
function getExecuteCommand(scriptPath: string, environment: Environment): string {