keycloak-scim/start.mjs

124 lines
3.2 KiB
JavaScript
Raw Normal View History

#!/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";
2021-07-15 20:56:25 +00:00
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);
});
};
2021-07-15 20:56:25 +00:00
async function decompressKeycloak() {
try {
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
});
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);
}
}
const run = () => {
const proc = spawn(
path.join(serverPath, "bin", `kc${extension}`),
[
"start-dev",
"--http-port=8180",
"--features=admin2,admin-fine-grained-authz,declarative-user-profile",
...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();
}