@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
41 lines • 1.26 kB
JavaScript
import { getAppLogger } from "@intlayer/config";
import configuration from "@intlayer/config/built";
import { existsSync, readFileSync } from "fs";
import { relative, resolve } from "path";
import {
formatNodeType,
NodeType
} from "../../types/index.mjs";
const file = (path) => {
const callerDir = intlayer_file_dir ?? process.cwd();
const isAbsolutePath = path.startsWith("/");
const isRelativePath = path.startsWith("./") || path.startsWith("../");
const appLogger = getAppLogger(configuration);
let filePath;
if (isAbsolutePath) {
appLogger(
`Using absolute path for file is not recommended. Use relative paths instead. Path: ${path}, imported from: ${intlayer_file_path}`,
{ level: "warn" }
);
filePath = path;
} else if (isRelativePath) {
filePath = resolve(callerDir, path);
} else {
filePath = resolve(process.cwd(), path);
}
let content;
if (existsSync(filePath)) {
content = readFileSync(filePath, "utf8");
} else {
appLogger(`File not found: ${filePath}`, { level: "warn" });
content = `File not found`;
}
return formatNodeType(NodeType.File, path, {
content,
fixedPath: relative(process.cwd(), filePath)
});
};
export {
file
};
//# sourceMappingURL=file.mjs.map