How to import node.js child_process to Ember Electron app? - ember.js

I'm trying to convert a node.js app to Ember Electron app. In the node.js app I use:
const spawn = require('child_process').spawnSync;
but don't know how to import 'child_process' into Ember Electron app.
Have tried:
import { child_process } from 'child_process'; // blows up: requireNode is not a function
and also:
import requireModule from 'ember-require-module';
const child_process = requireModule('child_process'); // does not find it
Maybe using node.js is a wrong approach in Ember Electron app?
Any idea appreciated. Thanks.

Sorry, the documentation says how to do it:
const spawn = requireNode('child_process').spawnSync;
https://ember-electron.js.org/docs/guides/development-and-debugging#require-and-requirenode-
And it works perfect.

Related

How to setup Backand SDK in an Ionic2 app?

I'd like to setup my Ionic2 app to use the Backand SDK. I am new to Ionic2 / Angular2 and I couldn't find any example out there with an Ionic2 app. I could only find example apps with Ionic1 / Angular1 apps which don't help me.
Any clues?
Thanks, Guillaume
As Aaron said, you can take 'backandService.ts' file from /app/service folder in my repository at
https://github.com/backand/ionic2-crud-example
Here you have direct link to file.
If you are using Typescript, you will be able to load it with this snippet:
import {BackandService} from '../../services/backandService'
Inject BackandService in you constructor:
constructor(public backandService:BackandService) {
}
And do login like this:
this.backandService.getAuthTokenSimple(this.username, this.password);
And then post data with this code:
this.backandService.postItem(this.name)

What's the recommended method for adding javascript functions in Ember-cli project?

I want to create a library containing general-purpose javascript functions and classes for use in any Ember object in an Ember-cli project. What's the recommended procedure for doing this?
You should generate new util in Ember CLI:
ember g util your-name
Then you can import it using:
import yourName from '../utils/your-name'
Creating an addon is the Ember-CLI-preferred method of doing that. Addons can be published to NPM and installed in Ember CLI projects using NPM. This makes it incredibly easy to share code. There's a guide for developing addons on the Ember CLI site.

werkzeug DispatcherMiddleware with separate ports

I'm playing with combining a couple flask apis that I have into one application that can be a little easier to deploy and set up for devs instead of needing three separate applications running. Currently each api resides on a separate port. I'm trying to use the DispatcherMiddleware to run all three applications but so far it seems like you can only use prefixes like
from frontend import app as frontend
from TestApi import app as test
from DevApi import app as dev
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
app = DispatcherMiddleware(frontend, {
'/test': test,
'/dev': dev
})
run_simple('localhost', 4000, app, use_reloader=True)
now all my services run on 4000 but what id like to have is something like this
from frontend import app as frontend
from TestApi import app as test
from DevApi import app as dev
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
app = DispatcherMiddleware(frontend, {
':5000': test,
':6000': dev
})
#frontend runs on 4000, test runs on 5000, dev runs on 6000
run_simple('localhost', 4000, app, use_reloader=True)
Am I just asking for something that makes no sense or is there a way to accomplish using this or another setup.
I've come to the conclusion that this is a stupid idea that is very fragile and requires a lot of unnecessary complexity. I've instead just opted to write a bash script that starts all the apps as separate wsgi instances on their own port.
The Flask documentation on application dispatching contains no hint on port based dispatching. As you mentioned you can simply start separate wsgi instances, as far as I know this is the only possible way using Werkzeug.
Reference: http://flask.pocoo.org/docs/latest/patterns/appdispatch/

Ember CLI http-mock as Addon

I'm using Ember CLI's http-mock feature to mock REST API endpoints, but I'd like to use it in multiple Ember CLI applications. I thought an addon would be a great solution to this, but I can't seem to get it to work. Does Ember Addon support http-mock?
Here's what I did.
Created an add on
$ ember addon my-http-mock
Then I created a simple test endpoint in the addon
$ ember g http-mock users
After publishing it to my github repository, I imported it into an Ember CLI project like this in package.json
"dependencies": {
"my-http-mock": "git://github.com/git-username/my-http-mock"
}
After npm installing it, I ran my app, but going to http://localhost:4200/api/users doesn't go to the API endpoint, and instead tries to load the Ember app.
Is there any way to use http-mock in multiple applications?
You'll need to have your addon implement the serverMiddleware and you can add middleware, or routes, to the http-mock running in the consuming ember-cli application.
Advanced Addon customization in ember-cli docs
That hook get's passed a config object that has the express app instance on it at config.app. You can then add whatever you'd like to do. If you're using the generated http-mock in the addon, it'd look something like this
{
name: 'my-http-mock',
serverMiddleware: function(config) {
// To require ALL mocks from your addon
var server = require('./server');
server(config.app);
// To require individual mocks
var users = require('./server/mocks/users');
users(config.app);
}
}
This is untested code but should work. The require all mocks one could possibly conflict in a weird way because it adds the bodyparser middleware and connect-restreamer and it probably already has that included if your app already has those from it's local http-mock. Try it out though! :)
note: This answer is in reference to using ember-cli 0.1.2

How do I use Tipfy and wsgi_intercept together for testing?

I'm creating an API using Tipfy. I have an existing suite that tests some methods I will expose via URL, but what I'd like to do is see exactly how these functions will work once they're exposed. I'd like to test the URL params directly, for example.
I think what I need is something like wsgi_intercept. It uses a function that
returns a WSGI app to run its tests so you don't have to run a web server in parallel; it bootstraps it for you in the setUp function. There's an example at http://ivory.idyll.org/articles/twill-and-wsgi_intercept.html, but I'm not sure how to do it with Tipfy.
What call to the Tipfy library will return the WSGI application itself? Tipfy.wsgi_app?
If there is another testing strategy or tool that you can suggest, I'd also appreciate that.
Thanks!
Use easy_install to install wsgi_intercept, then get hold of the Tipfy app via make_wsgi_app.
import config
import tipfy
app = tipfy.make_wsgi_app(config.config)
# Enable the interception of HTTP calls.
from wsgi_intercept.urllib2_intercept import install_opener
install_opener()
wsgi_intercept.add_wsgi_intercept('localhost', 8000, lambda: app)