Skip to content

Commit

Permalink
Added createArchiveByFileStat
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 8, 2023
1 parent d5e6878 commit 9af471d
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {stat} from 'node:fs/promises';

import {Archive} from './archive';
import {ArchiveDir} from './archive/dir';
import {ArchiveHdi} from './archive/hdi';
Expand All @@ -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,
Expand Down Expand Up @@ -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<ICreateArchiveOptions> | 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<ICreateArchiveOptions> | null = null
) {
const st = await stat(path);
return st.isDirectory()
? new ArchiveDir(path)
: createArchiveByFileExtension(path, options);
}

0 comments on commit 9af471d

Please sign in to comment.