UNPKG

cspell

Version:

A Spelling Checker for Code!

79 lines 2.66 kB
/** * This is a wrapper for 'file-entry-cache' */ import { mkdirSync } from 'node:fs'; import * as path from 'node:path'; import { isMainThread } from 'node:worker_threads'; import * as fec from './file-entry-cache.mjs'; export function createFromFile(pathToCache, useCheckSum, useRelative) { const absPathToCache = path.resolve(pathToCache); const relDir = path.dirname(absPathToCache); mkdirSync(relDir, { recursive: true }); const create = wrap(() => fec.createFromFile(absPathToCache, useCheckSum)); const feCache = create(); const cacheWrapper = { get cache() { return feCache.cache; }, getHash(buffer) { return feCache.getHash(buffer); }, hasFileChanged: wrap((cwd, file) => { // console.log(file); return feCache.hasFileChanged(resolveFile(cwd, file)); }), analyzeFiles: wrap((cwd, files) => { return feCache.analyzeFiles(resolveFiles(cwd, files)); }), getFileDescriptor: wrap((cwd, file) => { return feCache.getFileDescriptor(resolveFile(cwd, file)); }), getUpdatedFiles: wrap((cwd, files) => { return feCache.getUpdatedFiles(resolveFiles(cwd, files)); }), normalizeEntries: wrap((cwd, files) => { return feCache.normalizeEntries(resolveFiles(cwd, files)); }), removeEntry: wrap((cwd, file) => { // console.log(file); return feCache.removeEntry(resolveFile(cwd, file)); }), deleteCacheFile() { feCache.deleteCacheFile(); }, destroy() { feCache.destroy(); }, reconcile: wrap((_cwd, noPrune) => { feCache.reconcile(noPrune); }), }; return cacheWrapper; function resolveFile(cwd, file) { if (!useRelative) return normalizePath(file); const r = path.relative(relDir, path.resolve(cwd, file)); return normalizePath(r); } function resolveFiles(cwd, files) { return files?.map((file) => resolveFile(cwd, file)); } function wrap(fn) { return (...params) => { const cwd = process.cwd(); try { isMainThread && process.chdir(relDir); return fn(cwd, ...params); } finally { isMainThread && process.chdir(cwd); } }; } } export function normalizePath(filePath) { if (path.sep === '/') return filePath; return filePath.split(path.sep).join('/'); } //# sourceMappingURL=fileEntryCache.js.map