How to use hooker - 10 common examples

To help you get started, we’ve selected a few hooker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jwarby / i18n-lint / test / lib / reporters / default.js View on Github external
});

    var expected = fs.readFileSync(
      __dirname + '/../../expected/reporters/default.txt'
    ).toString();

    // Execute method under test
    try {
      reporter(errors);
    } catch(e) {
      hooker.unhook(process.stdout, 'write');
      console.log(e);
      console.log(e.stack);
    }

    hooker.unhook(process.stdout, 'write');
    expect(actual).to.equal(expected);

    done();
  });
github jwarby / i18n-lint / test / lib / reporters / unix.js View on Github external
it('should not output anything if there are no errors', function() {
    var actual = '';

    hooker.hook(process.stdout, 'write', {
      pre: function(out) {
        actual += out;

        return hooker.preempt();
      }
    });

    // Execute
    reporter([]);

    hooker.unhook(process.stdout, 'write');

  });
});
github sitespeedio / grunt-sitespeedio / test / testBudgetOutput.js View on Github external
var stdoutEqual = function(callback, done) {
  var actual = '';
  // Hook process.stdout.write
  hooker.hook(grunt.log, ['error', 'ok'], {
    // This gets executed before the original process.stdout.write.
    pre: function(result) {
      // Concatenate uncolored result onto actual.
      actual += grunt.log.uncolor(result);
      // Prevent the original process.stdout.write from executing.
      return hooker.preempt();
    }
  });
  // Execute the logging code to be tested.
  callback();
  // Restore process.stdout.write to its original value.
  hooker.unhook(grunt.log, ['error', 'ok']);
  // Actually test the actually-logged stdout string to the expected value.
  done(actual);
};
github catjsteam / catjs / test / projectA / Gruntfile.js View on Github external
var hooker = require('hooker');
    _fs.writeSync(_logFileStream, [" ", new Date(), ""].join("\n"));
    _fs.writeSync(_logFileStream, ["--------------------------------------------------------------", ""].join("\n"));

    // Override grunt.log.header to update a per-line prefix and prevent default logging.
    var prefix;
    hooker.hook(grunt.log, 'header', function () {
        prefix = '[' + grunt.task.current.nameArgs + '] ';
        return hooker.preempt();
    });

    // Override process.stdout to log the name+args of the current task before
    // every logged line.
    var newline = true;
    hooker.hook(process.stdout, 'write', function (str) {
        var ret;
        str = String(str);
        if (newline) {
            if (str === '\n') {
                return hooker.preempt();
            } else if (prefix) {
                str = prefix + str.replace(/(\n)(?!$)/g, '$1' + prefix);
            }
        }

        newline = str.slice(-1) === '\n';
        ret = hooker.filter(this, [str]);

        _fs.writeSync(_logFileStream, str);

        return ret;
github pghalliday / grunt-mocha-test / tasks / mocha.js View on Github external
run(function(error, failureCount) {
      // close the file if it was opened
      if (fd) {
        fs.closeSync(fd);
      }
      // Restore process.stdout.write to its original value
      hooker.unhook(process.stdout, 'write');
      // Actually test the actually-logged stdout string to the expected value
      done(error, failureCount);
    });
  };
github sitespeedio / grunt-sitespeedio / test / testBudgetOutput.js View on Github external
var stdoutEqual = function(callback, done) {
  var actual = '';
  // Hook process.stdout.write
  hooker.hook(grunt.log, ['error', 'ok'], {
    // This gets executed before the original process.stdout.write.
    pre: function(result) {
      // Concatenate uncolored result onto actual.
      actual += grunt.log.uncolor(result);
      // Prevent the original process.stdout.write from executing.
      return hooker.preempt();
    }
  });
  // Execute the logging code to be tested.
  callback();
  // Restore process.stdout.write to its original value.
  hooker.unhook(grunt.log, ['error', 'ok']);
  // Actually test the actually-logged stdout string to the expected value.
  done(actual);
};
github jwarby / i18n-lint / test / lib / reporters / json.js View on Github external
hooker.hook(process.stdout, 'write', {
      pre: function(out) {
        actual += stripAnsi(out);

        return hooker.preempt();
      }
    });

    var expected = fs.readFileSync(
      __dirname + '/../../expected/reporters/json.txt'
    ).toString();

    // Execute method under test
    reporter(errors);

    hooker.unhook(process.stdout, 'write');
    expect(actual).to.equal(expected);

    // Test that valid json is output
    expect(JSON.stringify.bind(JSON, actual)).not.to.throw.error;

    done();
  });
github catjsteam / catjs / src / libraries / cat / Gruntfile.js View on Github external
var hooker = require('hooker');
    _fs.writeSync(_logFileStream, [" ", new Date(), ""].join("\n"));
    _fs.writeSync(_logFileStream, ["--------------------------------------------------------------", ""].join("\n"));

    // Override grunt.log.header to update a per-line prefix and prevent default logging.
    var prefix;
    hooker.hook(grunt.log, 'header', function () {
        prefix = '[' + grunt.task.current.nameArgs + '] ';
        return hooker.preempt();
    });

    // Override process.stdout to log the name+args of the current task before
    // every logged line.
    var newline = true;
    _fs.writeSync(_logFileStream, _path.resolve("."));
    hooker.hook(process.stdout, 'write', function (str) {
        var ret;
        str = String(str);
        if (newline) {
            if (str === '\n') {
                return hooker.preempt();
            } else if (prefix) {
                str = prefix + str.replace(/(\n)(?!$)/g, '$1' + prefix);
            }
        }

        newline = str.slice(-1) === '\n';
        ret = hooker.filter(this, [str]);


        _fs.writeSync(_logFileStream, str);
        //console.log("[grunt process] see cat.test.log for details: ", str);
github org-scn-design-studio-community / sdkpackage / src / org.scn.community.geovis / script / node_modules / grunt / node_modules / grunt-legacy-log / index.js View on Github external
Log.prototype.initColors = function() {
  if (this.option('no-color')) {
    // String color getters should just return the string.
    colors.mode = 'none';
    // Strip colors from strings passed to console.log.
    hooker.hook(console, 'log', function() {
      var args = _.toArray(arguments);
      return hooker.filter(this, args.map(function(arg) {
        return typeof arg === 'string' ? colors.stripColors(arg) : arg;
      }));
    });
  }
};
github sindresorhus / time-grunt / index.js View on Github external
process.once('timegruntexit', function (exitCode) {
		clearInterval(interval);

		process.exit = originalExit;
		hooker.unhook(grunt.log, 'header');

		var diff = Date.now() - prevTime;

		if (prevTaskName) {
			tableData.push([prevTaskName, diff]);
		}

		// `grunt.log.header` should be unhooked above, but in some cases it's not
		log('\n\n' + chalk.underline('Execution Time') + chalk.gray(' (' + startTimePretty + ')'));
		log(formatTable(tableData) + '\n');

		if (cb) {
			cb(tableData, function () {
				process.exit(exitCode);
			});

hooker

Monkey-patch (hook) functions for debugging and stuff.

MIT
Latest version published 13 years ago

Package Health Score

65 / 100
Full package analysis