Implementation of #160 and #103 for v6

This commit is contained in:
garronej 2022-08-20 14:56:20 +07:00
parent f5b87f4669
commit 8cd584cbd5
2 changed files with 60 additions and 16 deletions

View File

@ -12,7 +12,8 @@ export function downloadBuiltinKeycloakTheme(params: { keycloakVersion: string;
downloadAndUnzip({ downloadAndUnzip({
"destDirPath": destDirPath, "destDirPath": destDirPath,
"url": `https://github.com/keycloak/keycloak/archive/refs/tags/${keycloakVersion}.zip`, "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")
}); });
} }
} }

View File

@ -2,31 +2,74 @@ import { basename as pathBasename, join as pathJoin } from "path";
import { execSync } from "child_process"; import { execSync } from "child_process";
import * as fs from "fs"; import * as fs from "fs";
import { transformCodebase } from "./transformCodebase"; 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 */ /** assert url ends with .zip */
export function downloadAndUnzip(params: { url: string; destDirPath: string; pathOfDirToExtractInArchive?: string }) { export function downloadAndUnzip(params: { url: string; destDirPath: string; pathOfDirToExtractInArchive?: string; cacheDirPath: string }) {
const { url, destDirPath, pathOfDirToExtractInArchive } = params; const { url, destDirPath, pathOfDirToExtractInArchive, cacheDirPath } = params;
const tmpDirPath = pathJoin(destDirPath, "..", "tmp_xxKdOxnEdx"); const extractDirPath = pathJoin(
const zipFilePath = pathBasename(url); 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<string, boolean | undefined>;
execSync(`unzip -o ${zipFilePath}${pathOfDirToExtractInArchive === undefined ? "" : ` "${pathOfDirToExtractInArchive}/**/*"`}`, { function readIsSuccessByExtractDirPath(): IsSuccessByExtractDirPath {
"cwd": tmpDirPath 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({ transformCodebase({
"srcDirPath": pathOfDirToExtractInArchive === undefined ? tmpDirPath : pathJoin(tmpDirPath, pathOfDirToExtractInArchive), "srcDirPath": pathOfDirToExtractInArchive === undefined ? extractDirPath : pathJoin(extractDirPath, pathOfDirToExtractInArchive),
destDirPath destDirPath
}); });
rm_r(tmpDirPath);
} }