arx-level-generator
Version:
A tool for creating Arx Fatalis maps
91 lines • 3.08 kB
JavaScript
import crypto from 'node:crypto';
import { createReadStream } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileExists } from '../helpers.js';
export const hashingAlgorithm = 'sha1';
/**
* Creates the folder structure inside the project's cache folder for a given path
* (supports nested folders)
*
* @param folder - a folder relative to the project's cache folder
* @param settings - an instance of the Settings object
* @returns the absolute path for the given folder
*/
export const createCacheFolderIfNotExists = async (folder, settings) => {
const fullFolder = path.resolve(settings.cacheFolder, folder);
try {
await fs.access(fullFolder, fs.constants.R_OK | fs.constants.W_OK);
}
catch (e) {
await fs.mkdir(fullFolder, { recursive: true });
}
return fullFolder;
};
/**
*
* @param filename - a pathname of a file relative to the project's root directory
*/
export const getCacheStats = async (filename, settings) => {
const { dir, base } = path.parse(filename);
const cachedFolder = await createCacheFolderIfNotExists(dir, settings);
const cachedFilename = path.join(cachedFolder, base);
const cacheExists = await fileExists(cachedFilename);
const hashOfCachedFilename = await loadHashOf(cachedFilename, settings);
return {
filename: cachedFilename,
exists: cacheExists,
hash: hashOfCachedFilename,
};
};
/**
* This function assumes that the cache folder exists
* and that the hash of the cached file is in sync with the contents of the __hashes.json
*
* @param filename - full path to a file
*/
export const loadHashOf = async (filename, settings) => {
const hashesFilename = path.resolve(settings.cacheFolder, '__hashes.json');
try {
const hashes = JSON.parse(await fs.readFile(hashesFilename, { encoding: 'utf-8' }));
return hashes[filename];
}
catch (e) {
return undefined;
}
};
/**
* This function does not generate hashes, but merely stores them
*
* @param filename - full path to a file
*/
export const saveHashOf = async (filename, hash, settings) => {
const hashesFilename = path.resolve(settings.cacheFolder, '__hashes.json');
await createCacheFolderIfNotExists('.', settings);
let hashes = {};
try {
hashes = JSON.parse(await fs.readFile(hashesFilename, { encoding: 'utf-8' }));
}
catch (e) { }
hashes[filename] = hash;
await fs.writeFile(hashesFilename, JSON.stringify(hashes), { encoding: 'utf-8' });
};
/**
* @param filename - full path to a file
*/
export const getHashOfFile = async (filename) => {
const hash = crypto.createHash(hashingAlgorithm);
const stream = createReadStream(filename);
stream.on('data', (chunk) => {
hash.update(chunk);
});
return new Promise((resolve, reject) => {
stream.on('error', (err) => {
reject(err);
});
stream.on('end', () => {
resolve(hash.digest('hex'));
});
});
};
//# sourceMappingURL=cache.js.map