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

gruntfile-api

v0.0.10

Published

Api to programmatically modify a gruntfile

Downloads

6

Readme

gruntfile-api NPM version Build Status dependencies

Api to programmatically modify your gruntfile

Getting Started

Install the module with: npm install gruntfile-api

var api = require('gruntfile-api'),
    fs = require('fs'),
    gruntfileData = fs.readFileSync('Gruntfile.js');

var output = api.init(gruntfileData)
    // change something
    .toString();

Documentation

Overview

Add global declaration

Add a global variable declaration to the gruntfile. declarations get seamlessly integrated into existing declaration structures. Keep in mind that function calls like require('module') will be evaluated when passed to the function line this Use 'addGlobalDeclarationRaw` to prevent evaluation.

api.addGlobalDeclaration(identifier,value)

parameter.identifier

Type: string

parameter.value

Type: mixed

example

api.addGlobalDeclaration('defaultTasks',['jshint','uglify'])

adds the following code right before module.exports = function (grunt)

var defaultTasks = ['jshint','uglify'];

or

var varA = 'something',
    varB = 'something else',
    defaultTasks = ['jshint','uglify'];

Add RAW global declaration

Add a global variable declaration to the gruntfile. declarations get seamlessly integrated into existing declaration structures

api.addGlobalDeclarationRaw(identifier,value)

parameter.identifier

Type: string

parameter.value

Type: string

example

api.addGlobalDeclaration('path','require(\'path\')')

adds the following code right before module.exports = function (grunt)

var path = require('path');

Register task

Register grunt task with grunt.registerTask. when there already is a task registered with the same identifier. The tasks will get merged based on the mergeType argument unless this one is invalid or skip Merge will be done in one of the following ways depending on the mergeType:

  • registered task is array and task is array -> default merge
  • registered task is function and task is array -> merge will add grunt.task.run(tasks) to registered task body
  • registered task is array and task is function -> merge will add grunt.task.run(registered tasks) to task body
  • registered task is function and task is function -> merge will add task function body to registered task function body
api.registerTask(identifier,value)

parameter.identifier

Type: string

The task identifier

parameter.value

Type: array|function

The task which are invoked

parameter.mergeType

Type: string can be one of the following: ['prepend','append','overwrite','skip'] Default: 'append'

How should tasks should be merged when there already is a task with the same identifier registered

example

api.registerTask('default',['jshint','uglify'])

adds the following code to the gruntfile

grunt.registerTask('default', ['jshint', 'uglify']);

merge example

api.registerTask('default',['jshint'],'prepend');

gruntfile before

grunt.registerTask('default', function(target) {
	grunt.task.run(['uglify']);
};

gruntfile after

grunt.registerTask('default', function(target) {
	grunt.task.run(['jshint']);
	grunt.task.run(['uglify']);
};

Insert task config

Insert Task configuration to the Gruntfile. Existing configurations should not be overwritten. That means, that the task target is added to the config if it already exists. Options will be added to the target configuration when the task already exists so that any existing configuration won't be messed up. Options that are already configured identically in the global task options will be dropped. Keep in mind that variable names or function calls will be evaluated when passed to the function line this When there's the need for variables or date objects use insertRawConfig

api.insertConfig(name,descriptor)

parameter.name

Type: string

The task identifier

parameter.descriptor

Type: mixed

The task configuration

example

api.insertConfig('watch', {
    gruntfile: {
        options: {
            time: (new Date()).getTime()
        },
        files: 'Gruntfile.js',
        tasks: ['jshint:gruntfile']
    }
})

adds the following code to the gruntfile

watch: {
    gruntfile: {
        options: {
            time: 1394485101147
        },
        files: 'Gruntfile.js',
        tasks: ['jshint:gruntfile']
    }
}

or adds the watch target to an existing watch configuration

watch: {
    lib: {
        files: 'lib/**/*.js',
        tasks: ['jshint:lib', 'nodeunit']
    },
    gruntfile: {
        options: {
            time: 1394485101147
        },
        files: 'Gruntfile.js',
        tasks: ['jshint:gruntfile']
    }
}

Insert RAW task config

Insert task configuration to the Gruntfile as String to prevent code evaluation

api.insertRawConfig(name,descriptor)

parameter.name

Type: string

The task identifier

parameter.descriptor

Type: string

The task configuration as string.

example

api.insertRawConfig('watch', "{  js: { options: { time: (new Date()).getTime() }, files: MYPREVIOUSDECLAREDFILES, tasks: ['jshint'] } }")

adds the following code to the gruntfile

watch: {
    js: {
        options: {
            time: (new Date()).time()
        },
        files: MYPREVIOUSDECLAREDFILES,
        tasks: ['jshint']
    }
}

or appends it.

Get the updated Gruntfile content

api.toString()

Get JSON object with all configured tasks

Invalid JSON Objects like variables or functions will added as String for information purpose

api.getJsonTasks()

Test Gruntfile for task config

Invalid JSON Objects like variables or functions will added as String for information purpose

api.hasConfig(identifier)

parameter.identifier

Type: string

The task identifier

loadNpmTask

Add loadNpmTasks call to Gruntfile. When load-grunt-tasks is active or the requested plugin is already loaded nothing will be added to the Gruntfile.

api.loadNpmTasks(pluginName)

parameter.pluginName

Type: string

The plugin name

Test Gruntfile for property inside task config

Invalid JSON Objects like variables or functions will added as String for information purpose

api.hasConfigProperty(identifier, property)

parameter.identifier

Type: string

The task identifier

parameter.property

Type: string|array

The property identifier

Examples

See examples/index.js

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

v0.0.1 - First very alpha!

v0.0.2 - Added some more functionality to the api

License

Copyright (c) 2014 Ben Zörb. Licensed under the MIT license.