Skip to content

aliaksandr-master/grunt-process

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm npm Dependency Status devDependency Status Build Status Coverage Status Code Climate Gitter

grunt-process

Gruntplugin for processing (add, replace, split) any file

Getting Started

This plugin requires Grunt ~0.4.x

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-process --save

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

grunt.loadNpmTasks('grunt-process');

The "process" task

Overview

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

grunt.initConfig({
  process: {
    options: {
      // common options go here.
    },
    your_target: {
      options: {
        // Target-specific options go here.
      },
      files: [
        // Target-specific file lists and/or options go here.
      ]
    },
  },
});

Options

options.read

Type: Function

method for open file and convert to string or any format what you need

grunt.initConfig({
    customRead: {
        options: {
            read: function (src, dest, fileObject) {
                return grunt.file.readJSON(src); // default function for read

                // if want use async read function:
                // var done = this.async();
                // process.nextTick(function () {
                //     var content = grunt.file.readJSON(src, readOptions);
                //     done(null, content);
                // });
            }
        },
        files: [{
            src: 'some/path/to.file',
           dest: 'distr/path/to.file'
        }]
    }
});

options.process

Type: Function

method for process file and convert to any format what you need (for save)

grunt.initConfig({
    customProcess: {
        options: {
            read: function (src, dest, content, fileObject) {
                return content;  // this is default function for process

                // if want use async process function:
                // var done = this.async();
                // process.nextTick(function () {
                //     content = content.replace(/[1-9]/g, ''); //remove all numbers for example
                //     done(null, content);
                // });
            }
        },
        files: [{
            src: 'some/path/to.file',
           dest: 'distr/path/to.file'
        }]
    }
});

options.save

Type: Function

method for save file(s). must return object (key - is file path; value - content)

grunt.initConfig({
    customSave: {
        options: {
            save: function (src, dest, content, fileObject) {
                var fileObject = {};
                fileObject[dest] = content;
                return fileObject;

                // if want use async save function:
                // var done = this.async();
                // process.nextTick(function () {
                //     var obj = {};
                //     obj[dest + '/1.txt'] = '1';
                //     obj[dest + '/2.txt'] = '2';
                //     obj[dest + '/3.txt'] = '3';
                //     done(null, obj);
                // });
            }
        },
        files: [{
            src: 'some/path/to.file',
           dest: 'distr/path/to.file'
        }]
    }
});

Usage

grunt.initConfig({
    compressAndSplitJsonByKey: {
        options: {
            // read file and convert to JSON
            read: function (src, dest, fileObject) {
                return grunt.file.readJSON(src);
            },

            // add someKey to content object
            process: function (src, dest, content, fileObject) {
                content.someKey = 123;

                return content;
            },

            // split json by object key
            save: function (src, dest, content, fileObject) {
              var files = {};

              _.each(content, function (v, k) {
                var file = fileObject.orig.dest + '/' + k + '.json';
                var content = JSON.stringify(v);
                files[file] = content;
              });

              return files;
            }
        },
        files: [
          {
            expand: true,
            cwd: 'src/dir',
            dest: 'dest/dir',
            src: ['**/*.json']
          }
        ]
    },
});

more exampels in Gruntfile.js

Extend this task

var gruntProcess = require('grunt-process/lib');

gruntProcess(grunt, filesArray, options, done);

example

'use strict';

var gruntProcess = require('grunt-process/lib');

module.exports = function (grunt) {
	grunt.registerMultiTask('my-json-processTask', function () {
	    var options = this.options({});

		task(grunt, this.files, {
		    read: function (src, dest, fileObject) {
		        return grunt.file.readJSON(src, options.readOptions);
		    },

		}, this.async());
	});
};

About

gruntplugin for processing (add, replace, split) any file

Resources

License

Stars

Watchers

Forks

Packages

No packages published