fix: directly execute file via filepath

This commit is contained in:
林一二 2021-09-17 02:25:18 +08:00
parent cf13d38b78
commit a8632dd05b
3 changed files with 20 additions and 28 deletions

View file

@ -33,12 +33,12 @@ export class NativeService implements INativeService {
}
}
public executeZxScript$(zxWorkerArguments: { fileContent: string; fileName: string }): Observable<string> {
public executeZxScript$(filePath: string): Observable<string> {
if (this.zxWorker === undefined) {
return of('this.zxWorker not initialized');
}
logger.info('zxWorker execute', { zxWorkerArguments, ZX_FOLDER });
const observable = this.zxWorker.executeZxScript(zxWorkerArguments, ZX_FOLDER);
logger.info('zxWorker execute', { filePath, ZX_FOLDER });
const observable = this.zxWorker.executeZxScript(filePath, ZX_FOLDER);
return new Observable((observer) => {
observable.subscribe((message) => {
if (message.type === 'control') {

View file

@ -9,7 +9,7 @@ import { WindowNames } from '@services/windows/WindowProperties';
* Wrap call to electron api, so we won't need remote module in renderer process
*/
export interface INativeService {
executeZxScript$(zxWorkerArguments: { fileContent: string; fileName: string }): Observable<string>;
executeZxScript$(filePath: string): Observable<string>;
open(uri: string, isDirectory?: boolean): Promise<void>;
pickDirectory(defaultPath?: string): Promise<string[]>;
pickFile(filters?: Electron.OpenDialogOptions['filters']): Promise<string[]>;

View file

@ -1,14 +1,11 @@
import 'source-map-support/register';
import { expose } from 'threads/worker';
import { fork } from 'child_process';
import { tmpdir } from 'os';
import { mkdtemp, writeFile } from 'fs-extra';
import path from 'path';
import { Observable } from 'rxjs';
import intercept from 'intercept-stdout';
import { IZxWorkerMessage, ZxWorkerControlActions } from './interface';
function executeZxScript({ fileContent, fileName }: { fileContent: string; fileName: string }, zxPath: string): Observable<IZxWorkerMessage> {
function executeZxScript(filePath: string, zxPath: string): Observable<IZxWorkerMessage> {
return new Observable<IZxWorkerMessage>((observer) => {
observer.next({ type: 'control', actions: ZxWorkerControlActions.start });
intercept(
@ -20,27 +17,22 @@ function executeZxScript({ fileContent, fileName }: { fileContent: string; fileN
},
);
void (async function executeZxScriptIIFE() {
try {
const temporaryDirectory = await mkdtemp(`${tmpdir()}${path.sep}`);
const temporaryScriptFile = path.join(temporaryDirectory, fileName);
await writeFile(temporaryScriptFile, fileContent);
const execution = fork(zxPath, [temporaryScriptFile], { silent: true });
try {
const execution = fork(zxPath, [filePath], { silent: true });
execution.on('close', function (code) {
observer.next({ type: 'control', actions: ZxWorkerControlActions.ended, message: `child process exited with code ${String(code)}` });
});
execution.stdout?.on('data', (stdout: Buffer) => {
observer.next({ type: 'stdout', message: String(stdout) });
});
execution.stderr?.on('data', (stdout: Buffer) => {
observer.next({ type: 'stderr', message: String(stdout) });
});
} catch (error) {
const message = `zx script's executeZxScriptIIFE() failed with error ${(error as Error).message} ${(error as Error).stack ?? ''}`;
observer.next({ type: 'control', actions: ZxWorkerControlActions.error, message });
}
})();
execution.on('close', function (code) {
observer.next({ type: 'control', actions: ZxWorkerControlActions.ended, message: `child process exited with code ${String(code)}` });
});
execution.stdout?.on('data', (stdout: Buffer) => {
observer.next({ type: 'stdout', message: String(stdout) });
});
execution.stderr?.on('data', (stdout: Buffer) => {
observer.next({ type: 'stderr', message: String(stdout) });
});
} catch (error) {
const message = `zx script's executeZxScriptIIFE() failed with error ${(error as Error).message} ${(error as Error).stack ?? ''}`;
observer.next({ type: 'control', actions: ZxWorkerControlActions.error, message });
}
});
}