@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
159 lines (157 loc) • 7.31 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
const require_utils_readDictionariesFromDisk = require('../utils/readDictionariesFromDisk.cjs');
const require_utils_getFormatFromExtension = require('../utils/getFormatFromExtension.cjs');
const require_detectFormatCommand = require('../detectFormatCommand.cjs');
const require_writeContentDeclaration_processContentDeclarationContent = require('./processContentDeclarationContent.cjs');
const require_writeContentDeclaration_transformJSONFile = require('./transformJSONFile.cjs');
const require_writeContentDeclaration_writeJSFile = require('./writeJSFile.cjs');
const require_writeContentDeclaration_writeMarkdownFile = require('./writeMarkdownFile.cjs');
const require_writeContentDeclaration_writeYamlFile = require('./writeYamlFile.cjs');
let node_fs_promises = require("node:fs/promises");
let node_path = require("node:path");
let node_fs = require("node:fs");
let _intlayer_config_defaultValues = require("@intlayer/config/defaultValues");
let _intlayer_core_plugins = require("@intlayer/core/plugins");
let node_child_process = require("node:child_process");
let node_util = require("node:util");
//#region src/writeContentDeclaration/writeContentDeclaration.ts
const formatContentDeclaration = async (dictionary, configuration, localeList) => {
/**
* Clean Markdown, Insertion, File, etc. node metadata
*/
const processedDictionary = await require_writeContentDeclaration_processContentDeclarationContent.processContentDeclarationContent(dictionary);
let content = processedDictionary.content;
/**
* Filter locales content
*/
if (dictionary.locale) content = (0, _intlayer_core_plugins.getPerLocaleDictionary)(processedDictionary, dictionary.locale).content;
else if (localeList) content = (0, _intlayer_core_plugins.getFilteredLocalesDictionary)(processedDictionary, localeList).content;
let pluginFormatResult = {
...dictionary,
content
};
/**
* Format the dictionary with the plugins
*/
for await (const plugin of configuration.plugins ?? []) if (plugin.formatOutput) {
const formattedResult = await plugin.formatOutput?.({
dictionary: pluginFormatResult,
configuration
});
if (formattedResult) pluginFormatResult = formattedResult;
}
if (!(pluginFormatResult.content && pluginFormatResult.key)) return pluginFormatResult;
let result = {
key: dictionary.key,
id: dictionary.id,
title: dictionary.title,
description: dictionary.description,
tags: dictionary.tags,
locale: dictionary.locale,
fill: dictionary.fill,
filled: dictionary.filled,
priority: dictionary.priority,
importMode: dictionary.importMode,
version: dictionary.version,
content
};
if (require_utils_getFormatFromExtension.getFormatFromExtension(dictionary.filePath ? (0, node_path.extname)(dictionary.filePath) : ".json") === "json" && pluginFormatResult.content && pluginFormatResult.key) result = {
$schema: "https://intlayer.org/schema.json",
...result
};
return result;
};
const defaultOptions = { newDictionariesPath: "intlayer-dictionaries" };
const writeContentDeclaration = async (dictionary, configuration, options) => {
const { system, compiler } = configuration;
const { baseDir } = system;
const noMetadata = compiler?.noMetadata ?? _intlayer_config_defaultValues.COMPILER_NO_METADATA;
const { newDictionariesPath, localeList } = {
...defaultOptions,
...options
};
const newDictionaryLocationPath = (0, node_path.join)(baseDir, newDictionariesPath);
const existingDictionary = require_utils_readDictionariesFromDisk.readDictionariesFromDisk(configuration.system.unmergedDictionariesDir)[dictionary.key]?.find((el) => el.localId === dictionary.localId);
const formattedContentDeclaration = await formatContentDeclaration(dictionary, configuration, localeList);
if (existingDictionary?.filePath) {
const isSameContent = (0, node_util.isDeepStrictEqual)(existingDictionary, dictionary);
const filePath = (0, node_path.resolve)(configuration.system.baseDir, existingDictionary.filePath);
if (isSameContent) return {
status: "up-to-date",
path: filePath
};
await writeFileWithDirectories(filePath, formattedContentDeclaration, configuration, noMetadata);
return {
status: "updated",
path: filePath
};
}
if (dictionary.filePath) {
const filePath = (0, node_path.resolve)(configuration.system.baseDir, dictionary.filePath);
await writeFileWithDirectories(filePath, formattedContentDeclaration, configuration, noMetadata);
return {
status: "created",
path: filePath
};
}
const contentDeclarationPath = (0, node_path.join)(newDictionaryLocationPath, `${dictionary.key}.content.json`);
await writeFileWithDirectories(contentDeclarationPath, formattedContentDeclaration, configuration, noMetadata);
return {
status: "imported",
path: contentDeclarationPath
};
};
const writeFileWithDirectories = async (absoluteFilePath, dictionary, configuration, noMetadata) => {
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(absoluteFilePath), { recursive: true });
const extension = (0, node_path.extname)(absoluteFilePath);
if (extension === ".md" || extension === ".mdx") {
await require_writeContentDeclaration_writeMarkdownFile.writeMarkdownFile(absoluteFilePath, dictionary, configuration);
return;
}
if (extension === ".yaml" || extension === ".yml") {
await require_writeContentDeclaration_writeYamlFile.writeYamlFile(absoluteFilePath, dictionary, configuration);
return;
}
if ([
".json",
".jsonc",
".json5"
].includes(extension)) {
let fileContent = "{}";
if ((0, node_fs.existsSync)(absoluteFilePath)) try {
fileContent = await (0, node_fs_promises.readFile)(absoluteFilePath, "utf-8");
} catch {}
const transformedContent = require_writeContentDeclaration_transformJSONFile.transformJSONFile(fileContent, dictionary, noMetadata);
const tempDir = configuration.system?.tempDir;
if (tempDir) await (0, node_fs_promises.mkdir)(tempDir, { recursive: true });
const tempFileName = `${(0, node_path.basename)(absoluteFilePath)}.${Date.now()}-${Math.random().toString(36).slice(2)}.tmp`;
const tempPath = tempDir ? (0, node_path.join)(tempDir, tempFileName) : `${absoluteFilePath}.${tempFileName}`;
try {
await (0, node_fs_promises.writeFile)(tempPath, transformedContent, "utf-8");
await (0, node_fs_promises.rename)(tempPath, absoluteFilePath);
} catch (error) {
try {
await (0, node_fs_promises.rm)(tempPath, { force: true });
} catch {}
throw error;
}
const formatCommand = require_detectFormatCommand.detectFormatCommand(configuration);
if (formatCommand) try {
(0, node_child_process.execSync)(formatCommand.replace("{{file}}", absoluteFilePath), {
stdio: "inherit",
cwd: configuration.system.baseDir
});
} catch (error) {
console.error(error);
}
return;
}
await require_writeContentDeclaration_writeJSFile.writeJSFile(absoluteFilePath, dictionary, configuration, noMetadata);
try {
await (0, node_fs_promises.rm)((0, node_path.join)(configuration.system.cacheDir, "intlayer-prepared.lock"), { recursive: true });
} catch {}
};
//#endregion
exports.writeContentDeclaration = writeContentDeclaration;
//# sourceMappingURL=writeContentDeclaration.cjs.map