UNPKG

@typed/content-hash

Version:

Content hash a directory of HTML/JS/CSS files and other static assets

32 lines 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fsReadFile = void 0; const Env_1 = require("@typed/fp/Env"); const FxEnv_1 = require("@typed/fp/FxEnv"); const Option_1 = require("fp-ts/Option"); const fs_1 = require("fs"); const logging_1 = require("../application/services/logging"); const getFileExtension_1 = require("./plugins/getFileExtension"); const sha512Hash_1 = require("./sha512Hash"); const sourceMapExt = '.map'; const proxyJsExt = '.proxy.js'; const fsReadFile = (filePath, options) => (0, FxEnv_1.Do)(function* (_) { yield* _((0, logging_1.debug)(`Reading file ${filePath}...`)); const contents = yield* _((0, Env_1.fromTask)(() => fs_1.promises.readFile(filePath).then((b) => (options.isBase64Encoded ? b.toString('base64') : b.toString())))); const fileExtension = (0, getFileExtension_1.getFileExtension)(filePath); const skipSourceMap = options.isBase64Encoded || !options.supportsSourceMaps || [sourceMapExt, proxyJsExt].some((ext) => fileExtension.endsWith(ext)); const document = { filePath, fileExtension, contents, contentHash: (0, Option_1.some)({ type: 'hash', hash: (0, sha512Hash_1.sha512Hash)(contents) }), dependencies: [], sourceMap: skipSourceMap ? Option_1.none : (0, Option_1.some)(filePath + sourceMapExt), isBase64Encoded: options.isBase64Encoded, }; return document; }); exports.fsReadFile = fsReadFile; //# sourceMappingURL=fsReadFile.js.map