UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

48 lines (46 loc) 1.66 kB
import { FILE, formatNodeType } from "@intlayer/types/nodeType"; import { colorizePath, getAppLogger } from "@intlayer/config/logger"; import { existsSync, readFileSync, statSync } from "node:fs"; import { dirname, isAbsolute, relative, resolve } from "node:path"; //#region src/transpiler/file/file.ts const fileContent = (path, callerDir, baseDir) => { const isRelativePath = path.startsWith("./") || path.startsWith("../"); const appLogger = getAppLogger(); let filePath; if (isAbsolute(path)) { appLogger(`Using absolute path for file is not recommended. Use relative paths instead. Path: ${path}, imported from: ${callerDir}`, { level: "warn" }); filePath = path; } else if (isRelativePath) filePath = resolve(callerDir, path); else filePath = resolve(baseDir, path); if (existsSync(filePath) && statSync(filePath).isFile()) try { return formatNodeType(FILE, path, { content: readFileSync(filePath, "utf8"), fixedPath: relative(baseDir, filePath) }); } catch { throw new Error(`Unable to read path: ${colorizePath(relative(baseDir, filePath))}`); } else throw new Error(`File not found: ${colorizePath(relative(baseDir, filePath))}`); }; /** * Function intended to be used to build intlayer dictionaries. * * Allow identify the usage of an external resource. * * Usage: * * ```ts * file('/path/to/file.md') // absolute path * * // or * * file('path/to/file.md') // relative path * ``` */ const file = (path) => { const { INTLAYER_FILE_PATH, INTLAYER_BASE_DIR } = globalThis; return fileContent(path, dirname(INTLAYER_FILE_PATH), INTLAYER_BASE_DIR); }; //#endregion export { file, fileContent }; //# sourceMappingURL=file.mjs.map