Skip to content

Commit dffb59b

Browse files
committed
GEE script runner module
1 parent bb8cdd1 commit dffb59b

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/utilities/scriptRunners.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Helpers to run GEE scripts.
3+
*/
4+
import * as vscode from 'vscode';
5+
import { getAccountToken } from './getToken';
6+
import { mkdtemp, rmdir } from 'node:fs/promises';
7+
import path = require("path");
8+
import os = require("os");
9+
import fs = require("fs");
10+
const scriptPrefix = "exports.main=function(ee,ceu,errCallback){" +
11+
"var print=ceu.print;var Map=ceu.Map; " +
12+
"Export=ceu.ExportMod(ee.batch.Export, errCallback); \n";
13+
var codeEditorUtils = require("./codeEditorUtils.js");
14+
function taskStartError(err:any){
15+
vscode.window.showErrorMessage("Failed to start EE task: \n " + err);
16+
}
17+
18+
function scriptRunError(err:any){
19+
vscode.window.showErrorMessage("EE script run failed: \n " + err);
20+
}
21+
22+
function eeInitError(err:any){
23+
vscode.window.showErrorMessage("EE initialization failed: \n " + err);
24+
}
25+
26+
export function scriptRunner(account:string, project: string | null, context:vscode.ExtensionContext){
27+
/*
28+
Runs a GEE script using a user account/project
29+
*/
30+
const editor = vscode.window.activeTextEditor;
31+
if (editor) {
32+
var ee = require("@google/earthengine");
33+
let document = editor.document;
34+
const documentUri = document.uri;
35+
if (documentUri.scheme==='file'){
36+
getAccountToken(account, context.globalState)
37+
.then((token:any)=>{
38+
ee.data.setAuthToken('', 'Bearer', token, 3600, [],
39+
()=>{
40+
try{
41+
ee.initialize(null, null,
42+
()=>{
43+
try{
44+
if(project){
45+
ee.data.setProject(project);
46+
}
47+
// Create a temporary directory:
48+
mkdtemp(path.join(os.tmpdir(), 'eetasksRunner-'))
49+
.then((tempDir:string)=>{
50+
// Create the file to run in the temporary directory
51+
let tempFile = path.join(tempDir, "temp.js");
52+
// TODO: random name instead of temp.js
53+
let tempUri = vscode.Uri.file(tempFile);
54+
fs.writeFile(tempFile,
55+
scriptPrefix + document.getText() + "\n}", () => {
56+
try{
57+
const userCode = require(tempFile);
58+
try{
59+
/*
60+
Note:
61+
Issue in windows when calling some ee
62+
functions synchronously. See:
63+
https://stackoverflow.com/questions/77436205/synchronous-function-call-to-external-library-within-vscode-freezes-only-in-wind
64+
In node/windows, node/linux,
65+
and vscode/linux this is not an issue.
66+
*/
67+
userCode.main(ee, codeEditorUtils, taskStartError);
68+
}catch(error){scriptRunError(error);}
69+
}catch(error){scriptRunError(error);}
70+
// Delete the temporary file and directory,
71+
// even if the script failed.
72+
vscode.workspace.fs.delete(tempUri).then(
73+
()=>rmdir(tempDir));
74+
}); // fs.writeFile
75+
}); // mkdtemp
76+
}catch (error){
77+
scriptRunError(error);
78+
}finally{
79+
return;
80+
}},
81+
(error:any)=>{eeInitError(error);},
82+
null, project); // ee.initialize
83+
}
84+
catch(error){
85+
vscode.window.showErrorMessage("Error retrieving earth engine token: \n" + error);
86+
}
87+
}, false); // ee.setAuthToken
88+
})
89+
.catch((err:any)=>{
90+
vscode.window.showErrorMessage(err);
91+
console.log(err);
92+
});
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)