npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-pretest

v1.0.0

Published

Grunt plugin to run task only when specified tests are passed

Downloads

14

Readme

grunt-pretest

Grunt plugin to run task only when specified tests are passed.

NPM version License Build Status

Getting Started

This plugin requires Grunt >=0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-pretest --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-pretest');

The "pretest" task

Overview

The pretest task is multi task.

In your project's Gruntfile, add a section named pretest to the data object passed into grunt.initConfig().

grunt.initConfig({
  pretest: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      options: {
        // Target-specific options go here.
      }
    },
  },
});

Options

options.test

Type: any
Default value: true

Specifies test or array of tests that should be passed to run a task. Any test that is not a function is converted to boolean value. If test is a function, its result is used as test value. The following parameters are passed into the function:

  • reference to the data object (see below)
  • reference to grunt object

If test function returns a non-empty string, a non-empty array or a function, the returned value will be used as task that should be run instead of task option.

The task described in task option or returned from a test function will be run only if:

  • results of all tests have true value when testConnect option is and
  • result of any test has true value when testConnect option is or

options.testConnect

Type: String
Default value: and

A boolean connector that should be used when array of tests is specified in test option. Valid values are the following: and, or (case-insensitive). Any other value is treated as and.

options.task

Type: String | Array of String | Function

Describes task(s) that should be run when test is passed. This option can be omitted if test function returns the necessary task(s). Each string should specify task in usual format (e.g. concat:foo, bar:testing:123). If a function is set as task, the following parameters will be passed into the function:

  • reference to the data object (see below)
  • reference to grunt object

If task function returns a non-empty string or a non-empty array, the returned value will be used as argument for grunt.task.run (i.e. defines task(s) that should be run after the processed pretest task).

Data object

Data object that is passed into test and task function has the following fields:

  • task - value of task option
  • pretest - reference to the object that represents the processed pretest task (see Inside Tasks for available properties)
  • options - options of the processed pretest task
  • source - a source item that is being processed

Usage examples

In this example, the concat task will be run only if directory specified by configDir configuration parameter exists.

grunt.initConfig({
    pretest: {
        configDir: {
            options: {
                test: function(data, grunt) {
                    return grunt.file.isDir(grunt.config.get("configDir"));
                },
                task: 'concat:config_files'
            }
        }
    }
});

In the following example, the test function returns a task that should be run depending on value of target option.

grunt.initConfig({
    pretest: {
        configDir: {
            options: {
                test: function(data, grunt) {
                    var target = grunt.option("target");
                    return target
                            ? "prepare:" + (target === "prod" ? "product" : "dev")
                            : false;
                }
            }
        }
    }
});

It is possible to specify sources for which tests and tasks should be run.

grunt.initConfig({
    pretest: {
        sourceList: {
            src: ["a.json", "b.xml", "c.data"],
            options: {
                test: function(data, grunt) {
                    var runTask = true;
                    try {
                        fs.statSync(data.source);
                    }
                    catch (e) {
                        runTask = false;
                    }
                    return runTask;
                },
                task: function(data, grunt) {
                    return "some-task:" + data.source;
                }
            }
        }
    }
});

See Gruntfile.js for more examples.

Related projects

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 1.0.0 / 2016-03-18

    • Change parameters for test and task functions.
    • Add ability to specify sources for which tests and tasks should be run (resolve #2).
  • 0.2.0 / 2016-03-07

    • Add support for Grunt 1.0.
  • 0.1.0 / 2016-02-03

    • task option can be omitted if test function returns task that should be run.
    • Task function can return task that should be run after the processed pretest task.