UNPKG

ts-patch

Version:

Patch typescript to support custom transformers in tsconfig.json

98 lines 4.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getModuleFile = void 0; const fs_1 = __importDefault(require("fs")); const patch_detail_1 = require("../patch/patch-detail"); const path_1 = __importDefault(require("path")); const utils_1 = require("../utils"); /* ****************************************************************************************************************** */ // region: Config /* ****************************************************************************************************************** */ const SHORT_CHUNK_SIZE = 1024; const LONG_CHUNK_SIZE = 64536; // endregion /* ****************************************************************************************************************** */ // region: Helpers /* ****************************************************************************************************************** */ function readFile(filePath, headersOnly) { return (0, utils_1.withFileLock)(filePath, () => { let CHUNK_SIZE = headersOnly ? SHORT_CHUNK_SIZE : LONG_CHUNK_SIZE; let result = ''; let doneReadingHeaders = false; let bytesRead; let buffer = Buffer.alloc(CHUNK_SIZE); const headerLines = []; let isLastHeaderIncomplete = false; const fd = fs_1.default.openSync(filePath, 'r'); try { readFileLoop: while ((bytesRead = fs_1.default.readSync(fd, buffer, 0, CHUNK_SIZE, null)) > 0) { const chunkString = buffer.toString('utf-8', 0, bytesRead); /* Handle Header */ if (!doneReadingHeaders) { const lines = chunkString.split('\n'); lineLoop: for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (i === 0 && isLastHeaderIncomplete) { headerLines[headerLines.length - 1] += line; } else { if (line.startsWith('///')) { headerLines.push(line); } else { doneReadingHeaders = true; if (!headersOnly) { result += lines.slice(i).join('\n'); CHUNK_SIZE = LONG_CHUNK_SIZE; buffer = Buffer.alloc(CHUNK_SIZE); break lineLoop; } else { break readFileLoop; } } } } if (!doneReadingHeaders) isLastHeaderIncomplete = !chunkString.endsWith('\n'); } else { /* Handle content */ result += chunkString; } } return { headerLines, content: headersOnly ? undefined : result }; } finally { fs_1.default.closeSync(fd); } }); } // endregion /* ****************************************************************************************************************** */ // region: Utils /* ****************************************************************************************************************** */ function getModuleFile(filePath, loadFullContent) { let { headerLines, content } = readFile(filePath, !loadFullContent); /* Get PatchDetail */ const patchDetail = patch_detail_1.PatchDetail.fromHeader(headerLines); return { moduleName: path_1.default.basename(filePath), filePath, patchDetail, get content() { if (content == null) content = readFile(this.filePath, false).content; return content; }, getHash() { return (0, utils_1.getHash)(this.content); } }; } exports.getModuleFile = getModuleFile; // endregion //# sourceMappingURL=module-file.js.map