UNPKG

@intlayer/chokidar

Version:

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

97 lines (95 loc) 5.82 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_createDictionaryEntryPoint_createDictionaryEntryPoint = require('./createDictionaryEntryPoint/createDictionaryEntryPoint.cjs'); const require_utils_readDictionariesFromDisk = require('./utils/readDictionariesFromDisk.cjs'); const require_writeJsonIfChanged = require('./writeJsonIfChanged.cjs'); let node_fs_promises = require("node:fs/promises"); let node_path = require("node:path"); let _intlayer_config_client = require("@intlayer/config/client"); let _intlayer_config_logger = require("@intlayer/config/logger"); let fast_glob = require("fast-glob"); fast_glob = require_runtime.__toESM(fast_glob); //#region src/cleanRemovedContentDeclaration.ts const cleanRemovedContentDeclaration = async (filePath, keysToKeep, configuration) => { const appLogger = (0, _intlayer_config_logger.getAppLogger)(configuration); const unmergedDictionaries = require_utils_readDictionariesFromDisk.readDictionariesFromDisk(configuration.system.unmergedDictionariesDir); const baseDir = configuration.system.baseDir; const relativeFilePath = (0, node_path.relative)(baseDir, filePath); const uniqueUnmergedDictionaries = Object.values(unmergedDictionaries).flat().filter((dictionary) => dictionary.filePath === relativeFilePath && !keysToKeep.includes(dictionary.key)).filter((dictionary, index, self) => index === self.findIndex((t) => t.key === dictionary.key)); const changedDictionariesLocalIds = []; const filesToRemove = []; const excludeKeys = []; await Promise.all(uniqueUnmergedDictionaries.map(async (dictionary) => { const unmergedFilePath = (0, node_path.normalize)((0, node_path.join)(configuration.system.unmergedDictionariesDir, `${dictionary.key}.json`)); try { const jsonContent = await (0, node_fs_promises.readFile)(unmergedFilePath, "utf8"); const parsedContent = JSON.parse(jsonContent); if (parsedContent.length === 1) { if (parsedContent[0].filePath === relativeFilePath) { appLogger(`Removing outdated dictionary ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)}`, { isVerbose: true }); filesToRemove.push(unmergedFilePath); excludeKeys.push(dictionary.key); } } else { await require_writeJsonIfChanged.writeJsonIfChanged(unmergedFilePath, parsedContent.filter((content) => content.filePath !== relativeFilePath)); changedDictionariesLocalIds.push(dictionary.localId); } } catch (error) { if (error.code === "ENOENT") { if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key); } } })); const dictionaries = require_utils_readDictionariesFromDisk.readDictionariesFromDisk(configuration.system.dictionariesDir); const uniqueMergedDictionaries = (Object.values(dictionaries)?.filter((dictionary) => !keysToKeep.includes(dictionary.key) && dictionary.localIds?.length === 1 && dictionary.localIds[0].endsWith(`::local::${relativeFilePath}`))).filter((dictionary, index, self) => index === self.findIndex((t) => t.key === dictionary.key)); await Promise.all(uniqueMergedDictionaries.map(async (dictionary) => { const mergedFilePath = (0, node_path.normalize)((0, node_path.join)(configuration.system.dictionariesDir, `${dictionary.key}.json`)); try { const fileContent = await (0, node_fs_promises.readFile)(mergedFilePath, "utf8"); const parsedContent = JSON.parse(fileContent); if (parsedContent.localIds?.length === 1) { if (parsedContent.localIds[0].endsWith(`::local::${relativeFilePath}`)) { appLogger(`Removing outdated unmerged dictionary ${(0, _intlayer_config_logger.colorizeKey)(dictionary.key)}`, { isVerbose: true }); filesToRemove.push(mergedFilePath); const typesFilePath = (0, node_path.normalize)((0, node_path.join)(configuration.system.typesDir, `${dictionary.key}.ts`)); filesToRemove.push(typesFilePath); const dynamicFiles = await (0, fast_glob.default)((0, _intlayer_config_client.normalizePath)((0, node_path.join)(configuration.system.dynamicDictionariesDir, `${dictionary.key}.*`)), { absolute: true }); filesToRemove.push(...dynamicFiles); if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key); } } else { const localIds = parsedContent.localIds?.filter((localeId) => !localeId.endsWith(`::local::${relativeFilePath}`)); await require_writeJsonIfChanged.writeJsonIfChanged(mergedFilePath, { ...parsedContent, localIds }); } } catch (error) { if (error.code === "ENOENT") { if (!excludeKeys.includes(dictionary.key)) excludeKeys.push(dictionary.key); const typesFilePath = (0, node_path.normalize)((0, node_path.join)(configuration.system.typesDir, `${dictionary.key}.ts`)); filesToRemove.push(typesFilePath); } } })); if (filesToRemove.length > 0 || excludeKeys.length > 0) { await require_createDictionaryEntryPoint_createDictionaryEntryPoint.createDictionaryEntryPoint(configuration, { excludeKeys }); if (filesToRemove.length > 0) setTimeout(async () => await Promise.all(filesToRemove.map(async (path) => { const relativePath = (0, node_path.relative)(baseDir, path); try { await (0, node_fs_promises.rm)(path, { force: true }); appLogger(`Deleted artifact: ${(0, _intlayer_config_logger.colorizePath)(relativePath)}`, { isVerbose: true }); } catch { appLogger(`Error while removing file ${(0, _intlayer_config_logger.colorizePath)(relativePath)}`, { isVerbose: true }); } })), 3e3); } return { changedDictionariesLocalIds, excludeKeys, hasRebuilt: filesToRemove.length > 0 || excludeKeys.length > 0 }; }; //#endregion exports.cleanRemovedContentDeclaration = cleanRemovedContentDeclaration; //# sourceMappingURL=cleanRemovedContentDeclaration.cjs.map