feat(sync): finish state file implementation
Some checks failed
Actions / Build and Push Documentation (push) Failing after 36s

This commit is contained in:
cswimr 2025-02-13 00:11:02 -06:00
parent 872ea12b3d
commit e8873f3ce8
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087

View file

@ -38,22 +38,19 @@ function getSaveLocation(
} }
} }
async function writeStateFile( async function writeStateFile(packwiz: Packwiz) {
packwiz: Packwiz,
hashes: { [fileName: string]: HashInfo },
) {
const stateFile = packwiz.index.location.parent.join(".packwizjs-state.json"); const stateFile = packwiz.index.location.parent.join(".packwizjs-state.json");
const state: State = { const state: State = {
stateVersion: 1, stateVersion: 1,
currentVersion: packwiz.version, currentVersion: packwiz.version,
hashes: hashes, hashes: {},
}; };
// for (const file of packwiz.index.files) { for (const file of packwiz.index.files) {
// state.hashes[packwiz.index.location.diff(file.file).join("/")] = { state.hashes[packwiz.index.location.diff(file.file).join("/")] = {
// hashFormat: file.hashFormat, hashFormat: file.hashFormat,
// hash: file.hash, hash: file.hash,
// }; };
// } }
const saveLocation = getSaveLocation(stateFile, packwiz.index); const saveLocation = getSaveLocation(stateFile, packwiz.index);
await write(saveLocation.toString(), JSON.stringify(state, null)); await write(saveLocation.toString(), JSON.stringify(state, null));
} }
@ -201,15 +198,9 @@ export async function iteratePackwizIndex(
} }
if (state) { if (state) {
let stateHash: HashInfo | undefined; const stateHash =
// TODO: implement hash checking for files downloaded via metafile providers state.hashes[packwiz.index.location.diff(file.file).join("/")];
if (!file.metafile) { if (stateHash?.hash && stateHash.hash === file.hash) {
stateHash =
state.hashes[
packwiz.index.location.diff(file.file).join("/")
];
}
if (stateHash?.hash && stateHash.hash === hash) {
console.log( console.log(
`Skipping already downloaded file ${file.file.toString()}`, `Skipping already downloaded file ${file.file.toString()}`,
); );
@ -219,11 +210,6 @@ export async function iteratePackwizIndex(
} else { } else {
await downloadFile(hash, hashFormat, url, saveLocation); await downloadFile(hash, hashFormat, url, saveLocation);
} }
hashes[packwiz.index.location.diff(file.file).join("/")] = {
hashFormat: hashFormat,
hash: hash,
};
} catch (error) { } catch (error) {
console.error(`Error downloading file: ${error}`); console.error(`Error downloading file: ${error}`);
reject(error); reject(error);
@ -236,5 +222,5 @@ export async function iteratePackwizIndex(
} }
} }
downloadNextFile(); downloadNextFile();
}).then(() => writeStateFile(packwiz, hashes)); }).then(() => writeStateFile(packwiz));
} }