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);
|
2022-02-15 13:24:56 +00:00
|
|
|
const version = args[0] && !args[0].startsWith("-") ? args[0] : "17.0.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 = () => {
|
2022-03-06 15:25:37 +00:00
|
|
|
const proc = spawn(
|
|
|
|
path.join(serverPath, "bin", `kc${extension}`),
|
|
|
|
[
|
|
|
|
"start-dev",
|
|
|
|
"--http-port=8180",
|
2022-03-25 12:12:49 +00:00
|
|
|
"--features=admin2,admin-fine-grained-authz,declarative-user-profile",
|
2021-11-15 07:28:55 +00:00
|
|
|
...args,
|
2022-03-06 15:25:37 +00:00
|
|
|
],
|
|
|
|
{
|
|
|
|
env: {
|
|
|
|
KEYCLOAK_ADMIN: "admin",
|
|
|
|
KEYCLOAK_ADMIN_PASSWORD: "admin",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
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(
|
2022-03-06 15:25:37 +00:00
|
|
|
`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();
|
|
|
|
}
|