@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
132 lines • 4.66 kB
JavaScript
import { basename, relative } from "path";
import {
appLogger,
getConfiguration
} from "@intlayer/config";
import { watch as chokidarWatch } from "chokidar";
import { getBuiltDictionariesPath } from "../getBuiltDictionariesPath.mjs";
import { loadLocalDictionaries } from "../loadDictionaries/loadLocalDictionaries.mjs";
import { buildDictionary } from "../transpiler/declaration_file_to_dictionary/index.mjs";
import { createDictionaryEntryPoint } from "../transpiler/dictionary_to_main/createDictionaryEntryPoint.mjs";
import {
createTypes,
createModuleAugmentation
} from "../transpiler/dictionary_to_type/index.mjs";
import { prepareIntlayer } from "../prepareIntlayer.mjs";
import { listDictionaries } from "../listDictionariesPath.mjs";
const recentlyAddedFiles = /* @__PURE__ */ new Set();
const handleAdditionalContentDeclarationFile = async (filePath, configuration) => {
const { content } = configuration ?? getConfiguration({
verbose: true
});
appLogger(
`Additional file detected: ${relative(content.baseDir, filePath)}`,
{
isVerbose: true
}
);
const localeDictionaries = await loadLocalDictionaries(filePath);
const dictionariesPaths = await buildDictionary(localeDictionaries);
createTypes(dictionariesPaths);
createDictionaryEntryPoint();
appLogger("Dictionaries built", {
isVerbose: true
});
createModuleAugmentation();
appLogger("Module augmentation built", {
isVerbose: true
});
};
const handleUnlikedContentDeclarationFile = async (filePath, configuration) => {
const { content } = configuration ?? getConfiguration({
verbose: true
});
appLogger(`Unlinked detected: ${relative(content.baseDir, filePath)}`, {
isVerbose: true
});
const files = listDictionaries(configuration);
const localeDictionaries = await loadLocalDictionaries(files);
const dictionariesPaths = await buildDictionary(localeDictionaries);
createTypes(dictionariesPaths);
createDictionaryEntryPoint();
appLogger("Dictionaries rebuilt", {
isVerbose: true
});
createModuleAugmentation();
appLogger("Module augmentation built", {
isVerbose: true
});
};
const handleContentDeclarationFileChange = async (filePath, configuration) => {
const { content } = configuration ?? getConfiguration({
verbose: true
});
appLogger(`Change detected: ${relative(content.baseDir, filePath)}`, {
isVerbose: true
});
const localeDictionaries = await loadLocalDictionaries(filePath);
const updatedDictionariesPaths = await buildDictionary(localeDictionaries);
const allDictionariesPaths = getBuiltDictionariesPath();
createTypes(updatedDictionariesPaths);
appLogger("TypeScript types built", {
isVerbose: true
});
if (updatedDictionariesPaths.some(
(updatedDictionaryPath) => !allDictionariesPaths.includes(updatedDictionaryPath)
)) {
createDictionaryEntryPoint();
appLogger("Dictionary list built", {
isVerbose: true
});
}
};
const watch = (options) => {
const configuration = options?.configuration ?? getConfiguration({
verbose: true
});
const { watch: isWatchMode, watchedFilesPatternWithPath } = configuration.content;
return chokidarWatch(watchedFilesPatternWithPath, {
persistent: isWatchMode,
// Make the watcher persistent
ignoreInitial: true,
// Process existing files
...options
}).on("add", async (filePath) => {
const fileName = 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 = basename(filePath);
if (recentlyAddedFiles.has(fileName)) {
return;
}
await handleUnlikedContentDeclarationFile(filePath, configuration);
}, 300);
}).on("error", async (error) => {
appLogger("Watcher error: " + error, {
level: "error"
});
appLogger("Restarting watcher");
await prepareIntlayer(configuration);
});
};
const buildAndWatchIntlayer = async (options) => {
const configuration = options?.configuration ?? getConfiguration();
await prepareIntlayer(configuration);
if (configuration.content.watch || options.persistent) {
watch({ ...options, configuration });
}
};
export {
buildAndWatchIntlayer,
handleAdditionalContentDeclarationFile,
handleContentDeclarationFileChange,
handleUnlikedContentDeclarationFile,
watch
};
//# sourceMappingURL=watcher.mjs.map