mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-04-27 15:50:57 -07:00
fix: directly execute file via filepath
This commit is contained in:
parent
cf13d38b78
commit
a8632dd05b
3 changed files with 20 additions and 28 deletions
|
|
@ -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') {
|
||||
|
|
|
|||
|
|
@ -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[]>;
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue