Skip to content

Commit 1cb319f

Browse files
committed
just commiting the code provided in the alpha
directory this is acting as a base, not really meaningful.
1 parent 0ed4fe3 commit 1cb319f

File tree

7 files changed

+6499
-0
lines changed

7 files changed

+6499
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Server/node_modules/

Commander/Commands.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Description: Firebase Command extension for DroidScript
3+
Author(s): D.Hurren, you?
4+
Copyright: droidscript.org
5+
License: Apache V2
6+
*/
7+
8+
//Make sure node sever is running.
9+
function Init() {
10+
//Start menu server (with unique context id) on first show of 'Firebase' context menu.
11+
if (!glob[ext_progId]) {
12+
var dir = "/sdcard/DroidScript/Extensions/Firebase/Server";
13+
ext_NodeRun(
14+
dir + "/node_Firebase.js",
15+
ext_progId,
16+
dir + "/node_modules",
17+
);
18+
glob[ext_progId] = true;
19+
}
20+
}
21+
22+
//Called when using menus or wifi ide '!firebase' commands.
23+
function Firebase_OnCommand(cmd) {
24+
Init();
25+
26+
//Execute menu command in our node node_Firebase.js file.
27+
ext_NodeExec(cmd.toLowerCase() + "()", ext_progId);
28+
}
29+
30+
//Called when 'Firebase' device context menu is selected.
31+
function Firebase_OnMenu() {
32+
Init();
33+
34+
//Show Firebase sub-menus.
35+
dlgFirebase = app.CreateListDialog("Firebase", "Serv,Deploy");
36+
dlgFirebase.Show();
37+
38+
//Handle menu selection.
39+
dlgFirebase.SetOnTouch((cmd) => {
40+
//Show fresh debug window.
41+
app.ShowDebug(true, "dialog,clear");
42+
43+
//Execute menu command in our node node_Firebase.js file.
44+
ext_NodeExec(cmd.toLowerCase() + "()", ext_progId);
45+
});
46+
}

Commander/Commands.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["firebase deploy", "firebase serv", "firebase login", "firebase list"]

Server/node_Firebase.js

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
Description: Firebase Command extension for DroidScript
3+
Author(s): D.Hurren, you?
4+
Copyright: droidscript.org
5+
License: Apache V2
6+
7+
Useful resources:
8+
https://github.com/firebase/firebase-tools
9+
https://github.com/firebase/firebase-tools/tree/master/src/commands
10+
Note: minor tweaks done to firebase-tools/lib/commands/login.js and firebase-tools//lib/logger.js
11+
*/
12+
13+
//Globals
14+
var firebaseLoaded = false;
15+
var client = null;
16+
var curProjId = null;
17+
18+
//Prevent process exiting on errors.
19+
process.on("SIGINT", function () {
20+
console.log("Received exit signal");
21+
});
22+
23+
//Load Firebase tools (if not already done).
24+
function init(callback) {
25+
if (!firebaseLoaded) {
26+
//Load firebase.
27+
console.log("Loading Firebase...");
28+
process.env.GOOGLE_APPLICATION_CREDENTIALS =
29+
process.env.DS_DIR +
30+
"/.keys/" +
31+
process.env.DS_PROJECT +
32+
"/firebase-key.json";
33+
process.env.XDG_CONFIG_HOME =
34+
process.env.DS_DATA_DIR +
35+
"/.DroidScript/Firebase/" +
36+
process.env.DS_PROJECT;
37+
process.env.DEBUG = false;
38+
client = require("firebase-tools");
39+
firebaseLoaded = true;
40+
41+
//Get project id (will only be one as we use project service key)
42+
client.projects
43+
.list()
44+
.then((data) => {
45+
curProjId = data[0]?.projectId;
46+
console.log("Project Id = " + curProjId);
47+
callback();
48+
})
49+
.catch((err) => {
50+
console.log(err.toString());
51+
});
52+
} else callback();
53+
}
54+
55+
//Login to Firebase (not required if using local creds).
56+
function login() {
57+
init(() => {
58+
client.logger.logger.reset();
59+
client
60+
.login({ localhost: true })
61+
.then((data) => {
62+
console.log(data);
63+
})
64+
.catch((err) => {
65+
console.log(err.toString());
66+
});
67+
});
68+
}
69+
70+
//List all Firebase projects.
71+
function list() {
72+
init(() => {
73+
client.logger.logger.reset();
74+
client.projects
75+
.list()
76+
.then((data) => {
77+
console.log(data);
78+
})
79+
.catch((err) => {
80+
console.log(err.toString());
81+
});
82+
});
83+
}
84+
85+
//Set default Firebase project.
86+
function use(projId) {
87+
var conf =
88+
process.env.DS_DIR + "/" + process.env.DS_PROJECT + "/firebase.json";
89+
90+
init(() => {
91+
console.log("Setting config: " + conf);
92+
client.logger.logger.reset();
93+
client
94+
.use(projId, { project: projId, config: conf })
95+
.then((data) => {
96+
console.log(data);
97+
})
98+
.catch((err) => {
99+
console.log(err.toString());
100+
});
101+
});
102+
}
103+
104+
//Get default Firebase project.
105+
function using() {
106+
init(() => {
107+
client.logger.logger.reset();
108+
client.projects
109+
.list()
110+
.then((data) => {
111+
console.log(data[0]?.projectId);
112+
})
113+
.catch((err) => {
114+
console.log(err.toString());
115+
});
116+
});
117+
}
118+
119+
//Start local test http server for project.
120+
//(projId is optional, defaults to current 'using' project)
121+
function serv() {
122+
var conf =
123+
process.env.DS_DIR + "/" + process.env.DS_PROJECT + "/firebase.json";
124+
console.log("Setting config: " + conf);
125+
126+
const fs = require("fs");
127+
var publicDir =
128+
process.env.DS_DIR + "/" + process.env.DS_PROJECT + "/public";
129+
if (!fs.existsSync(publicDir)) {
130+
console.log("Error: No 'public' folder exists in this project");
131+
return;
132+
}
133+
134+
init(() => {
135+
console.log("Serving on port 5000...");
136+
client.logger.logger.reset();
137+
client
138+
.serve({
139+
project: curProjId,
140+
config: conf,
141+
port: 5000,
142+
only: "hosting",
143+
})
144+
.then((data) => {
145+
console.log(data);
146+
})
147+
.catch((err) => {
148+
console.log(err.toString());
149+
});
150+
});
151+
}
152+
153+
//Deploy project to firebase server (reads local firebase.json file)
154+
//(projId is optional, defaults to current 'using' project)
155+
function deploy() {
156+
var conf =
157+
process.env.DS_DIR + "/" + process.env.DS_PROJECT + "/firebase.json";
158+
console.log("Setting config: " + conf);
159+
160+
init(() => {
161+
console.log("Deploying to Firebase...");
162+
client.logger.logger.reset();
163+
client
164+
.deploy({ project: curProjId, config: conf })
165+
.then((data) => {
166+
console.log("Done! " + JSON.stringify(data));
167+
})
168+
.catch((err) => {
169+
console.log(err.toString());
170+
});
171+
});
172+
}

0 commit comments

Comments
 (0)