# Code coverage report for lib/config/watchable.js

## Statements: 85.71% (30 / 35) Branches: 62.5% (5 / 8) Functions: 100% (7 / 7) Lines: 85.71% (30 / 35)

All files » lib/config/ » watchable.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    751
    1

    1

    1
    1

    1
    1

    1

    1
    1

    1
    1
    1

    1
    2

    1

    1

    1

    1

    1
    1
    1

    1
    1
    3
    3
    2

    1
    1

    1'use strict';
    var utils = require('../utils'),
        path = require('path'),
        crypto = require('crypto'),
        fs = require('fs'),
        watchFileName,
        watchFile;

    // Attempts to see if fs.watch will work. On some platforms, it doesn't.
    // See: http://nodejs.org/api/fs.html#fs_caveats
    // Sends the callback true if fs.watch will work, false if it won't
    //
    // Caveats:
    // If there is no writable tmp directory, it will also return true, although
    // a warning message will be displayed.
    var changeDetected = false;

    function check(cb) {
      var tmpdir;

      watchable.cb = cb;
      changeDetected = false;

      Iif (utils.isWindows) {
        tmpdir = process.env.TEMP;
      } else Eif (process.env.TMPDIR) {
        tmpdir = process.env.TMPDIR;
      } else {
        tmpdir = '/tmp';
      }

      watchFileName = path.join(tmpdir, 'nodemonCheckFsWatch' + crypto.randomBytes(16).toString('hex'));
      watchFile = fs.openSync(watchFileName, 'w');
      Iif (watchFile < 0) {
        utils.log.fail('Unable to write to temp directory. If you experience problems with file reloading, ensure ' + tmpdir + ' is writable.');
        cb(true);
        return;
      }
      fs.watch(watchFileName, function() {
        cb(true);
      });

      fs.watchFile(watchFileName, function () {});

      setTimeout(function () {
        // This should trigger fs.watch, if it works
        fs.writeSync(watchFile, '1');

        // higher timeout to allow for windows to trigger the watch event
        setTimeout(finish, 1000);
      }, 250);

    }

    // Verifies that fs.watch was not triggered and sends false to the callback
    // but if the callback has already been used (changeDetected), it won't call.
    var finish = function() {
      fs.unlinkSync(watchFileName);
      watchable.cb(false);
    };

    var watchable = module.exports = function (config, ready) {
      check(function(success) {
        config.system.useWatch = success;
        if (changeDetected) {
          utils.bus.emit('config:update');
        } else {
          changeDetected = true; // prevents the `ready` being called twice
          ready();
        }
      });
    };

    watchable.check = check;