Skip to content

Commit

Permalink
Create functions that throw
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 8, 2023
1 parent a7b0d3d commit b9bcd43
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICreateArchiveOptions> | 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.
Expand All @@ -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<ICreateArchiveOptions> | null = null
) {
const st = await stat(path);
return st.isDirectory()
? new ArchiveDir(path)
: createArchiveByFileExtensionOrThrow(path, options);
}

0 comments on commit b9bcd43

Please sign in to comment.