-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·165 lines (128 loc) · 3.34 KB
/
Copy pathcli.js
File metadata and controls
executable file
·165 lines (128 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
import { spawn, spawnSync } from "child_process";
import { select } from "@inquirer/prompts";
import open from "open";
import net from "net";
const REST_PORT = await findFreePort(5000);
const FRONTEND_URL = "https://aiidateam.github.io/aiida-explorer/";
function fail(msg) {
console.error(`\n${msg}\n`);
process.exit(1);
}
/* ---------------------------
CHECKS
---------------------------- */
function checkVerdi() {
const res = spawnSync("verdi", ["--help"]);
if (res.error) fail('"verdi" not found.');
}
function checkRestApiDeps() {
const res = spawnSync("python", [
"-c",
"from aiida.restapi.run_api import run_api",
]);
if (res.status !== 0) {
fail("Missing AiiDA REST API dependencies.");
}
}
function isPortFree(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.once("error", () => resolve(false));
server.once("listening", () => {
server.close();
resolve(true);
});
server.listen(port, "127.0.0.1");
});
}
async function findFreePort(start = 5000, max = 5100) {
for (let p = start; p <= max; p++) {
if (await isPortFree(p)) return p;
}
throw new Error("No free ports found");
}
/* ---------------------------
PROFILES
---------------------------- */
function getProfiles() {
const res = spawnSync("verdi", ["profile", "list"], {
encoding: "utf-8",
});
if (res.status !== 0) fail("Failed to list profiles.");
let active = null;
const profiles = res.stdout
.split("\n")
.map((l) => l.trim())
.filter(Boolean)
.map((l) => {
if (l.startsWith("*")) {
active = l.replace(/^\*\s*/, "");
return active;
}
return l;
});
return { profiles, active };
}
async function selectProfile(profiles, active) {
return select({
message: "Select AiiDA profile",
choices: profiles.map((p) => ({
name: p,
value: p,
description: p === active ? "active" : undefined,
})),
default: active,
});
}
/* ---------------------------
REST API
---------------------------- */
function startRestApi(profile) {
console.log(`Starting REST API (${profile}) on ${REST_PORT}...`);
return spawn("verdi", [
"-p",
profile,
"restapi",
"--port",
REST_PORT,
"--verbosity",
"error",
]);
}
/* ---------------------------
WAIT FOR API
---------------------------- */
async function waitForApi() {
const url = `http://localhost:${REST_PORT}/api/v4`;
for (let i = 0; i < 30; i++) {
try {
const res = await fetch(url);
if (res.ok) return url;
} catch {}
await new Promise((r) => setTimeout(r, 1000));
}
fail("REST API did not start in time.");
}
/* ---------------------------
MAIN
---------------------------- */
async function main() {
checkVerdi();
checkRestApiDeps();
const { profiles, active } = getProfiles();
const selectedProfile = await selectProfile(profiles, active);
const restProc = startRestApi(selectedProfile);
const apiUrl = await waitForApi();
const frontend = FRONTEND_URL + `?api_url=${encodeURIComponent(apiUrl)}`;
console.log("\nOpening browser...");
await open(frontend);
function shutdown() {
console.log("\nShutting down...");
restProc.kill("SIGTERM");
process.exit(0);
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
main();