@storm-stack/core
Version:
A build toolkit and runtime used by Storm Software in TypeScript applications
70 lines (66 loc) • 2.41 kB
JavaScript
;
var chunkEG72EGDV_cjs = require('./chunk-EG72EGDV.cjs');
var chunk3ONWID2V_cjs = require('./chunk-3ONWID2V.cjs');
var chmodX = require('@stryke/fs/chmod-x');
var helpers = require('@stryke/fs/helpers');
var readFile = require('@stryke/fs/read-file');
var removeFile = require('@stryke/fs/remove-file');
var writeFile = require('@stryke/fs/write-file');
var hash = require('@stryke/hash');
var exists = require('@stryke/path/exists');
var filePathFns = require('@stryke/path/file-path-fns');
var joinPaths = require('@stryke/path/join-paths');
function getCacheHashKey(id, code) {
return hash.hash({
id,
code: chunkEG72EGDV_cjs.getString(code)
}, {
maxLength: 32
});
}
chunk3ONWID2V_cjs.__name(getCacheHashKey, "getCacheHashKey");
function getCacheFileName(id, hashKey) {
return `${filePathFns.findFileName(id, {
withExtension: false
})}_${hashKey}.cache`;
}
chunk3ONWID2V_cjs.__name(getCacheFileName, "getCacheFileName");
async function getCache(sourceFile, cacheDir) {
const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code);
const cacheFilePath = joinPaths.joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey));
if (!exists.existsSync(cacheFilePath)) {
return void 0;
}
const cache = await readFile.readFile(cacheFilePath);
if (!cache.includes(hashComment(hashKey))) {
return void 0;
}
return cache;
}
chunk3ONWID2V_cjs.__name(getCache, "getCache");
async function setCache(sourceFile, cacheDir, transpiled) {
const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code);
const cacheFilePath = joinPaths.joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey));
if (exists.existsSync(cacheFilePath)) {
await removeFile.removeFile(cacheFilePath);
}
if (transpiled) {
if (!exists.existsSync(cacheDir)) {
await helpers.createDirectory(cacheDir);
}
if (!await chmodX.isWritable(cacheDir)) {
throw new Error(`Cache directory is not writable: ${cacheDir}`);
}
await writeFile.writeFile(cacheFilePath, `${transpiled}
${hashComment(hashKey)}`.trim());
}
}
chunk3ONWID2V_cjs.__name(setCache, "setCache");
function hashComment(hashKey) {
return `/* storm-stack_${hashKey} */`;
}
chunk3ONWID2V_cjs.__name(hashComment, "hashComment");
exports.getCache = getCache;
exports.setCache = setCache;
//# sourceMappingURL=chunk-FWFMIVER.cjs.map
//# sourceMappingURL=chunk-FWFMIVER.cjs.map