feross / hostile

Simple, programmatic `/etc/hosts` manipulation (in node.js)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple entries not being added

lgomez opened this issue · comments

Hi,

I'm not sure why this does not work but only the last entry actually gets added.

var currentPrototypes = fs.readdirSync(path.join(__dirname, '..', 'prototypes'));
currentPrototypes.forEach(function(prototype){
    console.log(prototype);
    hostile.set('127.0.0.1', prototype + '.ux.localhost', function (err) {
      if (err) {
        console.error(err);
      } else {
        console.log('set /etc/hosts successfully!');
      }
    });
});

Where currentPrototypes is a list of subfolders in the prototypes directory.

Also, if I run it again, it adds another identical entry where it should fail silently.

Any ideas?

Thank you,

You need to wait until the callback has fired before calling hostile.set again, or else you will be doing multiple writes at the same time to the same file, which means that only the last write will win.

You might consider using a library like run-series to help you with this.

Perhaps hostile should support expose a function that takes multiple values and uses run-series internally. PR welcome :)