2020-08-06 09:31:45 +00:00
|
|
|
#!/usr/bin/env node
|
2021-01-21 12:09:50 +00:00
|
|
|
const http = require("https");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const { spawn } = require("child_process");
|
2020-08-06 09:31:45 +00:00
|
|
|
|
2021-01-21 12:09:50 +00:00
|
|
|
const decompress = require("decompress");
|
|
|
|
const decompressTargz = require("decompress-targz");
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
const args = process.argv.slice(2);
|
2021-01-21 12:09:50 +00:00
|
|
|
const version = args[0] || "12.0.1";
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
const decompressKeycloak = () =>
|
|
|
|
decompress(fileName, folder, {
|
2021-01-21 12:09:50 +00:00
|
|
|
plugins: [decompressTargz()],
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
console.log("Files decompressed");
|
|
|
|
})
|
|
|
|
.catch((e) => console.error(e));
|
2020-08-06 09:31:45 +00:00
|
|
|
const run = () => {
|
2021-01-21 12:09:50 +00:00
|
|
|
const proc = spawn(path.join(serverPath, "bin", `standalone${extension}`), [
|
|
|
|
"-Djboss.socket.binding.port-offset=100",
|
|
|
|
]);
|
|
|
|
proc.stdout.on("data", (data) => {
|
2020-08-06 09:31:45 +00:00
|
|
|
console.log(data.toString());
|
|
|
|
});
|
2021-01-21 12:09:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-08-06 09:31:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(fileName)) {
|
|
|
|
const file = fs.createWriteStream(fileName);
|
2021-01-21 12:09:50 +00:00
|
|
|
request(
|
|
|
|
`https://github.com/keycloak/keycloak/releases/download/${version}/keycloak-${version}.tar.gz`,
|
|
|
|
file
|
|
|
|
);
|
2020-08-06 09:31:45 +00:00
|
|
|
} else if (!fs.existsSync(serverPath)) {
|
|
|
|
decompressKeycloak().then(() => run());
|
|
|
|
} else {
|
|
|
|
run();
|
|
|
|
}
|