gavoja / aemsync

Adobe AEM Synchronization Tool
MIT License
136 stars 56 forks source link

Watching files not working on Windows #49

Closed piotr-andruszkiewicz-wttech closed 3 years ago

piotr-andruszkiewicz-wttech commented 4 years ago

Hello,

In our project we are using aemsync (which uses simple-watcher), wrapped with aem-sync-webpack-plugin. I know the latter uses aemsync v1.1.1, but while debugging I updated the dependency to 4.0.1 on my local copy. We use aemsync to watch changes and push clientlibs during development.

Unfortunately, the aemsync/simple-watcher is not working correctly on our Windows machines. What I mean is I was able to observe that it triggers the push only when webpack is starting, and ignores all future updates. It works fine on our Macs.

As a workaround we switched to using gaze, like this:

    gaze(options.watchDir + '/**/*.*', {interval: options.pushInterval}, function (err, watcher) {
        this.on('all', function (event, filepath) {
            aemsync.push(options.watchDir, options);
        });
        this.on('error', function (error) {
            console.debug('Error: ', error.code);
        });
    });

If you need any additional information, I'll be happy to provide it.

Best regards, Piotr

mmange commented 4 years ago

hi @piandrus-cog - Can you share more details on the work around? How do you hook this up with webpack?

piotr-andruszkiewicz-wttech commented 4 years ago

In webpack config we apply the aem-sync-webpack-plugin (pasting only relevant parts):

const AemSyncPlugin = require('../../clientlibs/aem-sync-webpack-plugin');
const { CLIENTLIBS_DIR, CLIENTLIBS } = require('../../clientlibs/const');

const devConfig = {
  plugins: [
    new AemSyncPlugin({
      targets: [process.env.AUTHOR_URL, process.env.PUBLISH_URL],
      watchDir: `../../${CLIENTLIBS_DIR}/${CLIENTLIBS.SITE}`,
      pushInterval: 1000,
    }),
  ],
};

Here is the modified aem-sync-webpack-plugin, which uses aemsync v4.0.1, with workaround applied:

// Copy of https://www.npmjs.com/package/aem-sync-webpack-plugin
// Slightly changed to workaround an issue with watching files changes on Windows.

const aemsync = require('aemsync');
const chalk = require('chalk');
const gaze = require('gaze');
const path = require('path');

const initAemSyncWatcher = function(options) {
  if (!(options && options.targets.length)) {
    console.error(chalk.red('AemSync targets property missing!'));
    return;
  } else if (!(options && options.watchDir)) {
    console.error(chalk.red('AemSync workingDir property missing!'));
    return;
  }

  options.onPushEnd = function(err, host) {
    if (err) {
      console.log('Error when pushing package', err);
    } else {
      console.log('Package pushed to ' + host);
    }
  };

  options.watchDir = path.resolve(options.watchDir);

  // aemsync(options.watchDir, options)
  gaze(options.watchDir + '/**/*.*', { interval: options.pushInterval }, function(err, watcher) {
    this.on('all', function(event, filepath) {
      aemsync.push(options.watchDir, options);
    });
    this.on('error', function(error) {
      console.debug('Error: ', error.code);
    });
  });
};

function AemSyncPlugin(options) {
  this.options = options;
}

AemSyncPlugin.prototype.apply = function(compiler) {
  if (process.argv.indexOf('--watch') !== -1) {
    initAemSyncWatcher(this.options);
  }
};

module.exports = AemSyncPlugin;
gavoja commented 3 years ago

My feeling is that the issue is related to the aem-sync-webpack-plugin package rather than aemsync itself.

I am using Windows myself and never run into issues with simple-watcher. The reason why I decided not to use any third party package for watching is because none I found was leveraging the recursive flag of Node's fs.watch(), which significantly improves performance on the supported platforms, especially for large directories. Both Chokidar and Gaze seem to iterate through the file tree instead. Chokidar would take up to 30 seconds to scan through one of my largest projects before being responsive, which was a no-go. In addition, simple-watcher has zero dependencies.

My first question would be: does the issue occur when using aemsync directly or only with aem-sync-webpack-plugin? If it occurs for the latter, the issue should be raised with the maintainer of the other package. Otherwise I am happy to help if you could provide me with the steps to reproduce.

gavoja commented 3 years ago

Closing due to no activity. Reopen if needed.