UNPKG

@intlayer/core

Version:

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

51 lines (49 loc) 2.06 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../../_virtual/_rolldown/runtime.cjs'); let _intlayer_types_nodeType = require("@intlayer/types/nodeType"); let _intlayer_config_logger = require("@intlayer/config/logger"); let node_fs = require("node:fs"); let node_path = require("node:path"); //#region src/transpiler/file/file.ts const fileContent = (path, callerDir, baseDir) => { const isRelativePath = path.startsWith("./") || path.startsWith("../"); const appLogger = (0, _intlayer_config_logger.getAppLogger)(); let filePath; if ((0, node_path.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 = (0, node_path.resolve)(callerDir, path); else filePath = (0, node_path.resolve)(baseDir, path); if ((0, node_fs.existsSync)(filePath) && (0, node_fs.statSync)(filePath).isFile()) try { return (0, _intlayer_types_nodeType.formatNodeType)(_intlayer_types_nodeType.FILE, path, { content: (0, node_fs.readFileSync)(filePath, "utf8"), fixedPath: (0, node_path.relative)(baseDir, filePath) }); } catch { throw new Error(`Unable to read path: ${(0, _intlayer_config_logger.colorizePath)((0, node_path.relative)(baseDir, filePath))}`); } else throw new Error(`File not found: ${(0, _intlayer_config_logger.colorizePath)((0, node_path.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, (0, node_path.dirname)(INTLAYER_FILE_PATH), INTLAYER_BASE_DIR); }; //#endregion exports.file = file; exports.fileContent = fileContent; //# sourceMappingURL=file.cjs.map