forked from edewit/keycloak-admin-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.mjs
executable file
·124 lines (113 loc) · 3.21 KB
/
start.mjs
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
#!/usr/bin/env node
import http from "node:https";
import fs from "node:fs";
import path from "node:path";
import { spawn } from "node:child_process";
import decompress from "decompress";
import ProgressPromise from "progress-promise";
import cliProgress from "cli-progress";
import colors from "colors";
const args = process.argv.slice(2);
const version = args[0] && !args[0].startsWith("-") ? args[0] : "17.0.0";
const folder = "server";
const fileName = path.join(folder, `keycloak-${version}.tar.gz`);
const serverPath = path.join(folder, `keycloak-${version}`);
const extension = process.platform === "win32" ? ".bat" : ".sh";
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
const progressTick = () => {
return new ProgressPromise((resolve, _, progress) => {
for (let i = 0; i < 10; i++) {
setTimeout(() => progress(i * 10), i * 1500);
}
setTimeout(resolve, 15000);
});
};
async function decompressKeycloak() {
try {
const progressBar = new cliProgress.Bar({
format:
"Decompress |" +
colors.cyan("{bar}") +
"| {percentage}% || ETA: {eta}s ",
barCompleteChar: "\u2588",
barIncompleteChar: "\u2591",
});
progressBar.start(100);
await Promise.all([
decompress(fileName, folder),
progressTick()
.progress((value) => progressBar.update(value))
.then(() => {
progressBar.update(100);
progressBar.stop();
console.log("\nFiles decompressed");
}),
]);
} catch (error) {
console.error(error);
}
}
const run = () => {
const proc = spawn(
path.join(serverPath, "bin", `kc${extension}`),
[
"start-dev",
"--http-port=8180",
"-Dkeycloak.profile.feature.admin2=enabled",
"-Dkeycloak.profile.feature.declarative_user_profile=enabled",
...args,
],
{
env: {
KEYCLOAK_ADMIN: "admin",
KEYCLOAK_ADMIN_PASSWORD: "admin",
},
}
);
proc.stdout.on("data", (data) => {
console.log(data.toString());
});
};
const request = (url, file, progressBar) => {
http.get(url, (response) => {
if (response.statusCode === 302) {
request(response.headers.location, file, progressBar);
} else if (response.statusCode === 404) {
throw new Error(`version not found '${version}'`);
} else {
let data = 0;
progressBar.start(parseInt(response.headers["content-length"]), 0);
response.pipe(file);
response.on("data", (chunk) => {
progressBar.update(data);
data += chunk.length;
});
response.on("end", () => {
console.log("\nDownloaded keycloak");
decompressKeycloak().then(() => run());
});
}
});
};
if (!fs.existsSync(fileName)) {
const file = fs.createWriteStream(fileName);
const progressBar = new cliProgress.Bar({
format:
"Download |" +
colors.cyan("{bar}") +
"| {percentage}% || {value}/{total} Chunks",
barCompleteChar: "\u2588",
barIncompleteChar: "\u2591",
});
request(
`https://github.com/keycloak/keycloak/releases/download/${version}/keycloak-${version}.tar.gz`,
file,
progressBar
);
} else if (!fs.existsSync(serverPath)) {
decompressKeycloak().then(() => run());
} else {
run();
}