diff --git a/.eslintrc.json b/.eslintrc.json index a93ac25b04..79b8cc6ff4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,5 +1,6 @@ { "env": { + "node":true, "browser": true, "es6": true }, diff --git a/.github/workflows/cypress.yml b/.github/workflows/cypress.yml index dc0821acb0..601e59dcde 100644 --- a/.github/workflows/cypress.yml +++ b/.github/workflows/cypress.yml @@ -27,13 +27,13 @@ jobs: - run: npm run build - name: Run Keycloak - run: ./start.js & sleep 40 + run: ./start.mjs & sleep 40 - name: Run Admin Console run: npx http-server ./build -P http://localhost:8180/ & sleep 30 - name: Admin Console client - run: ./import.js + run: ./import.mjs - name: Cypress run run: npm run start:cypress-tests diff --git a/Dockerfile b/Dockerfile index 02814bd567..a6a4138e29 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ WORKDIR /app COPY . . # replace Keycloak endpoints -RUN sed -i "s/${DEFAULT_KEYCLOAK_ENDPOINT}/${KEYCLOAK_ENDPOINT}/g" import.js &&\ +RUN sed -i "s/${DEFAULT_KEYCLOAK_ENDPOINT}/${KEYCLOAK_ENDPOINT}/g" import.mjs &&\ sed -i "s/${DEFAULT_KEYCLOAK_ENDPOINT}/${KEYCLOAK_ENDPOINT}/g" src/context/auth/keycloak.ts &&\ sed -i "s/adminv2//g" snowpack.config.js diff --git a/README.md b/README.md index e15b6f61eb..a3a35a2b85 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ For development on this project you will need a running Keycloak server listenin 1. Start keycloak * Download and run with one command ```bash - $> ./start.js + $> ./start.mjs ``` * or download Keycloak server from [keycloak downloads page][2] unpack and run it like: ```bash @@ -19,7 +19,7 @@ For development on this project you will need a running Keycloak server listenin * like this: ![realm settings](./realm-settings.png "Realm Settings") * or click on the "Select file" button and import `security-admin-console-v2.json` - * or run `$> ./import.js` + * or run `$> ./import.mjs` 1. Install dependencies and run: ```bash diff --git a/import.js b/import.js deleted file mode 100755 index 274ce3a5db..0000000000 --- a/import.js +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env node -const KcAdminClient = require('keycloak-admin').default; -const consoleClientConfig = require("./security-admin-console-v2.json"); - - -const adminClient = new KcAdminClient({ - baseUrl: "http://localhost:8180/auth", - realmName: 'master', -}); - -(async () => { - await adminClient.auth({ - username: 'admin', - password: 'admin', - grantType: 'password', - clientId: 'admin-cli', - }); - - const adminConsoleClient = await adminClient.clients.find({clientId: "security-admin-console-v2"}); - if (adminConsoleClient.length === 0) { - adminClient.clients.create(consoleClientConfig); - } -})(); \ No newline at end of file diff --git a/import.mjs b/import.mjs new file mode 100755 index 0000000000..60ce409aad --- /dev/null +++ b/import.mjs @@ -0,0 +1,27 @@ +#!/usr/bin/env node +import { readFile } from "node:fs/promises"; +import KcAdminClient from "keycloak-admin"; + +const consoleClientConfig = JSON.parse( + await readFile(new URL("./security-admin-console-v2.json", import.meta.url)) +); + +const adminClient = new KcAdminClient.default({ + baseUrl: "http://localhost:8180/auth", + realmName: "master", +}); + +await adminClient.auth({ + username: "admin", + password: "admin", + grantType: "password", + clientId: "admin-cli", +}); + +const adminConsoleClient = await adminClient.clients.find({ + clientId: "security-admin-console-v2", +}); + +if (adminConsoleClient.length === 0) { + adminClient.clients.create(consoleClientConfig); +} diff --git a/package.json b/package.json index 13164d849d..2ade787030 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "scripts": { "build": "snowpack build", - "lint": "eslint \"./src/**/*.{js,jsx,ts,tsx}\"", + "lint": "eslint \"./src/**/*.{js,jsx,mjs,ts,tsx}\"", "postinstall": "grunt", "start": "snowpack dev", "test": "jest", @@ -93,6 +93,6 @@ "typescript": "4.2.4" }, "lint-staged": { - "*.{js,jsx,ts,tsx}": "eslint --cache --fix" + "*.{js,jsx,mjs,ts,tsx}": "eslint --cache --fix" } } diff --git a/start.js b/start.mjs similarity index 68% rename from start.js rename to start.mjs index 6efe015b79..7ac23df519 100755 --- a/start.js +++ b/start.mjs @@ -1,11 +1,11 @@ #!/usr/bin/env node -const http = require("https"); -const fs = require("fs"); -const path = require("path"); -const { spawn } = require("child_process"); +import http from "node:https"; +import fs from "node:fs"; +import path from "node:path"; +import { spawn } from "node:child_process"; -const decompress = require("decompress"); -const decompressTargz = require("decompress-targz"); +import decompress from "decompress"; +import decompressTargz from "decompress-targz"; const args = process.argv.slice(2); const version = args[0] || "12.0.1"; @@ -19,19 +19,24 @@ if (!fs.existsSync(folder)) { fs.mkdirSync(folder); } -const decompressKeycloak = () => - decompress(fileName, folder, { - plugins: [decompressTargz()], - }) - .then(() => { - console.log("Files decompressed"); - }) - .catch((e) => console.error(e)); +async function decompressKeycloak() { + try { + await decompress(fileName, folder, { + plugins: [decompressTargz()], + }); + + console.log("Files decompressed"); + } catch (error) { + console.error(error); + } +} + const run = () => { - const addProc = spawn(path.join(serverPath, "bin", `add-user-keycloak${extension}`), [ - "--user", "admin", - "--password", "admin" - ]); + 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",