UNPKG

@intlayer/core

Version:

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

52 lines (50 loc) 2.09 kB
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs'); let __intlayer_types = require("@intlayer/types"); let node_fs = require("node:fs"); let node_path = require("node:path"); let __intlayer_config = require("@intlayer/config"); //#region src/transpiler/file/file.ts const fileContent = (path, callerDir, baseDir) => { const isRelativePath = path.startsWith("./") || path.startsWith("../"); const appLogger = (0, __intlayer_config.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 { const content = (0, node_fs.readFileSync)(filePath, "utf8"); return (0, __intlayer_types.formatNodeType)(__intlayer_types.NodeType.File, path, { content, fixedPath: (0, node_path.relative)(baseDir, filePath) }); } catch { appLogger(`Unable to read path: ${(0, __intlayer_config.colorizePath)((0, node_path.relative)(baseDir, filePath))}`, { level: "warn" }); } else appLogger(`File not found: ${(0, __intlayer_config.colorizePath)((0, node_path.relative)(baseDir, filePath))}`, { level: "warn" }); return (0, __intlayer_types.formatNodeType)(__intlayer_types.NodeType.File, path, { content: `-` }); }; /** * 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