savePack
This commit is contained in:
+37
-2
@@ -1,6 +1,6 @@
|
||||
import type { FileSystemDirectoryHandle, FileSystemFileHandle, FileSystemGetDirectoryOptions, FileSystemGetFileOptions, FileSystemWriteChunkType, Response } from "@sugoidogo/importable-types-web"
|
||||
import type { Hash, HashFormat, PackFileMetaData, PackIndex, PackMetaData, Path, Url } from "./types"
|
||||
import { parseTOML } from "confbox"
|
||||
import { parseTOML, stringifyTOML } from "confbox"
|
||||
import { forAsync } from "@sugoidogo/js-util"
|
||||
|
||||
const pathSeperatorRegex = /[\\\/]/
|
||||
@@ -146,9 +146,44 @@ export async function loadPack(packDirectoryHandle: FileSystemDirectoryHandle, p
|
||||
const file = await fileHandle.getFile()
|
||||
const fileBytes = await file.bytes()
|
||||
if (!packMetaData.options || !packMetaData.options["no-internal-hashes"]) {
|
||||
assertHashMatch(fileBytes,entry["hash-format"]||packIndex["hash-format"],entry.hash)
|
||||
assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash)
|
||||
}
|
||||
if (entry.metafile) pack.packFiles[entry.file] = parseTOML(fileBytes.toString())
|
||||
})
|
||||
return pack
|
||||
}
|
||||
|
||||
export async function savePack(pack: PackwizPack, hashes?: boolean) {
|
||||
if (typeof hashes !== "boolean") {
|
||||
hashes = !pack.packMetaData.options || !pack.packMetaData.options["no-internal-hashes"]
|
||||
}
|
||||
const textEncoder = new TextEncoder()
|
||||
const packIndexPathSegments = pack.packMetaData.index.file.split(pathSeperatorRegex)
|
||||
packIndexPathSegments.pop()
|
||||
const packIndexDirname = packIndexPathSegments.join("/")
|
||||
await forAsync(pack.packIndex.files, async entry => {
|
||||
const packFileMetaData = pack.packFiles[entry.file]
|
||||
if (!packFileMetaData) return
|
||||
const packFileBytes = textEncoder.encode(stringifyTOML(packFileMetaData))
|
||||
if (hashes) {
|
||||
entry.hash = await getHash(packFileBytes, entry["hash-format"] || pack.packIndex["hash-format"])
|
||||
.then(hash => hash.toString())
|
||||
} else {
|
||||
delete entry.hash
|
||||
}
|
||||
const fileHandle = await getFileHandle(pack.packDirectoryHandle, packIndexDirname + "/" + entry.file)
|
||||
await writeFile(fileHandle, packFileBytes)
|
||||
})
|
||||
await getFileHandle(pack.packDirectoryHandle, pack.packMetaData.index.file, { "create": true }).then(async packIndexFileHandle => {
|
||||
const packIndexBytes = textEncoder.encode(stringifyTOML(pack.packIndex))
|
||||
if (hashes) {
|
||||
pack.packMetaData.index.hash = await getHash(packIndexBytes, pack.packMetaData.index["hash-format"])
|
||||
.then(hash => hash.toString())
|
||||
} else {
|
||||
delete pack.packMetaData.index.hash
|
||||
}
|
||||
await writeFile(packIndexFileHandle, packIndexBytes)
|
||||
})
|
||||
await getFileHandle(pack.packDirectoryHandle, pack.packFileName, { "create": true })
|
||||
.then(packFileHandle => writeFile(packFileHandle, stringifyTOML(pack.packMetaData)))
|
||||
}
|
||||
Reference in New Issue
Block a user