UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

67 lines (64 loc) 2.16 kB
import { getString } from './chunk-K4ZS2URJ.js'; import { __name } from './chunk-43IZMM3W.js'; import { isWritable } from '@stryke/fs/chmod-x'; import { createDirectory } from '@stryke/fs/helpers'; import { readFile } from '@stryke/fs/read-file'; import { removeFile } from '@stryke/fs/remove-file'; import { writeFile } from '@stryke/fs/write-file'; import { hash } from '@stryke/hash'; import { existsSync } from '@stryke/path/exists'; import { findFileName } from '@stryke/path/file-path-fns'; import { joinPaths } from '@stryke/path/join-paths'; function getCacheHashKey(id, code) { return hash({ id, code: getString(code) }, { maxLength: 32 }); } __name(getCacheHashKey, "getCacheHashKey"); function getCacheFileName(id, hashKey) { return `${findFileName(id, { withExtension: false })}_${hashKey}.cache`; } __name(getCacheFileName, "getCacheFileName"); async function getCache(sourceFile, cacheDir) { const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code); const cacheFilePath = joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey)); if (!existsSync(cacheFilePath)) { return void 0; } const cache = await readFile(cacheFilePath); if (!cache.includes(hashComment(hashKey))) { return void 0; } return cache; } __name(getCache, "getCache"); async function setCache(sourceFile, cacheDir, transpiled) { const hashKey = getCacheHashKey(sourceFile.id, sourceFile.code); const cacheFilePath = joinPaths(cacheDir, getCacheFileName(sourceFile.id, hashKey)); if (existsSync(cacheFilePath)) { await removeFile(cacheFilePath); } if (transpiled) { if (!existsSync(cacheDir)) { await createDirectory(cacheDir); } if (!await isWritable(cacheDir)) { throw new Error(`Cache directory is not writable: ${cacheDir}`); } await writeFile(cacheFilePath, `${transpiled} ${hashComment(hashKey)}`.trim()); } } __name(setCache, "setCache"); function hashComment(hashKey) { return `/* storm-stack_${hashKey} */`; } __name(hashComment, "hashComment"); export { getCache, setCache }; //# sourceMappingURL=chunk-NNKV7APZ.js.map //# sourceMappingURL=chunk-NNKV7APZ.js.map