keycloak_theme/src/bin/tools/downloadAndUnzip.ts

81 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-03-03 02:31:02 +01:00
import { basename as pathBasename, join as pathJoin } from "path";
import { execSync } from "child_process";
import * as fs from "fs";
import { transformCodebase } from "./transformCodebase";
2022-08-20 14:56:20 +07:00
import * as crypto from "crypto";
2021-02-28 18:40:57 +01:00
2021-03-03 02:31:02 +01:00
/** assert url ends with .zip */
2022-09-08 12:06:26 +03:00
export function downloadAndUnzip(params: {
isSilent: boolean;
url: string;
destDirPath: string;
pathOfDirToExtractInArchive?: string;
cacheDirPath: string;
}) {
2022-08-20 14:56:20 +07:00
const { url, destDirPath, pathOfDirToExtractInArchive, cacheDirPath } = params;
2021-02-28 18:40:57 +01:00
2022-08-20 14:56:20 +07:00
const extractDirPath = pathJoin(
cacheDirPath,
`_${crypto.createHash("sha256").update(JSON.stringify({ url, pathOfDirToExtractInArchive })).digest("hex").substring(0, 15)}`
);
2021-02-28 18:40:57 +01:00
2022-08-20 14:56:20 +07:00
fs.mkdirSync(cacheDirPath, { "recursive": true });
2021-03-03 02:31:02 +01:00
2022-08-20 14:56:20 +07:00
const { readIsSuccessByExtractDirPath, writeIsSuccessByExtractDirPath } = (() => {
const filePath = pathJoin(cacheDirPath, "isSuccessByExtractDirPath.json");
2022-08-20 14:56:20 +07:00
type IsSuccessByExtractDirPath = Record<string, boolean | undefined>;
2022-08-20 14:56:20 +07:00
function readIsSuccessByExtractDirPath(): IsSuccessByExtractDirPath {
if (!fs.existsSync(filePath)) {
return {};
}
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
});
2021-03-03 02:31:02 +01:00
2023-01-08 14:57:18 +01:00
fs.rmSync(extractDirPath, { "recursive": true, "force": true });
2022-08-20 14:56:20 +07:00
fs.mkdirSync(extractDirPath);
const zipFileBasename = pathBasename(url);
2022-09-08 12:06:26 +03:00
execSync(`curl -L ${url} -o ${zipFileBasename} ${params.isSilent ? "-s" : ""}`, { "cwd": extractDirPath });
2022-08-20 14:56:20 +07:00
execSync(`unzip -o ${zipFileBasename}${pathOfDirToExtractInArchive === undefined ? "" : ` "${pathOfDirToExtractInArchive}/**/*"`}`, {
"cwd": extractDirPath
});
2023-01-08 14:57:18 +01:00
fs.rmSync(pathJoin(extractDirPath, zipFileBasename), { "recursive": true, "force": true });
2022-08-20 14:56:20 +07:00
writeIsSuccessByExtractDirPath({
...isSuccessByExtractDirPath,
[extractDirPath]: true
});
}
2021-03-03 02:31:02 +01:00
transformCodebase({
2022-08-20 14:56:20 +07:00
"srcDirPath": pathOfDirToExtractInArchive === undefined ? extractDirPath : pathJoin(extractDirPath, pathOfDirToExtractInArchive),
destDirPath
2021-03-03 02:31:02 +01:00
});
}