From b9bcd4362e047be528a0e10be575af119b9d01c3 Mon Sep 17 00:00:00 2001 From: JrMasterModelBuilder Date: Sat, 7 Oct 2023 22:01:10 -0400 Subject: [PATCH] Create functions that throw --- src/create.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/create.ts b/src/create.ts index 611fdb0..b8c140e 100644 --- a/src/create.ts +++ b/src/create.ts @@ -98,6 +98,25 @@ export function createArchiveByFileExtension( return null; } +/** + * Create an Archive instance for a given path. + * Based on file extension. + * + * @param path File path. + * @param options Optional options. + * @returns Archive instance. + */ +export function createArchiveByFileExtensionOrThrow( + path: string, + options: Readonly | null = null +) { + const a = createArchiveByFileExtension(path, options); + if (!a) { + throw new Error(`Unsupported archive format: ${path}`); + } + return null; +} + /** * Create an Archive instance for a given path. * Based on file extension or if a directory. @@ -118,3 +137,21 @@ export async function createArchiveByFileStat( ? new ArchiveDir(path) : createArchiveByFileExtension(path, options); } + +/** + * 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. + */ +export async function createArchiveByFileStatOrThrow( + path: string, + options: Readonly | null = null +) { + const st = await stat(path); + return st.isDirectory() + ? new ArchiveDir(path) + : createArchiveByFileExtensionOrThrow(path, options); +}