{"version":3,"file":"create.mjs","names":["stat","ArchiveDir","ArchiveHdi","ArchiveTar","ArchiveTarBz2","ArchiveTarGz","ArchiveZip","archives","archivesExtensionsCache","archivesExtensions","all","Archive","FILE_EXTENSIONS","ext","push","toLowerCase","sort","a","b","length","createArchiveByFileExtension","path","options","pathLower","list","endsWith","nobrowse","createArchiveByFileExtensionOrThrow","Error","createArchiveByFileStat","st","catch","isDirectory","createArchiveByFileStatOrThrow"],"sources":["../src/create.ts"],"sourcesContent":["import {stat} from 'node:fs/promises';\n\nimport {Archive} from './archive.ts';\nimport {ArchiveDir} from './archive/dir.ts';\nimport {ArchiveHdi} from './archive/hdi.ts';\nimport {ArchiveTar} from './archive/tar.ts';\nimport {ArchiveTarBz2} from './archive/tar/bz2.ts';\nimport {ArchiveTarGz} from './archive/tar/gz.ts';\nimport {ArchiveZip} from './archive/zip.ts';\n\nexport interface ICreateArchiveOptions {\n\t/**\n\t * Set the nobrowse option on mounted disk images.\n\t *\n\t * @default false\n\t */\n\tnobrowse?: boolean;\n}\n\nconst archives: (typeof Archive)[] = [\n\tArchiveDir,\n\tArchiveHdi,\n\tArchiveTar,\n\tArchiveTarBz2,\n\tArchiveTarGz,\n\tArchiveZip\n];\n\ninterface IArchiveExt {\n\t/**\n\t * Archive constructor.\n\t */\n\tArchive: typeof Archive;\n\n\t/**\n\t * File extension.\n\t */\n\text: string;\n}\n\nlet archivesExtensionsCache: IArchiveExt[] | null = null;\n\n/**\n * Get all archive and extension pairs, ordered longest to shortest.\n *\n * @returns List of archive and extenion pairs.\n */\nfunction archivesExtensions() {\n\tif (archivesExtensionsCache) {\n\t\treturn archivesExtensionsCache;\n\t}\n\n\t// List all the extension and archive pairs.\n\tconst all: IArchiveExt[] = [];\n\tfor (const Archive of archives) {\n\t\tconst {FILE_EXTENSIONS} = Archive;\n\t\tif (!FILE_EXTENSIONS) {\n\t\t\tcontinue;\n\t\t}\n\t\tfor (const ext of FILE_EXTENSIONS) {\n\t\t\tall.push({\n\t\t\t\tArchive,\n\t\t\t\text: ext.toLowerCase()\n\t\t\t});\n\t\t}\n\t}\n\n\t// Match longest extensions first.\n\tall.sort((a, b) => b.ext.length - a.ext.length);\n\treturn (archivesExtensionsCache = all);\n}\n\n/**\n * Create an Archive instance for a given path.\n * Based on file extension.\n *\n * @param path File path.\n * @param options Optional options.\n * @returns Archive instance or null.\n */\nexport function createArchiveByFileExtension(\n\tpath: string,\n\toptions: Readonly<ICreateArchiveOptions> | null = null\n) {\n\tconst pathLower = path.toLowerCase();\n\tconst list = archivesExtensions();\n\tfor (const {Archive, ext} of list) {\n\t\tif (pathLower.endsWith(ext)) {\n\t\t\tconst a = new (Archive as unknown as new (path: string) => Archive)(\n\t\t\t\tpath\n\t\t\t);\n\t\t\tif (options && a instanceof ArchiveHdi) {\n\t\t\t\ta.nobrowse = options.nobrowse ?? false;\n\t\t\t}\n\t\t\treturn a;\n\t\t}\n\t}\n\treturn null;\n}\n\n/**\n * Create an Archive instance for a given path.\n * Based on file extension.\n *\n * @param path File path.\n * @param options Optional options.\n * @returns Archive instance.\n */\nexport function createArchiveByFileExtensionOrThrow(\n\tpath: string,\n\toptions: Readonly<ICreateArchiveOptions> | null = null\n) {\n\tconst a = createArchiveByFileExtension(path, options);\n\tif (!a) {\n\t\tthrow new Error(`Unsupported archive format: ${path}`);\n\t}\n\treturn a;\n}\n\n/**\n * Create an Archive instance for a given path.\n * Based on file extension or if a directory.\n *\n * @param path File path.\n * @param options Optional options.\n * @returns Archive instance or null.\n */\nexport async function createArchiveByFileStat(\n\tpath: string,\n\toptions: Readonly<ICreateArchiveOptions> | null = null\n) {\n\tconst st = await stat(path).catch(() => null);\n\tif (!st) {\n\t\treturn null;\n\t}\n\treturn st.isDirectory()\n\t\t? new ArchiveDir(path)\n\t\t: createArchiveByFileExtension(path, options);\n}\n\n/**\n * Create an Archive instance for a given path.\n * Based on file extension or if a directory.\n *\n * @param path File path.\n * @param options Optional options.\n * @returns Archive instance.\n */\nexport async function createArchiveByFileStatOrThrow(\n\tpath: string,\n\toptions: Readonly<ICreateArchiveOptions> | null = null\n) {\n\tconst st = await stat(path);\n\treturn st.isDirectory()\n\t\t? new ArchiveDir(path)\n\t\t: createArchiveByFileExtensionOrThrow(path, options);\n}\n"],"mappings":"AAAA,SAAQA,IAAI,QAAO,kBAAkB;AAGrC,SAAQC,UAAU,QAAO,mBAAkB;AAC3C,SAAQC,UAAU,QAAO,mBAAkB;AAC3C,SAAQC,UAAU,QAAO,mBAAkB;AAC3C,SAAQC,aAAa,QAAO,uBAAsB;AAClD,SAAQC,YAAY,QAAO,sBAAqB;AAChD,SAAQC,UAAU,QAAO,mBAAkB;AAW3C,MAAMC,QAA4B,GAAG,CACpCN,UAAU,EACVC,UAAU,EACVC,UAAU,EACVC,aAAa,EACbC,YAAY,EACZC,UAAU,CACV;AAcD,IAAIE,uBAA6C,GAAG,IAAI;;AAExD;AACA;AACA;AACA;AACA;AACA,SAASC,kBAAkBA,CAAA,EAAG;EAC7B,IAAID,uBAAuB,EAAE;IAC5B,OAAOA,uBAAuB;EAC/B;;EAEA;EACA,MAAME,GAAkB,GAAG,EAAE;EAC7B,KAAK,MAAMC,OAAO,IAAIJ,QAAQ,EAAE;IAC/B,MAAM;MAACK;IAAe,CAAC,GAAGD,OAAO;IACjC,IAAI,CAACC,eAAe,EAAE;MACrB;IACD;IACA,KAAK,MAAMC,GAAG,IAAID,eAAe,EAAE;MAClCF,GAAG,CAACI,IAAI,CAAC;QACRH,OAAO;QACPE,GAAG,EAAEA,GAAG,CAACE,WAAW,CAAC;MACtB,CAAC,CAAC;IACH;EACD;;EAEA;EACAL,GAAG,CAACM,IAAI,CAAC,CAACC,CAAC,EAAEC,CAAC,KAAKA,CAAC,CAACL,GAAG,CAACM,MAAM,GAAGF,CAAC,CAACJ,GAAG,CAACM,MAAM,CAAC;EAC/C,OAAQX,uBAAuB,GAAGE,GAAG;AACtC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASU,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