UNPKG

@intlayer/chokidar

Version:

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

124 lines 5.35 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_built = __toESM(require("@intlayer/config/built")); var import_unmerged_dictionaries_entry = __toESM(require("@intlayer/unmerged-dictionaries-entry")); var import_deep_equal = __toESM(require("deep-equal")); var import_fs = require("fs"); var import_promises = require("fs/promises"); var import_path = require("path"); var import_prepareContentDeclaration = require('../prepareContentDeclaration.cjs'); var import_writeJSFile = require('./writeJSFile.cjs'); const DEFAULT_NEW_DICTIONARY_PATH = "intlayer-dictionaries"; const formatContentDeclaration = async (dictionary) => { const preparedContentDeclaration = await (0, import_prepareContentDeclaration.prepareContentDeclaration)(dictionary); const { filePath, $schema, ...dictionaryWithoutPath } = preparedContentDeclaration; const formattedContentDeclaration = { $schema: "https://intlayer.org/schema.json", ...dictionaryWithoutPath }; return formattedContentDeclaration; }; 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_unmerged_dictionaries_entry.default[dictionary.key].filter((el) => el.filePath === dictionary.filePath); const filePath = dictionary.filePath; const formattedContentDeclaration = await formatContentDeclaration(dictionary); if (existingDictionary) { if ((0, import_deep_equal.default)(existingDictionary, dictionary)) { return { status: "up-to-date", path: filePath }; } else { if (filePath) { await writeFileWithDirectories(filePath, formattedContentDeclaration); return { status: "updated", path: filePath }; } 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 (0, import_promises.mkdir)(dir, { recursive: true }); } const extention = (0, import_path.extname)(filePath); const acceptedExtensions = import_built.default.content.fileExtensions.map( (extention2) => (0, import_path.extname)(extention2) ); if (!acceptedExtensions.includes(extention)) { throw new Error( `Invalid file extension: ${extention}, file: ${filePath}` ); } if (extention === ".json") { const jsonDictionary = JSON.stringify(data, null, 2); await (0, import_promises.writeFile)(filePath, jsonDictionary); } else { await (0, import_writeJSFile.writeJSFile)(filePath, data); } } 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