kaven-utils
Version:
Utils for Node.js.
139 lines (138 loc) • 4.96 kB
JavaScript
/********************************************************************
* @author: Kaven
* @email: kaven@wuwenkai.com
* @website: http://blog.kaven.xyz
* @file: [Kaven-Utils] /src/KavenUtility.Archive.ts
* @create: 2020-07-29 13:57:23.481
* @modify: 2025-10-11 23:39:02.978
* @version: 6.0.5
* @times: 59
* @lines: 159
* @copyright: Copyright © 2020-2025 Kaven. All Rights Reserved.
* @description: [description]
* @license: [license]
********************************************************************/
import { CombinePath, Strings_DoubleQuotes, Strings_WhiteSpace, SurroundBy } from "kaven-basic";
import { isAbsolute } from "node:path";
import { Execute } from "./KavenUtility.ChildProcess.js";
import { DeleteFile, IsDirectory, IsPathExistSync } from "./KavenUtility.FileSystem.js";
/**
* @since 6.0.5
* @version 2025-10-11
*/
export function Build7zCommand(command, baseArchiveName, switches, ...args) {
const switchList = [];
if (switches) {
for (const key of Object.keys(switches)) {
if (switches[key]) {
switchList.push(`${key}${switches[key]}`);
}
else {
switchList.push(key);
}
}
}
const cmd = `7z ${command} ${switchList.join(Strings_WhiteSpace)} ${baseArchiveName} ${args.join(Strings_WhiteSpace)}`;
return cmd;
}
/**
* This function calls the 7z binary, it should be available in your PATH.
* @since 6.0.5
* @version 2025-10-11
*/
export async function Invoke7zCommand(command, baseArchiveName, switches, args, options) {
const cmd = Build7zCommand(command, SurroundBy(baseArchiveName, Strings_DoubleQuotes), switches, ...args ?? []);
return await Execute(cmd, options);
}
/**
* This function calls the 7z binary, it should be available in your PATH.
* @since 5.3.0
* @version 2025-10-11
* @example
* CreateArchive("./dist/", "dist.7z")
* CreateArchive("./dist/*", "dist.7z")
*
* CreateArchive("./dist/", "dist.zip")
* CreateArchive("./dist/*", "dist.zip")
*/
export async function CreateArchive(input, outputFile, options, executeOptions) {
const sourceList = typeof input === "string" ? [input] : input;
const switches = {};
if (options) {
if (options.compressionLevel !== undefined) {
switches["-mx"] = options.compressionLevel.toString();
}
if (options.password !== undefined) {
switches["-p"] = SurroundBy(options.password, Strings_DoubleQuotes);
}
if (!options.notRecurseSubdirectories) {
switches["-r"] = undefined;
}
if (options.archiveType) {
switches["-t"] = options.archiveType;
}
}
const inputList = [];
for (let item of sourceList) {
if (!item.endsWith("*") && isAbsolute(item) && await IsDirectory(item)) {
item = CombinePath(item, "*");
}
item = SurroundBy(item, Strings_DoubleQuotes);
inputList.push(item);
}
if (IsPathExistSync(outputFile)) {
if (options?.overwriteFile) {
await DeleteFile(outputFile);
}
else {
throw new Error(`File ${outputFile} already exists`);
}
}
await Invoke7zCommand("a", outputFile, switches, inputList, executeOptions);
return outputFile;
}
/**
* This function calls the 7z binary, it should be available in your PATH.
* @since 5.3.0
* @version 2025-10-11
* @example
* Create7zArchive("./dist/", "dist.7z")
* Create7zArchive("./dist/*", "dist.7z")
*/
export async function Create7zArchive(inputFiles, outputFile, options, executeOptions) {
options ??= {};
options.archiveType = "7z";
return await CreateArchive(inputFiles, outputFile, options, executeOptions);
}
/**
* This function calls the 7z binary, it should be available in your PATH.
* @since 5.3.0
* @version 2025-10-11
* @example
* CreateZipArchive("./dist/", "dist.zip")
* CreateZipArchive("./dist/*", "dist.zip")
*/
export async function CreateZipArchive(inputFiles, outputFile, options, executeOptions) {
options ??= {};
options.archiveType = "zip";
return await CreateArchive(inputFiles, outputFile, options, executeOptions);
}
/**
* This function calls the 7z binary, it should be available in your PATH.
* @since 6.0.5
* @version 2025-10-11
*/
export async function ExtractArchive(archiveFile, options, executeOptions) {
const switches = {};
if (options) {
if (options?.outputDirectory) {
switches["-o"] = SurroundBy(options.outputDirectory, Strings_DoubleQuotes);
}
if (options?.password) {
switches["-p"] = SurroundBy(options.password, Strings_DoubleQuotes);
}
}
// assume Yes on all queries
switches["-y"] = undefined;
return await Invoke7zCommand("x", archiveFile, switches, undefined, executeOptions);
}