Dean Radcliffe
Dean Radcliffe

Reputation: 2206

Can I run a one-liner using Meteor Shell?

As I develop useful one-liners, it'd be handy to be able to run meteor shell, passing it the one-liner, and expecting that it will exit when done. Some syntax such as this, perhaps ?

meteor shell -e 'Meteor.users.remove({})'

Is this a feature request, or does it already exist already ?

Supplemental: I picture adding several of these scripts to package.json so they can be shared by all developers on the project.

Upvotes: 4

Views: 532

Answers (1)

Adam Monsen
Adam Monsen

Reputation: 9420

Here's an Expect program for scripting the Meteor shell. You can either run a separate file or run a command directly. Usage:

./mshell -f runThisFile.js
./mshell -e 'console.log("foo")'

Here's the code (License: MIT). Save to a file (mshell, or whatever you choose) and make executable:

#!/usr/bin/expect --
set timeout 3
spawn meteor shell
expect "> "
set firstArg [lindex $argv 0]
set secondArg [lindex $argv 1]
if { $firstArg == "-f" } {
  send [exec cat $secondArg]
  send "\n"
} elseif { $firstArg == "-e" } {
  send "$secondArg\n"
}
expect "true"
send "\x04"
expect "Shell exiting...\n"

On Ubuntu I ran sudo apt-get install expect to install Expect prior to running this program.

Clearly we'd rather running scripts or one-liners were built into meteor shell, so let's just consider this a proof-of-concept.

Upvotes: 8

Related Questions