Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Grunt programmatically from JavaScript #1108

Closed
wants to merge 1 commit into from

Conversation

auscaster
Copy link
Contributor

This fix allows Grunt to be used programmatically from JavaScript without the need for a Gruntfile, like so:

var grunt = require('grunt');

grunt.initConfig({
    pkg: {
        "name" : "grunt-prog-test",
        "version" : "0.0.1",
        "devDependencies": {    
            "grunt": "~0.4.2",
            "grunt-contrib-concat": "~0.1.3",
            "grunt-contrib-cssmin" : "~0.6.1"
        }
    },
    concat: {
        css: {
            src: [
                'src/*.css'
            ],
            dest: 'dist/test.css'
        }
    },
    cssmin: {
        css: {
            src: 'dist/test.css',
            dest: 'dist/test.min.css'
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-cssmin');

grunt.tasks([ 'concat:css', 'cssmin:css' ], { verbose: true, nogruntfile: true }, function () { 
  console.log('done')
});

…ion so tasks can be run programmatically from JavaScript
@auscaster auscaster changed the title Scripting Grunt-Init #64 via grunt-init: Added nogruntfile option so tas... Use Grunt programmatically from JavaScript Mar 31, 2014
@logankoester
Copy link

👍 Yes, please!

@auscaster
Copy link
Contributor Author

Just to give a little more info: this PR adds a 'nogruntfile' options which bypasses the Gruntfile presence check and resulting exception in task.js task.init(). Other than that everything works as expected when manually calling grunt.tasks().

PS. Sorry for the confusion regarding the gruntjs/grunt-init#64, this PR is a standalone issue...

@stevenvachon
Copy link

+1

@vladikoff vladikoff added this to the 0.4.6 milestone Apr 8, 2014
@xcambar
Copy link

xcambar commented Jun 2, 2014

Great, I'd love to see this merged!

@cowboy
Copy link
Member

cowboy commented Jun 18, 2014

Will this do what you want it to?
https://github.com/gruntjs/grunt/compare/pr1108?expand=1

This is the code I used to test (from within the Grunt project root directory):

var grunt = require('./lib/grunt');

grunt.initConfig({
  pkg: grunt.file.readJSON('./package.json'),
  bar: {
    options: {a: 1, b: 2},
    one: {},
    two: {
      options: {b: 3, c: 4},
    },
  },
});

grunt.registerTask('foo', function() {
  console.log(grunt.config('pkg.name'), grunt.config('pkg.version'));
});

grunt.registerMultiTask('bar', function() {
  console.log(this.options());
});

grunt.tasks(['foo', 'bar'], {gruntfile: false}, function() {
  console.log('done');
});

/cc @vladikoff @shama @tkellen because I'm trying to figure out if I haven't accounted for something critical, that will cause backward-compatibility issues.

@cowboy
Copy link
Member

cowboy commented Jun 18, 2014

FWIW, there may be other things that prevent Grunt from playing nicely when run as a lib, for example, the way it handles process.exit() and uncaught exceptions. If these are issues, perhaps they can be dealt with separately.

@vladikoff vladikoff modified the milestones: 0.4.7, 0.4.6 Jun 18, 2014
@vladikoff
Copy link
Member

ping @auscaster

@auscaster
Copy link
Contributor Author

Thanks @cowboy, your patch works fine for me using your test code and my own.

Can't see any obvious issues using the grunt core this way, but as you say some dependencies and other grunt modules may be problematic (haven't found any probs yet though) :)

mattpodwysocki added a commit to Reactive-Extensions/RxJS-CLI that referenced this pull request Jun 26, 2014
@cowboy cowboy modified the milestones: 0.4.6, 0.4.7 Jul 10, 2014
@cowboy cowboy closed this in 77155e3 Jul 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants