From 8cd584cbd5b8209f5dbb53345ee9dc11dfe5ac14 Mon Sep 17 00:00:00 2001 From: garronej Date: Sat, 20 Aug 2022 14:56:20 +0700 Subject: [PATCH] Implementation of #160 and #103 for v6 --- src/bin/download-builtin-keycloak-theme.ts | 3 +- src/bin/tools/downloadAndUnzip.ts | 73 +++++++++++++++++----- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/bin/download-builtin-keycloak-theme.ts b/src/bin/download-builtin-keycloak-theme.ts index 758afd67..66dbcdc2 100644 --- a/src/bin/download-builtin-keycloak-theme.ts +++ b/src/bin/download-builtin-keycloak-theme.ts @@ -12,7 +12,8 @@ export function downloadBuiltinKeycloakTheme(params: { keycloakVersion: string; downloadAndUnzip({ "destDirPath": destDirPath, "url": `https://github.com/keycloak/keycloak/archive/refs/tags/${keycloakVersion}.zip`, - "pathOfDirToExtractInArchive": `keycloak-${keycloakVersion}/themes/src/main/resources${ext}/theme` + "pathOfDirToExtractInArchive": `keycloak-${keycloakVersion}/themes/src/main/resources${ext}/theme`, + "cacheDirPath": pathJoin(keycloakThemeBuildingDirPath, ".cache") }); } } diff --git a/src/bin/tools/downloadAndUnzip.ts b/src/bin/tools/downloadAndUnzip.ts index dc4c9aae..1522e327 100644 --- a/src/bin/tools/downloadAndUnzip.ts +++ b/src/bin/tools/downloadAndUnzip.ts @@ -2,31 +2,74 @@ import { basename as pathBasename, join as pathJoin } from "path"; import { execSync } from "child_process"; import * as fs from "fs"; import { transformCodebase } from "./transformCodebase"; -import { rm_rf, rm, rm_r } from "./rm"; +import { rm, rm_rf } from "./rm"; +import * as crypto from "crypto"; /** assert url ends with .zip */ -export function downloadAndUnzip(params: { url: string; destDirPath: string; pathOfDirToExtractInArchive?: string }) { - const { url, destDirPath, pathOfDirToExtractInArchive } = params; +export function downloadAndUnzip(params: { url: string; destDirPath: string; pathOfDirToExtractInArchive?: string; cacheDirPath: string }) { + const { url, destDirPath, pathOfDirToExtractInArchive, cacheDirPath } = params; - const tmpDirPath = pathJoin(destDirPath, "..", "tmp_xxKdOxnEdx"); - const zipFilePath = pathBasename(url); + const extractDirPath = pathJoin( + cacheDirPath, + `_${crypto.createHash("sha256").update(JSON.stringify({ url, pathOfDirToExtractInArchive })).digest("hex").substring(0, 15)}` + ); - rm_rf(tmpDirPath); + fs.mkdirSync(cacheDirPath, { "recursive": true }); - fs.mkdirSync(tmpDirPath, { "recursive": true }); + const { readIsSuccessByExtractDirPath, writeIsSuccessByExtractDirPath } = (() => { + const filePath = pathJoin(cacheDirPath, "isSuccessByExtractDirPath.json"); - execSync(`curl -L ${url} -o ${zipFilePath}`, { "cwd": tmpDirPath }); + type IsSuccessByExtractDirPath = Record; - execSync(`unzip -o ${zipFilePath}${pathOfDirToExtractInArchive === undefined ? "" : ` "${pathOfDirToExtractInArchive}/**/*"`}`, { - "cwd": tmpDirPath - }); + function readIsSuccessByExtractDirPath(): IsSuccessByExtractDirPath { + if (!fs.existsSync(filePath)) { + return {}; + } - rm(pathBasename(url), { "cwd": tmpDirPath }); + return JSON.parse(fs.readFileSync(filePath).toString("utf8")); + } + + function writeIsSuccessByExtractDirPath(isSuccessByExtractDirPath: IsSuccessByExtractDirPath): void { + fs.writeFileSync(filePath, Buffer.from(JSON.stringify(isSuccessByExtractDirPath, null, 2), "utf8")); + } + + return { readIsSuccessByExtractDirPath, writeIsSuccessByExtractDirPath }; + })(); + + downloadAndUnzip: { + const isSuccessByExtractDirPath = readIsSuccessByExtractDirPath(); + + if (isSuccessByExtractDirPath[extractDirPath]) { + break downloadAndUnzip; + } + + writeIsSuccessByExtractDirPath({ + ...isSuccessByExtractDirPath, + [extractDirPath]: false + }); + + rm_rf(extractDirPath); + + fs.mkdirSync(extractDirPath); + + const zipFileBasename = pathBasename(url); + + execSync(`curl -L ${url} -o ${zipFileBasename}`, { "cwd": extractDirPath }); + + execSync(`unzip -o ${zipFileBasename}${pathOfDirToExtractInArchive === undefined ? "" : ` "${pathOfDirToExtractInArchive}/**/*"`}`, { + "cwd": extractDirPath + }); + + rm(zipFileBasename, { "cwd": extractDirPath }); + + writeIsSuccessByExtractDirPath({ + ...isSuccessByExtractDirPath, + [extractDirPath]: true + }); + } transformCodebase({ - "srcDirPath": pathOfDirToExtractInArchive === undefined ? tmpDirPath : pathJoin(tmpDirPath, pathOfDirToExtractInArchive), + "srcDirPath": pathOfDirToExtractInArchive === undefined ? extractDirPath : pathJoin(extractDirPath, pathOfDirToExtractInArchive), destDirPath }); - - rm_r(tmpDirPath); }