From 9af471d9b7c608f5c92a46575cb2ced0a8918af6 Mon Sep 17 00:00:00 2001 From: JrMasterModelBuilder Date: Sat, 7 Oct 2023 21:37:14 -0400 Subject: [PATCH] Added createArchiveByFileStat --- src/create.ts | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/create.ts b/src/create.ts index 3c36f8c..f32ca8c 100644 --- a/src/create.ts +++ b/src/create.ts @@ -1,3 +1,5 @@ +import {stat} from 'node:fs/promises'; + import {Archive} from './archive'; import {ArchiveDir} from './archive/dir'; import {ArchiveHdi} from './archive/hdi'; @@ -6,6 +8,15 @@ import {ArchiveTarBz2} from './archive/tar/bz2'; import {ArchiveTarGz} from './archive/tar/gz'; import {ArchiveZip} from './archive/zip'; +export interface ICreateArchiveOptions { + /** + * Set the nobrowse option on mounted disk images. + * + * @default false + */ + nobrowse?: boolean; +} + const archives: (typeof Archive)[] = [ ArchiveDir, ArchiveHdi, @@ -60,20 +71,47 @@ function archivesExtensions() { } /** - * Create an Archive instance for a given path, based on file extension. + * Create an Archive instance for a given path. + * Based on file extension. * * @param path File path. + * @param options Optional options. * @returns Archive instance or null. */ -export function createArchiveByFileExtension(path: string) { +export function createArchiveByFileExtension( + path: string, + options: Readonly | null = null +) { const pathLower = path.toLowerCase(); const list = archivesExtensions(); for (const {Archive, ext} of list) { if (pathLower.endsWith(ext)) { - return new (Archive as unknown as new (path: string) => Archive)( + const a = new (Archive as unknown as new (path: string) => Archive)( path ); + if (options && a instanceof ArchiveHdi) { + a.nobrowse = options.nobrowse ?? false; + } + return a; } } return null; } + +/** + * Create an Archive instance for a given path. + * Based on file extension or if a directory. + * + * @param path File path. + * @param options Optional options. + * @returns Archive instance or null. + */ +export async function createArchiveByFileStat( + path: string, + options: Readonly | null = null +) { + const st = await stat(path); + return st.isDirectory() + ? new ArchiveDir(path) + : createArchiveByFileExtension(path, options); +}