How to create manage/server side commands? - meteor

Is there a way in Meteor, to add your own commands to the meteor command for the given project? So that on the server side you could have some commands similar to Django's manage commands. Like command for importing/updating data, maybe cron jobs, things like that? Thing is, I would like commands to have access to all the libraries and environment Meteor provides, especially so that I am sure that data stored in the database is compatible with Meteor.

At the moment this isn't directly possible without an edit of meteor's source. If you're using meteorite you can have a localized meteor version which won't affect the rest of your meteor apps.
You might have to add a custom function over at /tools/meteor.js in this forked/modified meteor.
with:
Commands.push({
name: "yourcustomcommand",
help: "..",
func: function (argv) {
//Custom stuff
});

Related

Hydra: Overriding hydra.run.dir/working directory management with Compose API

I'm attempting to use Hydra's compose API to launch runs programmatically instead of via CLI. This works for the most part. However, overriding hydra.run.dir to change the base directory doesn't seem to have the effect when using the compose API. I.e.:
with hydra.experimental.initialize_config_module(config_module=config_module):
cfg = hydra.experimental.compose(
config_name=config_name,
overrides=["hydra.run.dir=/tmp/workdir", ...],
return_hydra_config=True
)
hydra.core.hydra_config.HydraConfig.instance().set_config(cfg)
with omegaconf.open_dict(cfg):
del cfg["hydra"]
generates a DictConfig with the appropriate entry for hydra.run.dir, but the working directory is not changed.
The compose API documentation states that not using #hydra.main entails forfeiting Hydra's working directory management. Is there a workaround for this?
The Compose API is stateless and is intentionally not changing the working directory, configuring the logging or changing global state and not integrating with the command line. If you need these functionalities you should consider using #hydra.main().
As a workaround, you can call chdir programmatically on your end (with os.chdir). You will probably also need to mkdir first.

Meteor: session-client-side

So im trying to build ontop of the angular-meteor WhatsApp clone tutorial using Ionic 2 CLI
This tutorial basically deletes the client folder in the meteor project and uses the meteor-client-side package inside an ionic project to connect to the meteor server.
Which works perfectly fine, but now i'd like to subscribe to a meteor publication with an reactive parameter.
After searching the Meteor API Documentation I found the Session object:
Session provides a global object on the client that you can use to
store an arbitrary set of key-value pairs. Use it to store things like
the currently selected item in a list.
What’s special about Session is that it’s reactive. If you
call Session.get("currentList") from inside a template, the template
will automatically be rerendered whenever 
Session.set("currentList", x) is called.
In the Meteor Subscribe Documentation you can find the following example:
Tracker.autorun(function () {
Meteor.subscribe("chat", {room: Session.get("current-room")});
Meteor.subscribe("privateMessages");
});
This subscribes you to the chat messages in the current room and to
your private messages. When you change rooms by
calling Session.set("current-room", "new-room"), Meteor will subscribe
to the new room’s chat messages, unsubscribe from the original room’s
chat messages, and continue to stay subscribed to your private
messages.
Which is exactly what I want to do too. But as the Session documentation states, session is a package I have to add to the meteor project:
To add Session to your application, run this command in your terminal:
meteor add session
Now my question, is there any way to add session to the meteor-client-side packages?
If I just try to call Session.set() it fails on runtime with Session is not defined
My guess is that I would need some npm package that extracts the Session functionality (basically a sessions-client-side npm package) like accounts-base-client-side
Is there an other way to do this?
How would I build my own sessions-client-side?
I tried to run meteor add session in my meteor project but was not able to find the code for Session anywhere in .meteor folder and npm_modules.
I also looked into the meteor GitHub but the Session.js file they have contains only documentation
Any input how to do something like this would be nice
Update:
I've looked into the accounts-base-client-side package and found out that they are autogenerated using a script, so im currently trying to adapt this script to work with Session instead of accounts-base.
You can find my attempt at: https://github.com/AwsmOli/session-client-side
Still work in progress, but i should get it to work soon
Update 2:
See my answer, my session-client-side is working now :)
The "Session" variable should just appear and be accessible. If you need to verify that, start a new project add the package and write some code to access it. It is likely that something has (unwittingly) nuked the Session variable - I have seen this before with another package.
Another way of doing this is with "getReactively". Below is a helper that uses it in a query. Make sure you declare it before the helper (otherwise it won't work). This one uses the result of another helper, but it can be any variable, and you just assign the variable for the reactivity to kick in and run the helper.
this.helpers({
currentUser: () => { return Meteor.user() },
elder: () => {
let e = Elders.findOne({_id: this.getReactively('this.currentUser._id')});
if (e) {
utils.services.setupElder(e);
}
return e;
}
});
As per the meteor docs, you have to import it:
import { Session } from 'meteor/session'
This will enable it on the client.
In earlier meteor versions this was not required, as it was both a default package, and automatically imported into the global namespace.
I ended up creating the session-client-side package myself, and its working nicely.
If you need it too, its available on GitHub:
https://github.com/AwsmOli/session-client-side
and NPM:
npm install session-client-side
credits to idanwe who created the client side packages and made it realy easy to adapt his work to work with any meteor package :)
To use it with Ionic 2 Apps:
import it in your entry points (src/app/main.prod.ts & src/app/main.dev.ts)
import 'session-client-side';
and now the global Session variable is accessable form anywhere in your app:
Session.set("aCoolNameFormyAwsmChangingObject", myAwsmChangingObject);
Thanks for the help!

How to read Meteor settings from a build plugin

I'm currently working on a package for Meteor that includes a build plugin. I need to access configurations from the settings file.
However Meteor.settings doesn't work (Meteor is not defined) and process.env.METEOR_SETTINGS also doesn't exist.
Is there any way for my plugin to access the settings file?
It appears that despite the documentation discussing the use of --settings, it doesn't work in a production environment, as often command line options are not available.
So the solution is to use environment variables, which are available only on the server.
server code, meteor methods:
eor methods
Meteor.methods({
getPJS: function() {
return process.env.PEERJS_SERVER;
},
client code
var PJS = Meteor.call("getPJS");
So you can make those environment variables available on the client if you need them.

What is __meteor_bootstrap__?

I am just starting with Meteor and working on an existing project. I am running into an issue with one of the packages(observatory-apollo) that's has the following line:
__meteor_bootstrap__.app.use Observatory.logger #TLog.useragent
It is complaining that __meteor_bootstrap__.app is undefined.
What is __meteor_boostrap__ exactly? I can't seem to find a description of what it is but from threads, people seem to know how to use it. I can only see it defined in boot.js, but it doesn't really tell me much...
Meteor uses connect npm module under the hood for various reasons, to serve static files, for example. __meteor_bootstrap__.app was the reference to connect app instance.
Before it was __meteor_bootstrap__.app but it changed couple of releases ago and became WebApp.connectHandlers object and is part of WebApp package.
WebApp is a standard package of Meteor, core package for building webapps. You don't usually need to add explicitly as it is a dependency of standard-app-packages.
Example of usage the connectHandlers is to inject connect middlewares in the same way as you would use any connect middleware (or some express middlewares, express is built on top of connect):
WebApp.connectHandlers
.use(connect.query())
.use(this._config.requestParser(bodyParser))
You can look at meteor-router Atmosphere package and take it as an example: https://github.com/tmeasday/meteor-router/blob/master/lib/router_server.js
More about connect: https://npmjs.org/package/connect

Using Handlebars util methods in Meteor server

I'm working on my first Meteor app, and I'm trying to escape a string on the server side. I was hoping to use Handlebars.Utils.escapeExpression, but even when I add handlebars (which I had to do, even though Meteor already uses it?), I still get
ReferenceError: Handlebars is not defined
error when that code is hit. Is there a way to invoke that method server side without manually including the source in my project?
Meteor uses Handlebars on the client only. Server-side rendering is on the roadmap.
Also, the Handlebars that comes with Meteor doesn't include Utils.
Use {{{thingThatNeedsEscaping}}} instead, as per the documentation that unescapes it.
Also, I don't think it's necessary to escape stuff before inserting it into the database, if you want it though there are other JS functions for that (like escape variants that are not deprecated).

Resources