keycloak_theme/src/bin/tools/downloadAndUnzip.ts

44 lines
1.2 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";
2021-02-28 18:40:57 +01:00
import fs from "fs";
import { transformCodebase } from "../tools/transformCodebase";
import { rm_rf, rm, rm_r } from "./rm";
2021-02-28 18:40:57 +01:00
2021-03-03 02:31:02 +01:00
/** assert url ends with .zip */
export function downloadAndUnzip(params: {
url: string;
destDirPath: string;
pathOfDirToExtractInArchive?: string;
}) {
const { url, destDirPath, pathOfDirToExtractInArchive } = params;
2021-02-28 18:40:57 +01:00
2021-03-03 02:31:02 +01:00
const tmpDirPath = pathJoin(destDirPath, "..", "tmp_xxKdOxnEdx");
2021-02-28 18:40:57 +01:00
2021-07-21 22:42:00 +02:00
rm_rf(tmpDirPath);
2021-03-03 02:31:02 +01:00
fs.mkdirSync(tmpDirPath, { "recursive": true });
execSync(`wget ${url}`, { "cwd": tmpDirPath });
execSync(
`unzip ${pathBasename(url)}${
pathOfDirToExtractInArchive === undefined
? ""
: ` "${pathOfDirToExtractInArchive}/*"`
}`,
{ "cwd": tmpDirPath },
);
2021-03-03 02:31:02 +01:00
2021-07-21 22:42:00 +02:00
rm(pathBasename(url), { "cwd": tmpDirPath });
2021-03-03 02:31:02 +01:00
transformCodebase({
"srcDirPath":
pathOfDirToExtractInArchive === undefined
? tmpDirPath
: pathJoin(tmpDirPath, pathOfDirToExtractInArchive),
destDirPath,
2021-03-03 02:31:02 +01:00
});
2021-07-21 22:42:00 +02:00
rm_r(tmpDirPath);
}