UNPKG

@intlayer/chokidar

Version:

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

154 lines 7 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var watcher_exports = {}; __export(watcher_exports, { buildAndWatchIntlayer: () => buildAndWatchIntlayer, handleAdditionalContentDeclarationFile: () => handleAdditionalContentDeclarationFile, handleContentDeclarationFileChange: () => handleContentDeclarationFileChange, handleUnlikedContentDeclarationFile: () => handleUnlikedContentDeclarationFile, watch: () => watch }); module.exports = __toCommonJS(watcher_exports); var import_path = require("path"); var import_config = require("@intlayer/config"); var import_chokidar = require("chokidar"); var import_getBuiltDictionariesPath = require('../getBuiltDictionariesPath.cjs'); var import_loadLocalDictionaries = require('../loadDictionaries/loadLocalDictionaries.cjs'); var import_declaration_file_to_dictionary = require('../transpiler/declaration_file_to_dictionary/index.cjs'); var import_createDictionaryEntryPoint = require('../transpiler/dictionary_to_main/createDictionaryEntryPoint.cjs'); var import_dictionary_to_type = require('../transpiler/dictionary_to_type/index.cjs'); var import_prepareIntlayer = require('../prepareIntlayer.cjs'); var import_listDictionariesPath = require('../listDictionariesPath.cjs'); const recentlyAddedFiles = /* @__PURE__ */ new Set(); const handleAdditionalContentDeclarationFile = async (filePath, configuration) => { const { content } = configuration ?? (0, import_config.getConfiguration)({ verbose: true }); (0, import_config.appLogger)( `Additional file detected: ${(0, import_path.relative)(content.baseDir, filePath)}`, { isVerbose: true } ); const localeDictionaries = await (0, import_loadLocalDictionaries.loadLocalDictionaries)(filePath); const dictionariesPaths = await (0, import_declaration_file_to_dictionary.buildDictionary)(localeDictionaries); (0, import_dictionary_to_type.createTypes)(dictionariesPaths); (0, import_createDictionaryEntryPoint.createDictionaryEntryPoint)(); (0, import_config.appLogger)("Dictionaries built", { isVerbose: true }); (0, import_dictionary_to_type.createModuleAugmentation)(); (0, import_config.appLogger)("Module augmentation built", { isVerbose: true }); }; const handleUnlikedContentDeclarationFile = async (filePath, configuration) => { const { content } = configuration ?? (0, import_config.getConfiguration)({ verbose: true }); (0, import_config.appLogger)(`Unlinked detected: ${(0, import_path.relative)(content.baseDir, filePath)}`, { isVerbose: true }); const files = (0, import_listDictionariesPath.listDictionaries)(configuration); const localeDictionaries = await (0, import_loadLocalDictionaries.loadLocalDictionaries)(files); const dictionariesPaths = await (0, import_declaration_file_to_dictionary.buildDictionary)(localeDictionaries); (0, import_dictionary_to_type.createTypes)(dictionariesPaths); (0, import_createDictionaryEntryPoint.createDictionaryEntryPoint)(); (0, import_config.appLogger)("Dictionaries rebuilt", { isVerbose: true }); (0, import_dictionary_to_type.createModuleAugmentation)(); (0, import_config.appLogger)("Module augmentation built", { isVerbose: true }); }; const handleContentDeclarationFileChange = async (filePath, configuration) => { const { content } = configuration ?? (0, import_config.getConfiguration)({ verbose: true }); (0, import_config.appLogger)(`Change detected: ${(0, import_path.relative)(content.baseDir, filePath)}`, { isVerbose: true }); const localeDictionaries = await (0, import_loadLocalDictionaries.loadLocalDictionaries)(filePath); const updatedDictionariesPaths = await (0, import_declaration_file_to_dictionary.buildDictionary)(localeDictionaries); const allDictionariesPaths = (0, import_getBuiltDictionariesPath.getBuiltDictionariesPath)(); (0, import_dictionary_to_type.createTypes)(updatedDictionariesPaths); (0, import_config.appLogger)("TypeScript types built", { isVerbose: true }); if (updatedDictionariesPaths.some( (updatedDictionaryPath) => !allDictionariesPaths.includes(updatedDictionaryPath) )) { (0, import_createDictionaryEntryPoint.createDictionaryEntryPoint)(); (0, import_config.appLogger)("Dictionary list built", { isVerbose: true }); } }; const watch = (options) => { const configuration = options?.configuration ?? (0, import_config.getConfiguration)({ verbose: true }); const { watch: isWatchMode, watchedFilesPatternWithPath } = configuration.content; return (0, import_chokidar.watch)(watchedFilesPatternWithPath, { persistent: isWatchMode, // Make the watcher persistent ignoreInitial: true, // Process existing files ...options }).on("add", async (filePath) => { const fileName = (0, import_path.basename)(filePath); recentlyAddedFiles.add(fileName); await handleAdditionalContentDeclarationFile(filePath, configuration); setTimeout(() => recentlyAddedFiles.delete(fileName), 1e3); }).on( "change", async (filePath) => await handleContentDeclarationFileChange(filePath, configuration) ).on("unlink", async (filePath) => { setTimeout(async () => { const fileName = (0, import_path.basename)(filePath); if (recentlyAddedFiles.has(fileName)) { return; } await handleUnlikedContentDeclarationFile(filePath, configuration); }, 300); }).on("error", async (error) => { (0, import_config.appLogger)("Watcher error: " + error, { level: "error" }); (0, import_config.appLogger)("Restarting watcher"); await (0, import_prepareIntlayer.prepareIntlayer)(configuration); }); }; const buildAndWatchIntlayer = async (options) => { const configuration = options?.configuration ?? (0, import_config.getConfiguration)(); await (0, import_prepareIntlayer.prepareIntlayer)(configuration); if (configuration.content.watch || options.persistent) { watch({ ...options, configuration }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildAndWatchIntlayer, handleAdditionalContentDeclarationFile, handleContentDeclarationFileChange, handleUnlikedContentDeclarationFile, watch }); //# sourceMappingURL=watcher.cjs.map