Skip to content

Commit d3a4d5a

Browse files
move local utils functions from vease
1 parent 9946ccd commit d3a4d5a

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

utils/local.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// Node.js imports
2+
import fs from "fs";
3+
import path from "path";
4+
import child_process from "child_process";
5+
6+
// Third party imports
7+
import pkg from "electron";
8+
const { app, dialog } = pkg;
9+
import { getPort } from "get-port-please";
10+
import pidtree from "pidtree";
11+
import isElectron from "is-electron";
12+
13+
import { fileURLToPath } from "url";
14+
15+
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
16+
const __dirname = path.dirname(__filename); // get the name of the directory
17+
console.log("__dirname", __dirname);
18+
19+
// Global variables
20+
var processes = [];
21+
22+
function venv_script_path(root_path, microservice_path) {
23+
const venv_path = path.join(root_path, microservice_path, "venv");
24+
var script_path;
25+
if (process.platform === "win32") {
26+
script_path = path.join(venv_path, "Scripts");
27+
} else {
28+
script_path = path.join(venv_path, "bin");
29+
}
30+
return script_path;
31+
}
32+
33+
function executable_path(microservice_path) {
34+
if (isElectron()) {
35+
if (app.isPackaged) {
36+
return process.resourcesPath;
37+
} else {
38+
return venv_script_path(app.getAppPath(), microservice_path);
39+
}
40+
} else {
41+
return venv_script_path(process.cwd(), microservice_path);
42+
}
43+
}
44+
45+
function executable_name(name) {
46+
if (process.platform === "win32") {
47+
return name + ".exe";
48+
}
49+
return name;
50+
}
51+
52+
function create_path(path) {
53+
if (!fs.existsSync(path)) {
54+
fs.mkdir(path, (err) => {
55+
if (err) {
56+
return console.error(err);
57+
}
58+
console.log(`${path} directory created successfully!`);
59+
});
60+
}
61+
return path;
62+
}
63+
64+
async function get_available_port(port) {
65+
const available_port = await getPort({ port, host: "localhost" });
66+
console.log("available_port", available_port);
67+
return available_port;
68+
}
69+
70+
async function kill_processes() {
71+
console.log("kill_processes", processes);
72+
await processes.forEach(async function (proc) {
73+
console.log(`Process ${proc} will be killed!`);
74+
try {
75+
process.kill(proc);
76+
} catch (error) {
77+
console.log(`${error} Process ${proc} could not be killed!`);
78+
}
79+
});
80+
}
81+
82+
function register_children_processes(proc) {
83+
pidtree(proc.pid, { root: true }, function (err, pids) {
84+
if (err) console.log("err", err);
85+
processes.push(...pids);
86+
});
87+
}
88+
89+
async function run_script(
90+
command,
91+
args,
92+
expected_response,
93+
timeout_seconds = 30
94+
) {
95+
return new Promise((resolve, reject) => {
96+
setTimeout(() => {
97+
reject("Timed out after " + timeout_seconds + " seconds");
98+
}, timeout_seconds * 1000);
99+
const child = child_process.spawn(command, args, {
100+
encoding: "utf8",
101+
shell: true,
102+
});
103+
register_children_processes(child);
104+
105+
// You can also use a variable to save the output for when the script closes later
106+
child.stderr.setEncoding("utf8");
107+
child.on("error", (error) => {
108+
dialog.showMessageBox({
109+
title: "Title",
110+
type: "warning",
111+
message: "Error occured.\r\n" + error,
112+
});
113+
});
114+
child.stdout.setEncoding("utf8");
115+
child.stdout.on("data", (data) => {
116+
//Here is the output
117+
data = data.toString();
118+
if (data.includes(expected_response)) {
119+
register_children_processes(child);
120+
resolve(child);
121+
}
122+
console.log(data);
123+
});
124+
125+
child.stderr.on("data", (data) => {
126+
console.log(data);
127+
});
128+
129+
child.on("close", (_code) => {
130+
//Here you can get the exit code of the script
131+
console.log("Child Process exited with code " + _code);
132+
});
133+
child.on("kill", () => {
134+
console.log("Child Process killed");
135+
});
136+
child.name = command.replace(/^.*[\\/]/, "");
137+
return child;
138+
});
139+
}
140+
141+
async function run_back(port, data_folder_path) {
142+
return new Promise(async (resolve, reject) => {
143+
const back_command = path.join(
144+
executable_path(path.join("microservices", "back")),
145+
executable_name("vease-back")
146+
);
147+
const back_port = await get_available_port(port);
148+
const back_args = [
149+
"--port " + back_port,
150+
"--data_folder_path " + data_folder_path,
151+
"--allowed_origin http://localhost:*",
152+
"--timeout " + 0,
153+
];
154+
await run_script(back_command, back_args, "Serving Flask app");
155+
resolve(back_port);
156+
});
157+
}
158+
159+
async function run_viewer(port, data_folder_path) {
160+
return new Promise(async (resolve, reject) => {
161+
const viewer_command = path.join(
162+
executable_path(path.join("microservices", "viewer")),
163+
executable_name("vease-viewer")
164+
);
165+
const viewer_port = await get_available_port(port);
166+
const viewer_args = [
167+
"--port " + viewer_port,
168+
"--data_folder_path " + data_folder_path,
169+
"--timeout " + 0,
170+
];
171+
await run_script(viewer_command, viewer_args, "Starting factory");
172+
resolve(viewer_port);
173+
});
174+
}
175+
176+
export {
177+
create_path,
178+
executable_name,
179+
executable_path,
180+
get_available_port,
181+
kill_processes,
182+
register_children_processes,
183+
run_script,
184+
run_back,
185+
run_viewer,
186+
};

0 commit comments

Comments
 (0)