next-intl-scanner
Version:
A tool to extract and manage translations from Next.js projects using next-intl
124 lines (123 loc) • 4.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = exports.validateConfig = exports.defaultConfig = void 0;
const logger_1 = __importDefault(require("./logger.js"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
exports.defaultConfig = {
locales: ["en"],
sourceDirectory: "./",
outputDirectory: "./messages",
defaultLocale: "en",
pages: [
{
match: "./src/(pages|app)/**/*.{js,jsx,ts,tsx}",
ignore: ["**/*.test.{js,jsx,ts,tsx}", "**/_*.js"],
},
],
customJSXPattern: [],
ignore: ["**/node_modules/**", "**/.next/**", ".git/**"],
};
const validateConfig = (config) => {
//merge the default config with the user config
const errors = [];
if (!config.locales ||
!Array.isArray(config.locales) ||
!config.locales.length) {
errors.push("Locales are required");
}
if (!config.sourceDirectory) {
errors.push("Source directory is required");
}
if (!config.outputDirectory) {
errors.push("Output directory is required");
}
if (!config.defaultLocale) {
errors.push("Default locale is required");
}
if (errors.length) {
logger_1.default.error("Failed to validate configuration");
logger_1.default.error(errors.join(", "));
return false;
}
return true;
};
exports.validateConfig = validateConfig;
const importFile = async (url) => {
let config;
try {
const ext = path_1.default.extname(url).toLowerCase();
if (ext === ".json") {
// For JSON files
const content = fs_1.default.readFileSync(url, {
encoding: "utf-8",
});
config = JSON.parse(content);
}
else if (ext === ".js" || ext === ".cjs" || ext === ".mjs") {
// For JS files, handle both CommonJS and ES modules
const module = require(url);
config = module.default || module;
}
else {
throw new Error(`Unsupported config file extension: ${ext}`);
}
return config;
}
catch (error) {
console.error("Error loading config file:", error);
throw error;
}
};
const loadConfig = async (configPath, custom = false) => {
let parsedConfig = exports.defaultConfig;
let config;
if (configPath) {
if (custom) {
// Resolve the path relative to the current working directory
const absolutePath = path_1.default.resolve(process.cwd(), configPath);
logger_1.default.info(`Using custom configuration file: ${absolutePath}`);
if (!fs_1.default.existsSync(absolutePath)) {
logger_1.default.error(`Custom Configuration file does not exist at: ${absolutePath}`);
return null;
}
config = await importFile(absolutePath);
}
else {
// Get the absolute path to the config file
const absolutePath = path_1.default.resolve(process.cwd(), configPath);
if (!fs_1.default.existsSync(absolutePath)) {
logger_1.default.error(`Default Configuration file does not exist at: ${absolutePath}\nPlease create next-intl-scanner.config.js or next-intl-scanner.config.json in your project root`);
return null;
}
//first find .json , if not found then find .js
const isJson = absolutePath.endsWith(".json");
const isJs = absolutePath.endsWith(".js");
if (!isJson && !isJs) {
logger_1.default.error(`Default Configuration file does not exist at: ${absolutePath}\nPlease create next-intl-scanner.config.js or next-intl-scanner.config.json in your project root`);
return null;
}
const configUrl = absolutePath;
config = await importFile(configUrl);
}
// Deep merge the configs
parsedConfig = {
...exports.defaultConfig,
...config,
pages: config.pages || exports.defaultConfig.pages,
customJSXPattern: config.customJSXPattern || exports.defaultConfig.customJSXPattern,
ignore: [...new Set([...exports.defaultConfig.ignore, ...(config.ignore || [])])],
};
if (!(0, exports.validateConfig)(parsedConfig)) {
return null;
}
}
else {
logger_1.default.info("Using default configuration");
}
return parsedConfig;
};
exports.loadConfig = loadConfig;