UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

125 lines 5.23 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var writeContentDeclaration_exports = {}; __export(writeContentDeclaration_exports, { writeContentDeclaration: () => writeContentDeclaration }); module.exports = __toCommonJS(writeContentDeclaration_exports); var import_fs = require("fs"); var fsPromises = __toESM(require("fs/promises")); var import_path = require("path"); var import_chokidar = require("@intlayer/chokidar"); var import_built = __toESM(require("@intlayer/config/built")); var import_dictionaries_entry = __toESM(require("@intlayer/dictionaries-entry")); var import_deep_equal = __toESM(require("deep-equal")); const DEFAULT_NEW_DICTIONARY_PATH = "intlayer-dictionaries"; const writeContentDeclaration = async (dictionary, config = import_built.default, newDictionariesPath) => { const { content } = config; const { baseDir } = content; const newDictionaryRelativeLocationPath = newDictionariesPath ?? DEFAULT_NEW_DICTIONARY_PATH; const newDictionaryLocationPath = `${baseDir}/${newDictionaryRelativeLocationPath}`; const existingDictionary = import_dictionaries_entry.default[dictionary.key]; const preparedContentDeclaration = await (0, import_chokidar.prepareContentDeclaration)(dictionary); const { filePath, $schema, ...dictionaryWithoutPath } = preparedContentDeclaration; const formattedContentDeclaration = { $schema: "https://intlayer.org/schema.json", ...dictionaryWithoutPath }; if (existingDictionary) { if ((0, import_deep_equal.default)(existingDictionary, dictionary)) { return { status: "up-to-date", path: filePath }; } else { if (filePath) { const isDictionaryJSON = filePath.endsWith(".json"); if (isDictionaryJSON) { const contentDeclarationPath = `${baseDir}/${filePath}`; await writeFileWithDirectories( contentDeclarationPath, formattedContentDeclaration ); return { status: "updated", path: contentDeclarationPath }; } else { await fsPromises.rm(filePath); const dictionariesDirPath = (0, import_path.dirname)(filePath); const dictionariesFileName = (0, import_path.basename)(filePath, (0, import_path.extname)(filePath)); const newFilePath = `${dictionariesDirPath}/${dictionariesFileName}.json`; await writeFileWithDirectories( newFilePath, formattedContentDeclaration ); return { status: "replaced", path: newFilePath }; } } else { const contentDeclarationPath = `${newDictionaryLocationPath}/${dictionary.key}.content.json`; await writeFileWithDirectories( contentDeclarationPath, formattedContentDeclaration ); return { status: "reimported in new location", path: contentDeclarationPath }; } } } else { const contentDeclarationPath = `${newDictionaryLocationPath}/${dictionary.key}.content.json`; await writeFileWithDirectories( contentDeclarationPath, formattedContentDeclaration ); return { status: "imported", path: contentDeclarationPath }; } }; const writeFileWithDirectories = async (filePath, data) => { try { const dir = (0, import_path.dirname)(filePath); const directoryExists = (0, import_fs.existsSync)(dir); if (!directoryExists) { await fsPromises.mkdir(dir, { recursive: true }); } const jsonDictionary = JSON.stringify(data, null, 2); await fsPromises.writeFile(filePath, jsonDictionary); } catch (error) { throw new Error(`Error writing file to ${filePath}: ${error}`); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { writeContentDeclaration }); //# sourceMappingURL=writeContentDeclaration.cjs.map