How to use zx as a library/dependency in Electron ? #227
-
I hope I can integrate But seems I can only access zx via shell, and only accept filepath as input. Can I just |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I have made a simple prototype that prove zx can be First you will need https://github.com/sindresorhus/fix-path to use shell in a GUI app. And for electron, use sindresorhus/fix-path#15 (comment) The core concept is: import { mkdtemp, writeFile } from 'fs-extra';
import { fork } from 'child_process';
import path from 'path';
import { tmpdir } from 'os';
const zxPath = '/Users/TiddlyGit-Desktop/node_modules/zx/zx.mjs';
const fileName = 'aaa.md';
const fileContent = `
# aaa
\`\`\`sh
pwd
ls
\`\`\`
\`\`\`js
const result = await fetch(encodeURI('https://suggest.taobao.com/sug?code=utf-8&q=字节')).then(res => res.json()).then(res => res.result);
console.log(\`!! \${result[0][0]}\`);
console.log(\`\${result[0][1]}\`);
\`\`\`
`;
(async function () {
const temporaryDirectory = await mkdtemp(`${tmpdir()}${path.sep}`);
const temporaryScriptFile = path.join(temporaryDirectory, fileName);
await writeFile(temporaryScriptFile, fileContent);
// DEBUG: console
console.log(`temporaryScriptFile`, temporaryScriptFile);
const execution = fork(zxPath, [temporaryScriptFile], { silent: true });
execution.on('close', function (code) {
console.log({ type: 'control', actions: 'ended', message: `child process exited with code ${String(code)}` });
});
execution.stdout?.on('data', (stdout: Buffer) => {
console.log({ type: 'stdout', message: String(stdout) });
});
execution.stderr?.on('data', (stdout: Buffer) => {
console.log({ type: 'stderr', message: String(stdout) });
});
})(); This works in dev mode, but may need some tweaks to work in a bundled electron app. For bundled app , use #229 Example: https://github.com/tiddly-gittly/TiddlyGit-Desktop/releases/tag/v0.4.5 2021-09-17.03.15.06.mov |
Beta Was this translation helpful? Give feedback.
I have made a simple prototype that prove zx can be
fork
to execute.mjs
file in a tmp folder: https://github.com/tiddly-gittly/TiddlyGit-Desktop/blob/0c6b26c0c1113e0c66d6f49f022c5733d4fa85e8/src/services/native/zxWorker.tsFirst you will need https://github.com/sindresorhus/fix-path to use shell in a GUI app. And for electron, use sindresorhus/fix-path#15 (comment)
The core concept is: