@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
50 lines (48 loc) • 1.77 kB
JavaScript
import { NodeType, formatNodeType } from "@intlayer/types";
import { existsSync, readFileSync, statSync } from "node:fs";
import { dirname, isAbsolute, relative, resolve } from "node:path";
import { colorizePath, getAppLogger } from "@intlayer/config";
//#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 {
const content = readFileSync(filePath, "utf8");
return formatNodeType(NodeType.File, path, {
content,
fixedPath: relative(baseDir, filePath)
});
} catch {
appLogger(`Unable to read path: ${colorizePath(relative(baseDir, filePath))}`, { level: "warn" });
}
else appLogger(`File not found: ${colorizePath(relative(baseDir, filePath))}`, { level: "warn" });
return formatNodeType(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, dirname(INTLAYER_FILE_PATH), INTLAYER_BASE_DIR);
};
//#endregion
export { file, fileContent };
//# sourceMappingURL=file.mjs.map