Skip to content

Commit

Permalink
fix: Handle watch events on macOS caused by git (#1696)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Feb 4, 2025
1 parent 9e73cbe commit c4c5986
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/next-intl/src/plugin/createMessagesDeclaration.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import {throwError} from './utils.js';
import watchFile from './watchFile.js';

function runOnce(fn: () => void) {
if (process.env._NEXT_INTL_COMPILE_MESSAGES === '1') {
Expand Down Expand Up @@ -40,10 +41,8 @@ export default function createMessagesDeclaration(messagesPath: string) {
}

function startWatching(messagesPath: string) {
const watcher = fs.watch(messagesPath, (eventType) => {
if (eventType === 'change') {
compileDeclaration(messagesPath, true);
}
const watcher = watchFile(messagesPath, () => {
compileDeclaration(messagesPath, true);
});

process.on('exit', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/next-intl/src/plugin/watchFile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import fs from 'fs';
import path from 'path';

/**
* Wrapper around `fs.watch` that provides a workaround
* for https://github.com/nodejs/node/issues/5039.
*/
export default function watchFile(filepath: string, callback: () => void) {
const directory = path.dirname(filepath);
const filename = path.basename(filepath);

return fs.watch(
directory,
{persistent: false, recursive: false},
(event, changedFilename) => {
if (changedFilename === filename) {
callback();
}
}
);
}

0 comments on commit c4c5986

Please sign in to comment.