2020-08-06 09:31:45 +00:00
|
|
|
#!/usr/bin/env node
|
2021-07-15 20:56:25 +00:00
|
|
|
import http from "node:https";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import path from "node:path";
|
|
|
|
import { spawn } from "node:child_process";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
2021-07-15 20:56:25 +00:00
|
|
|
import decompress from "decompress";
|
2021-11-15 07:28:55 +00:00
|
|
|
|
|
|
|
import ProgressPromise from "progress-promise";
|
|
|
|
import cliProgress from "cli-progress";
|
|
|
|
import colors from "colors";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
const args = process.argv.slice(2);
|
2021-12-15 13:00:30 +00:00
|
|
|
const version = args[0] && !args[0].startsWith("-") ? args[0] : "15.1.0";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
2021-01-21 12:09:50 +00:00
|
|
|
const folder = "server";
|
|
|
|
const fileName = path.join(folder, `keycloak-${version}.tar.gz`);
|
2020-08-06 09:31:45 +00:00
|
|
|
const serverPath = path.join(folder, `keycloak-${version}`);
|
2021-01-21 12:09:50 +00:00
|
|
|
const extension = process.platform === "win32" ? ".bat" : ".sh";
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(folder)) {
|
|
|
|
fs.mkdirSync(folder);
|
|
|
|
}
|
|
|
|
|
2021-11-15 07:28:55 +00:00
|
|
|
const progressTick = () => {
|
|
|
|
return new ProgressPromise((resolve, _, progress) => {
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
setTimeout(() => progress(i * 10), i * 1500);
|
|
|
|
}
|
|
|
|
setTimeout(resolve, 15000);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-15 20:56:25 +00:00
|
|
|
async function decompressKeycloak() {
|
|
|
|
try {
|
2021-11-15 07:28:55 +00:00
|
|
|
const progressBar = new cliProgress.Bar({
|
|
|
|
format:
|
|
|
|
"Decompress |" +
|
|
|
|
colors.cyan("{bar}") +
|
|
|
|
"| {percentage}% || ETA: {eta}s ",
|
|
|
|
barCompleteChar: "\u2588",
|
|
|
|
barIncompleteChar: "\u2591",
|
2021-07-15 20:56:25 +00:00
|
|
|
});
|
2021-11-15 07:28:55 +00:00
|
|
|
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");
|
|
|
|
}),
|
|
|
|
]);
|
2021-07-15 20:56:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 09:31:45 +00:00
|
|
|
const run = () => {
|
2021-07-15 20:56:25 +00:00
|
|
|
const addProc = spawn(
|
|
|
|
path.join(serverPath, "bin", `add-user-keycloak${extension}`),
|
|
|
|
["--user", "admin", "--password", "admin"]
|
|
|
|
);
|
|
|
|
|
2021-01-28 15:04:48 +00:00
|
|
|
addProc.on("exit", () => {
|
|
|
|
const proc = spawn(path.join(serverPath, "bin", `standalone${extension}`), [
|
|
|
|
"-Djboss.socket.binding.port-offset=100",
|
2021-07-16 08:51:09 +00:00
|
|
|
"-Dprofile.feature.newadmin=enabled",
|
2021-11-15 07:28:55 +00:00
|
|
|
...args,
|
2021-01-28 15:04:48 +00:00
|
|
|
]);
|
|
|
|
proc.stdout.on("data", (data) => {
|
|
|
|
console.log(data.toString());
|
|
|
|
});
|
2020-08-06 09:31:45 +00:00
|
|
|
});
|
2021-01-21 12:09:50 +00:00
|
|
|
};
|
|
|
|
|
2021-11-15 07:28:55 +00:00
|
|
|
const request = (url, file, progressBar) => {
|
2021-01-21 12:09:50 +00:00
|
|
|
http.get(url, (response) => {
|
2021-11-15 07:28:55 +00:00
|
|
|
if (response.statusCode === 302) {
|
|
|
|
request(response.headers.location, file, progressBar);
|
|
|
|
} else if (response.statusCode === 404) {
|
|
|
|
throw new Error(`version not found '${version}'`);
|
2021-01-21 12:09:50 +00:00
|
|
|
} else {
|
2021-11-15 07:28:55 +00:00
|
|
|
let data = 0;
|
|
|
|
progressBar.start(parseInt(response.headers["content-length"]), 0);
|
2021-01-21 12:09:50 +00:00
|
|
|
response.pipe(file);
|
2021-11-15 07:28:55 +00:00
|
|
|
response.on("data", (chunk) => {
|
|
|
|
progressBar.update(data);
|
|
|
|
data += chunk.length;
|
|
|
|
});
|
2021-01-21 12:09:50 +00:00
|
|
|
response.on("end", () => {
|
2021-11-15 07:28:55 +00:00
|
|
|
console.log("\nDownloaded keycloak");
|
2021-01-21 12:09:50 +00:00
|
|
|
decompressKeycloak().then(() => run());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(fileName)) {
|
|
|
|
const file = fs.createWriteStream(fileName);
|
2021-11-15 07:28:55 +00:00
|
|
|
const progressBar = new cliProgress.Bar({
|
|
|
|
format:
|
|
|
|
"Download |" +
|
|
|
|
colors.cyan("{bar}") +
|
|
|
|
"| {percentage}% || {value}/{total} Chunks",
|
|
|
|
barCompleteChar: "\u2588",
|
|
|
|
barIncompleteChar: "\u2591",
|
|
|
|
});
|
|
|
|
|
2021-01-21 12:09:50 +00:00
|
|
|
request(
|
|
|
|
`https://github.com/keycloak/keycloak/releases/download/${version}/keycloak-${version}.tar.gz`,
|
2021-11-15 07:28:55 +00:00
|
|
|
file,
|
|
|
|
progressBar
|
2021-01-21 12:09:50 +00:00
|
|
|
);
|
2020-08-06 09:31:45 +00:00
|
|
|
} else if (!fs.existsSync(serverPath)) {
|
|
|
|
decompressKeycloak().then(() => run());
|
|
|
|
} else {
|
|
|
|
run();
|
|
|
|
}
|