@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
93 lines (91 loc) • 4.96 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_utils_getFormatFromExtension = require('../utils/getFormatFromExtension.cjs');
const require_writeContentDeclaration_transformJSFile = require('./transformJSFile.cjs');
const require_detectFormatCommand = require('../detectFormatCommand.cjs');
const require_getContentDeclarationFileTemplate_getContentDeclarationFileTemplate = require('../getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs');
let node_fs_promises = require("node:fs/promises");
let node_path = require("node:path");
let _intlayer_config_logger = require("@intlayer/config/logger");
let node_fs = require("node:fs");
let node_child_process = require("node:child_process");
//#region src/writeContentDeclaration/writeJSFile.ts
/**
* Updates a JavaScript/TypeScript file based on the provided JSON instructions.
* It targets a specific dictionary object within the file (identified by its 'key' property)
* and updates its 'content' entries. Currently, it focuses on modifying arguments
* of 't' (translation) function calls.
*/
const writeJSFile = async (filePath, dictionary, configuration, noMetadata) => {
const mergedDictionary = {
...configuration.dictionary,
...dictionary
};
const appLogger = (0, _intlayer_config_logger.getAppLogger)(configuration);
if (!(0, node_fs.existsSync)(filePath)) {
const format = require_utils_getFormatFromExtension.getFormatFromExtension((0, node_path.extname)(filePath));
appLogger("File does not exist, creating it", { isVerbose: true });
const template = await require_getContentDeclarationFileTemplate_getContentDeclarationFileTemplate.getContentDeclarationFileTemplate(mergedDictionary.key, format, Object.fromEntries(Object.entries({
id: noMetadata ? void 0 : mergedDictionary.id,
locale: noMetadata ? void 0 : mergedDictionary.locale,
filled: noMetadata ? void 0 : mergedDictionary.filled,
fill: noMetadata ? void 0 : mergedDictionary.fill,
description: noMetadata ? void 0 : mergedDictionary.description,
title: noMetadata ? void 0 : mergedDictionary.title,
tags: noMetadata ? void 0 : mergedDictionary.tags,
version: noMetadata ? void 0 : mergedDictionary.version,
priority: noMetadata ? void 0 : mergedDictionary.priority,
importMode: noMetadata ? void 0 : mergedDictionary.importMode
}).filter(([, value]) => value !== void 0)), noMetadata);
const tempDir = configuration.system?.tempDir;
if (tempDir) await (0, node_fs_promises.mkdir)(tempDir, { recursive: true });
const tempFileName = `${(0, node_path.basename)(filePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`;
const tempPath = tempDir ? (0, node_path.join)(tempDir, tempFileName) : `${filePath}.${tempFileName}`;
try {
await (0, node_fs_promises.writeFile)(tempPath, template, "utf-8");
await (0, node_fs_promises.rename)(tempPath, filePath);
} catch (error) {
try {
await (0, node_fs_promises.rm)(tempPath, { force: true });
} catch {}
throw error;
}
}
let fileContent = await (0, node_fs_promises.readFile)(filePath, "utf-8");
if (fileContent === "") {
const format = require_utils_getFormatFromExtension.getFormatFromExtension((0, node_path.extname)(filePath));
fileContent = await require_getContentDeclarationFileTemplate_getContentDeclarationFileTemplate.getContentDeclarationFileTemplate(mergedDictionary.key, format, {}, noMetadata);
}
const finalCode = await require_writeContentDeclaration_transformJSFile.transformJSFile(fileContent, dictionary, dictionary.locale, noMetadata);
const tempDir = configuration.system?.tempDir;
if (tempDir) await (0, node_fs_promises.mkdir)(tempDir, { recursive: true });
const tempFileName = `${(0, node_path.basename)(filePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`;
const tempPath = tempDir ? (0, node_path.join)(tempDir, tempFileName) : `${filePath}.${tempFileName}`;
try {
await (0, node_fs_promises.writeFile)(tempPath, finalCode, "utf-8");
await (0, node_fs_promises.rename)(tempPath, filePath);
(0, _intlayer_config_logger.logger)(`Successfully updated ${filePath}`, {
level: "info",
isVerbose: true
});
} catch (error) {
try {
await (0, node_fs_promises.rm)(tempPath, { force: true });
} catch {}
const err = error;
(0, _intlayer_config_logger.logger)(`Failed to write updated file: ${filePath}`, { level: "error" });
throw new Error(`Failed to write updated file ${filePath}: ${err.message}`);
}
const formatCommand = require_detectFormatCommand.detectFormatCommand(configuration);
if (formatCommand) try {
(0, node_child_process.execSync)(formatCommand.replace("{{file}}", filePath), {
stdio: "inherit",
cwd: configuration.system.baseDir
});
} catch (error) {
console.error(error);
}
};
//#endregion
exports.writeJSFile = writeJSFile;
//# sourceMappingURL=writeJSFile.cjs.map