keycloak-scim/start.mjs

76 lines
1.9 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 decompressTargz from "decompress-targz";
const args = process.argv.slice(2);
2021-08-03 08:48:56 +00:00
const version = args[0] || "15.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);
}
2021-07-15 20:56:25 +00:00
async function decompressKeycloak() {
try {
await decompress(fileName, folder, {
plugins: [decompressTargz()],
});
console.log("Files decompressed");
} catch (error) {
console.error(error);
}
}
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"]
);
addProc.on("exit", () => {
const proc = spawn(path.join(serverPath, "bin", `standalone${extension}`), [
"-Djboss.socket.binding.port-offset=100",
"-Dprofile.feature.newadmin=enabled",
]);
proc.stdout.on("data", (data) => {
console.log(data.toString());
});
});
};
const request = (url, file) => {
http.get(url, (response) => {
if (response.statusCode == 302) {
request(response.headers.location, file);
} else {
response.pipe(file);
response.on("end", () => {
console.log("Downloaded keycloak");
decompressKeycloak().then(() => run());
});
}
});
};
if (!fs.existsSync(fileName)) {
const file = fs.createWriteStream(fileName);
request(
`https://github.com/keycloak/keycloak/releases/download/${version}/keycloak-${version}.tar.gz`,
file
);
} else if (!fs.existsSync(serverPath)) {
decompressKeycloak().then(() => run());
} else {
run();
}