gee-community / ee-runner

Command-line runner for Google Earth Engine Playground scripts
Apache License 2.0
67 stars 18 forks source link

running script with packages #26

Closed guy1ziv2 closed 5 years ago

guy1ziv2 commented 5 years ago

Is it possible to run a script that has a package e.g.

var g = require('users/gene/packages:geometry') ?

I tried to run on a linux by

  1. git cloning my javascript files
  2. git cloning the packages into a directory called users/gene/packages

but this still reports an error. Any ideas?

gena commented 5 years ago

Only with some small modifications, because EE does not use standards when including packages via require(). You can put all packages in a separate folder, e.g. ./packages/, and then include as:

var g = require('./packages/geometry')

guy1ziv2 commented 5 years ago

To keep code organized - is there a way to check in the script if it runs under ee-runner or inside the Code Editor (I am cloning the scripts from googlesource.com every update). Maybe something like

if (running_on_ee_runner) {
    var g = require('./packages/geometry')
} else {
    var g = require('users/gene/packages:geometry')
}
gena commented 5 years ago

... hmm, you can probably try this:

function isCommandLine() {
  try { 
    commandLine 
  }
  catch(e) {
      if(e.name == "ReferenceError") {
          return false;
      }
  }

  return true
}

print(isCommandLine())

This should return true in ee-runner and false in Code Editor

guy1ziv2 commented 5 years ago

This is great, thanks!

Guy Ziv

On Sun, Jun 23, 2019 at 9:42 PM gena notifications@github.com wrote:

... hmm, you can probably try this:

function isCommandLine() { try { commandLine } catch(e) { if(e.name == "ReferenceError") { return false; } }

return true } print(isCommandLine())

This should return true in ee-runner and false in Code Editor

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/gee-community/ee-runner/issues/26?email_source=notifications&email_token=AAP66O2QB6VISEGYAI2V6PLP37N4RA5CNFSM4H2ZTC7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYLGTIY#issuecomment-504785315, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP66O6IWUP2IGWOIC3T2GDP37N4RANCNFSM4H2ZTC7A .

guy1ziv2 commented 5 years ago

It ended up a tiny bit more complex, because how Code Editor is looking at require(). A working script goes like this:

if (isCommandLine()) { var filename = '../packages/viewshed' var pkg = require(filename) } else { var pkg = require('users/guy1ziv2/packages:viewshed') }