How to run nestjs with electron? - electron

I am trying to make a desktop application with electronjs. Since nestjs makes the back-end development super-easy, I want to use it with electron.
Initially I tried to run the command
electron . && nest start
This makes the nest process start only after the electron app is closed.
I found only one video on youtube for this and no other proper solution elsewhere.
link: https://www.youtube.com/watch?v=vWpybfpyzPI
I was having difficulty in understanding it.
I also wanted to know the possibility of smooth communication between electron and nestjs if I am able to achieve what has been achieved in the video. It doesn't seem like a standard thing to me.

This is not easy but not impossible as well...
I will assume that you are going to create some angular frontend application along with local nestjs api server... Then we can do this with following tricks...
Create electron + angular app using xplat
Create your local nestjs application.
For production builds do the following trick..
a) In electron index.ts file add the following may be in create window function.
if (!serve) {
const { fork } = require('child_process');
const ps = fork(${__dirname}/main.js);
}
a.1) first build your Web Application.
b) build your nestjs application and then copy nestjs node_modules plus nestjs dist into your angular dist.
c) using electron_builder generate your packages and make sure electron builder will copy node_modules.. by default it will ignore it but we can pass some arguments to electron builder... by using package.json
something like below...
"files":[
"/*",
{
"from": "node_modules",
"to": "node_modules",
"filter": ["/*"]
}
],
Note : for serving we dont need to run nestjs in electron all we need is to serve nestjs separately...

Related

swagger-ui Existing application - JBoss EAP 7.4

I'm trying to integrate our RESTEasy API with swagger-ui to allow external module developers to test against our interface.
My knowledge is a little hazy when it comes to the support of existing APIs. The service containing the REST API is an application packaged into an *.ear file deployed on a JBoss EAP 7.4 Server.
I deployed the *.dist folder of swagger-ui on the undertow web-server and swagger-ui is accessible. So far so good.
My question is now: is it at all possible to have swagger detect the resource contents of the *.ear file without my having to configure the swagger.json manually, or, alternatively, package it into a *.war file and deploy it on the EAP?
If so, how to proceed? Do I need the swagger-codegen, the inspector oder will just the UI somehow work?
Thanks for any pointers.
Swagger can't automatically detect those. But if you're already exposing the swagger.json file in your ear, then maybe you can just change the swagger-ui source to look for that specific address when starting up. You should change the url property in the following code to point to your own:
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">
window.ui = SwaggerUIBundle({
"dom_id": "#swagger-ui",
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
queryConfigEnabled: true,
validatorUrl: "https://validator.swagger.io/validator",
url: "https://YOUR_SERVER/CONTEXT_PATH/swagger.json" // <-- CHANGE THIS URL
})
//</editor-fold>
};
Also if you're using a more recent versions of the WildFly, there is a dedicated subsystem named microprofile-openapi-smallrye that you can enable to generate the json file for you automatically. You can find more details about it in this tutorial.

Electron development - Generate API client based on swagger definition

I am very new to electron, trying to use it to build a cross-platform app which should be able to run natively on the machines. On the server side, I already have an application which exposes a REST API, documented with swagger.
Now I am trying to generate a client stub for this swagger definition, which I can then use with electron. How is that accomplished? Should I just generate JS code and use it (how would that work?)? Or is there another (better) way to do it as Electron has build in functions to access REST APIs like
I spent a considerable amount if time searching for a solution and did not find one. Now I wonder if that is such an uncommon scenario to use Electron as framework accessing REST APIs, and auto-generating the code using swagger codegen.
The great thing is that Electron apps can be very similarly developed to normal web applications. This is possibly why you didn't find specific instructions for using Electron with the tools you are used to using.
You should be able to go ahead and use whichever tools you would normally use to generate stubs for calling REST from any web application, and the stubs should work fine when referenced within Electron (as long as they generate in Javascript or Typescript).
Have you tried using Swagger codegen, did you try use the resulting client code API, and did it give you an error? Try posting any specific errors as new questions on Stack Overflow for solutions (or edit this question to be more specific).
Electron is almost like a blank canvas - there is no "right" or "wrong" way to develop, although there are certainly "good practises" and "bad practises".
There are definitely concepts that are unique to developing applications within Electron and for this it would be good to couple your development experience with some general Electron reading and learning.
You will very soon run in to "unique" Electron concepts such as "main" and "renderer" and it will be much easier if you have learning material to guide you. There is a lot of material for learning Electron so I won't try make a list here.
Also note that Stack Overflow is more useful when specific errors or minimum examples are provided and you'll probably get better answers that way :-) See: https://stackoverflow.com/help/mcve for more info on this.
I actually ended up swagger-codegen as GrahamMc suggested.
The general approach was like that:
rm -rf api
wget http://localhost/site/json-schema -O api.json
docker run --user `id -u`:`id -g` --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate -i /local/api.json -l javascript -o /local/api
rm api.json
cd api
npm install
Step 1 is cleaning up old generated code and step 2 is downloading the swagger spec which is not available from within the docker-container. The rest is cleaning up and installing dependencies.
From within the code, it can then be used like that:
var jtm_api = require('.api/')
var userApi = new jtm_api.UserApi()
var cb = function(error, data, response) {
if (response.status == 200) {
//do whatever
} else {
//do whatever
}
}
userApi.usersLoginPost(txtUser, txtPwd, cb)
There is an extensive documentation available on how to use the generated code starting from the README.md file within the generated folder.

Electron-Builder Linux updates - APPIMAGE env is not defined

I am on an Electron + Create React App stack.
I am using Electron Updater (https://github.com/develar/electron-updater) for auto updates.
Platform: Linux, Format: AppImage.
When my app checks for updates, I get the following error:
APPIMAGE env is not defined.
Has someone experienced the same issue? Suggestions required.
Please don't use electron-updater anymore, since it is no longer supported according to its GitHub page.
Most often, this occurs when you are trying to use the auto updater in development mode (or non-packaged AppImage mode). It only works in a packed production build.
For me however, this also occurred in a packed AppImage, and turned out to be caused by using the webpack DefinePlugin, like this:
plugins: [
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
},
...
Removing the process.env definition allowed APPIMAGE to be defined once again in the distributed package. It seems the rest of the definitions can be left in place safely without breaking the auto-updater.
If removing this definition is not possible for your use-case, according to some users it's possible to simply override it at the beginning of your main thread (not renderer thread) file like this:
process.env.APPIMAGE = path.join(__dirname, 'dist', `MyApp-${app.getVersion()}.AppImage`)
... obviously with the correct file name in the 3rd argument of Path.join.
This override approach didn't seem to do anything for me though, so I myself went with simply removing process.env from the DefinePlugin definitions, but it may help in other cases.
try to use electron-builder for building your app cause this module is is in maintainance only mode.
the autoUpdate for linux is not possible, you can do that only for mac and windows try to read this documentation.

How do I demo an electron app on the web

Is there a way to easily distribute an electron.atom.io app as a static site?
I don't need all the functionality, I just want to allow the client to view the latest updates.
-- edit --
Perhaps a better way to ask the question is; "How do I build a web app that can be hosted online and run on electron with minimum rewriting" - similar to the Slack app that works the same way on web or electron app.
As long as your main use of Electron is to create a 'native browser wrapper' for a web-app this is entirely possible.
You will have to implement a check if your application is running inside a browser or inside Electron and wrap your electron specific code in it:
if (window && window.process && process.versions['electron']) {
const {BrowserWindow} = require('electron').remote
}
You'll probably have to step through your application and disable Electron specific functionality at multiple places.
You have other options to do a long distance demo of an Electron app
Electron is basically a shell to run node.js apps on the desktop. This means if you want to move it to the web, you have to give up all the Electron APIs that access the local system and you're left with a basic node.js app, which is most likely not desirable.
To demo your desktop app to an off-site client, you can either make a presentation with screenshots detailing the current user flow, or compile a sandboxed demo version of your app and send it over to them.
Screen presentation
This is your quickest and easiest solution if your client just wants to be kept in the loop and see some eye candy. You can just record how the app works with some example data, add some written or audio explanation to it, and let them enjoy the smooth ride.
Build a demo
If your client wants to actually have a hands-on demo with the app, you need to have some form of basic code distribution. The cleanest way to do this would be to tie up all loose ends in your current app flows, block all unfinished roads in it and compile it for whatever platform your client requested the demo for.
Take a look at the electron-packager and electron-builder docs to get an idea how to build an .exe, .dmg or whatever file from your Electron app, then send that file to them with some basic instructions.

Using Cloud Code with the Parse Server and Heroku

I am trying to understand the new Parse Server and have deployed on Heroku. This went smoothly but what I am struggling with is figuring out how to write server side code (Cloud Code). I've read over the parse server example many times so I must be missing something but I'm very unclear if I should be using Express for something, or how I even begin to include my Cloud Code files. Any help is very much appreciated.
UPDATE:
I found the cloud folder I was just looking in the wrong place. I moved it and index.js to my apps folder on the desktop. I have changed the default code in main.js to my custom code. I have set up index.js with my apps information. The problem now is when I run the app and try to call the cloud code functions I get error invalid function.
If you have the parse server example running on heroku you are 90 percent there. Just open the cloud/main.js file and start adding your cloud code. There should be a hello cloud function there as an example.
To use your already created cloud code modules/files you can require them as you have done before on parse.com. The only difference is that the path should now be relative instead of absolute. For example require('cloud/cloudFunctions'); should be require('./cloudFunctions'); if you had a module called cloudFunctions.js in the cloud directory.
Cloud Code works similar to how it did on parse.com and you shouldn't have to think too much about expressjs for simple applications. That said, parse server is using expressjs so yes you are using it.
Parse server is simply a another node module similar to the other thousands available. If you do not have previous experience with nodejs, running parse server can seem complicated. Therefore I would recommend reading about the basics of nodejs before a full migration.
I'm using the Bitnami stack on a Google Compute Engine instance and I had a similar problem to yours. To solve it, just navigate to the folder where your server.js file is and create a folder called "cloud". Then create the main.js file inside the cloud folder with the following content:
Parse.Cloud.define('hello', function(req, res) {
res.success('Hi');
});
Now open the server.js file and find the line containing the path to the cloud code file. Change it to point to you main.js file like this:
This could be any arbitrary folder of your choosing.
Now just restart your parse server and call the cloud function:
String result = ParseCloud.callFunction("hello", new HashMap<>());
This is with the Java SDK but should not be much different. The variable result will equal "Hi" if you've used the function from above.

Resources