I’m having problems using pdfkit in Electron due to “fs.readFileSync is not a function” … how can i use fs library in Electron? I’ve read that is disabled for security reasons, but i would need it to be executed offline.
In Electron Framework as we know we have two type of process .
1.) Main Process
2.)Render Process
so main.js file which you passed to command electron main.js (Note:- while you are calling it with command line arguments ) so at this time electron will create 2 process . one is main process in which your main.js run and from that process it will launch your index.html file and scripts which you include in it in another render process.
so in that main.js file use the fs module like we do in the node.js using
const fs = require('fs');
fs.readFileSync()
and the data you get it here pass to the render process using interprocess communication provided by the Electron Framework using its APIs IPCRender IPCMain.
Related
I am using webpack-dev-server to build and serve an electron app. I am using the multi-target support so it builds and serves both the Renderer and Main files. These are available at http://localhost:8080/, in my case the Main entry point is http://localhost:8080/background.js, and the Renderer index.html is http://localhost:8080/ which loads http://localhost:8080/js/main.js.
webpack-dev-server only builds the files in memory - they are never written to disk. However Electron only seems to run its Main process from files on disk. I.e. if I run electron path/to/background.js it works fine, but that file doesn't exist when using webpack-dev-server. If I run electron http://localhost:8080/ it just loads the Renderer index.html and never runs background.js.
Basically I want to do
electron http://localhost:8080/background.js
and have it download and run that file as the Main process. That command doesn't work (it just opens the background.js file as text. Is there any way to achieve this short of doing it manually with curl or whatever?
I settled on a workaround - you can make webpack-dev-server serve its output from memory and write the files to disk. I added this to the webpack config for my Main process:
devServer: {
writeToDisk: true
}
Note that due to a bug you need to add it to your Renderer config too, even though you don't need those files, otherwise nothing gets written.
I'm doing long-running computations in an electron app. I'm "forking" the node process using child_process.fork to execute a script in a separate process not to block the renderer process.
The app is working fine provided that the script I'm launching with child_process.fork is in the directory I'm launching it from. What I'd like to do instead is to ship the script inside the binary (I'm using electron-builder to build one).
I've found out that the builder packs everything (including the script I'm interested in) into an .asar archive - can it be accessed by the binary at runtime?
child_process.spawn and using a language other than JS is an option too, but the problem persists - I don't know how to embed the script into the binary.
I solved the problem thanks to this response from another question.
I've added
node: {
__dirname: true,
},
to webpack.config.js and used process.resourcesPath to resolve the file path.
child_process.fork can utilize the asar library, if given a script path inside an asar archive. To achieve this I used roughly
import path from 'path';
import { fork } from 'child_process';
const scriptPath = path.join(process.resourcesPath!, 'app.asar', fileName);
const args = [];
const process = fork(scriptPath, args);
child.execFile on the other hand can utilize the asar library to spawn a process of the binary contained in an asar archive. Note that it can only resolve the binary, not its parameters, so if you wanted to embed say a Python interpreter and a Python script in your electron app, you should package it in a single binary or extract the script to the file system and execute using the embedded Python binary or load its content to a variable and execute directly using python -c "print(44)" with your script content as a -c argument parameter.
I used Electron Packager to build my application which has some initial data in a NeDB. But then the packager generates the db file alright without the data after I ran the .exe file that was created.
I have a simple dart project that has one executable in bin folder (test.dart). I have activated it with dart global activate and now I can run it directly with just typing the name of that executable file.
Inside that dart file I would like to know the path of that script. Basically for now I'm just printing something like this:
print('1: ' + Platform.script.toString());
print('2: ' + Platform.script.path);
print('3: ' + Platform.executable);
print('4: ' + Platform.packageRoot);
print('5: ' + Platform.resolvedExecutable);
When I run it directly:
test
or with pub:
pub global run test
or even with package name:
pub global run test:test
I always get the same result:
1: http://localhost:53783/test.dart
2: /test.dart
3: E:\apps\dart-sdk\bin\dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
The issue here is that I can't get the absolute path for test.dart file.
When I run it like this:
dart /path/to/project/bin/test.dart
I get what I need:
1: file:///E:/projects/dart/test/bin/test.dart
2: /E:/projects/dart/test/bin/test.dart
3: dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
Is there a way how to get absolute path for a script that is currently running, regardless of a way how it was executed?
tl;dr: There's not a great way of doing what you want, but it's in the works.
The notion of a "path for a script that is currently running" is more complicated than it might sound at first blush. There are a number of ways that the Dart ecosystem invokes main(). Off the top of my head, here are a few:
Manually running the file with dart.
Running the file in an isolate.
Compiling the file to a snapshot and either manually running it or running it in an isolate.
Automatically compiling the file to a cached executable and running that either in a subprocess or in an isolate.
Adding a wrapper script that imports the file and invokes main(), and either running that in a subprocess or in an isolate.
Serving the file over HTTP, and running it either in a subprocess or in an isolate.
In some of these cases, the "script that is running" is actually a wrapper, not the original file you authored. In others, it's a snapshot that may have no inherent knowledge of the file from which it was created. In others, the file has been modified by transformers and the actual Dart code that's running isn't on disk at all.
I suspect what you're actually looking for isn't the executable URL itself, but the location of your package's files. We're working on a collection of APIs that will make this possible, as well as a resource package that will provide a nice API for dealing with your packages' resources.
Until that lands, there is a dart:mirrors hack you can use. If you give one of your library files an explicit library tag, say library my.cool.package, you can write:
var libPath = currentMirrorSystem().findLibrary(#my.cool.package).uri.path;
This will give you the path to your library, which you can use to figure out where your package's lib/ files live.
If you want a reliable way to access the current file while running in pub you'll need to use mirrors.
Here's a sample usage with dartdoc tests - https://github.com/dart-lang/dartdoc/blob/41a5e4d3f6e0084a9bc2af80546da331789f410d/test/compare_output_test.dart#L17
import 'dart:mirrors';
Uri get _currentFileUri =>
(reflect(main) as ClosureMirror).function.location.sourceUri;
void main() { ... }
Not particularly pretty, but it's easy to just put into a util file.
Currently I am building an application that launches crtmpd (a rtmp server written in C++).
Whenever I launch the application from within Flash Builder it works great, if I install a release build from an air file the process.start() returns a generic "could not start process" error.
The crtmpserver.exe is stored in applicationDirectory and the config file is kept in applicationStorageDirectoy.
So I've moved it to the native storage directory. But the problem persists. I've found the following though:
startupInfo = new NativeProcessStartupInfo();
startupInfo.executable = binFile;
processArgs = new Vector.<String>();
processArgs.push(luaFile.nativePath);
startupInfo.arguments = processArgs;
In the IDE after this startupInfo is setup correctly. On client machines all properties of startupInfo are still null.
You might want to try putting it in the application storage directory. I've seen permissions issues were the user wasn't Admin and could't run a native process once it was installed.
The problem was I was packaging with an Air installer rather than a native installer.
Once I packaged with the appropriate installer the problem has resolved itself.