Giter VIP home page Giter VIP logo

grunt-curl's Introduction

grunt-curl Build status

Download files from the internet via grunt.

This was created for dependency management via grunt-curl and grunt-zip as a low-tech alternative to bower and similar solutions.

http://twolfson.com/2014-01-19-low-tech-dependency-management-via-grunt-tasks

Getting Started

grunt-curl can be installed via npm: npm install grunt-curl

Then, add and configure it in your grunt file:

module.exports = function (grunt) {
  // Configure `curl` with URLs
  // If you would like to download multiple files
  // to the same directory, there is `curl-dir`
  grunt.initConfig({
    curl: {
      'location/to/download/github.html': 'http://github.com/',
    }
  });

  // Load in `grunt-curl`
  grunt.loadNpmTasks('grunt-curl');
};

Now, we can run our task:

$ grunt curl
Running "curl:location/to/download/github.html" (curl) task
File "location/to/download/github.html" created.

Done, without errors.

Documentation

grunt-curl creates 2 grunt tasks for you to use/configure, curl and curl-dir. curl is designed for downloading single files at a time. curl-dir is designed for downloading multiple files to a common directory.

Both tasks support accepting request parameters as a src file. Here is an example creating a POST request.

curl

We support 2 different formats for configuring curl.

Short format

The short format relies on grunt's support of {dest: src}

curl: {
  'location/to/download/file.js': 'http://files.com/path/to/file.js'
}

This format is suggested only if you don't need to run curl tasks separately

grunt curl

If you want to run this task standalone, it must be executed via:

grunt curl:dest
# grunt curl:location/to/download/file.js

Long format

curl: {
  'task-name': {
    src: 'http://files.com/path/to/file.js',
    dest: 'location/to/download/file.js'
  }
}

This can be run standalone via

grunt curl:task-name

Using request options

This is an example of the long format leveraging request parameters for making a POST request.

curl: {
  'task-name': {
    src: {
      url: 'http://files.com/path/to/file.js',
      method: 'POST',
      body: 'abc'
    },
    dest: 'location/to/download/file.js'
  }
}

curl-dir

curl-dir supports 2 configuration formats.

Short format

As with curl, we leverage grunt's {dest: src} format for our short format.

'curl-dir': {
  // These will be saved as:
  // 'location/to/save/files/file1.js' and
  // 'location/to/save/files/file2.js'
  'location/to/save/files': [
    'http://files.com/path/to/file1.js',
    'http://generic.com/scripts/file2.js'
  ]
}

As with before, this can be executed via grunt curl-dir but will execute other tasks at the same level. To run this task standalone, it must be run via:

grunt curl-dir:location/to/save/files

Long format

'curl-dir': {
  'task-name': {
    src: [
      'http://files.com/path/to/file1.js',
      'http://files.com/path/to/file2.js'
    ],
    dest: 'location/to/save/files'
  }
}

This task can be executed from the command line via

grunt curl-dir:task-name

Brace expansion

curl-dir supports brace expansion for src in both formats.

'curl-dir': {
  'brace-expansion': {
    src: ['http://files.com/path/to/{file1,file2}.js'],
    // Expands to: [
    //  'http://files.com/path/to/file1.js',
    //  'http://files.com/path/to/file2.js'
    // ]
    dest: 'location/to/save/files'
  }
}

Filepath mapping

URLs can be mapped to custom filepaths via the router option in the long format.

'curl-dir': {
  'custom-filepaths': {
    src: [
      'http://files.com/path/to/file1.js',
      'http://generic.com/scripts/file2.js'
    ],
    router: function (url) {
      // Save `file1.js` to 'location/to/save/files/hello/world/file1.js'
      // and `file2.js` to 'location/to/save/files/goodbye/moon/file2.js'
      var filepath = url.replace('http://files.com/path/to', 'hello/world');
      return url.replace('http://generic.com/scripts', 'goodbye/moon');
    },
    dest: 'location/to/save/files'
  }
}

Using request options

As demonstrated in curl, we can use request options to leverage special HTTP actions (e.g. make a POST request).

'curl-dir': {
  custom: {
    src: [{
      url: 'http://files.com/path/to/file.js',
      method: 'POST',
      body: 'abc'
    }],
    dest: 'location/to/save/files'
  }
}

Examples

Using a proxy

Using request options we can add a proxy to our requests

curl: {
  custom: {
    src: {
      url: 'http://google.com/',
      proxy: 'http://127.0.0.1:9001/'
    },
    dest: 'google.html'
  }
}

Using authentication

Using request options we can add authentication to our requests

curl: {
  custom: {
    src: {
      url: 'http://secureserver.com/members',
      auth: {
        user: 'my-username',
        pass: 'my-password'
      }
    },
    dest: 'secure.html'
  }
}

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 your code using grunt and test via npm test.

Donating

Support this project and others by twolfson via donations.

http://twolfson.com/support-me

Unlicense

As of Jun 14 2014, Todd Wolfson has released this repository and its contents to the public domain.

It has been released under the UNLICENSE.

Prior to Jun 14 2014, this repository and its contents were licensed under the MIT license.

grunt-curl's People

Contributors

duereg avatar lynchmaniac avatar maxcutler avatar shinnn avatar twolfson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grunt-curl's Issues

Tests assume localhost

I just forked this repo and tried to run the Gruntfile in the test folder. I got this:

Running "clean:test" (clean) task
Cleaning actual/...OK

Running "curl:js" (curl) task
File "actual/file.js" created.

Running "curl:zip" (curl) task
File "actual/file.zip" created.

Running "curl:post" (curl) task
Warning: connect ECONNREFUSED Use --force to continue.

Aborted due to warnings.

Looking at 'curl-dir':post in the grunt.js file I saw that you assume a file at http://localhost:4000/post.txt Why? I think all test tasks should pass after I just forked it.

curl:target doesn't work ?

Hi,

I have an issue while trying to do something like :

curl: {
    dev: {
      'dest1': 'src1',
      'dest2': 'src2'
    },
    prod: {
      'dest3': 'src3',
      'dest4': 'src4'
    }
}

and test them with grunt:dev, grunt:prod...
It doesn't seems to work properly.

Problem with multiply files

'server_localhost': {
src: ['http://localhost/en_US.json', 'http://localhost/es_US.json'],
dest: 'builds/json'
}
I try to get several sources and copy in 1 directory but did not make it to work.
If it is just 1 source its working.
Any help with this issue?

custom request options in curl-dir cause parse error

Hi!
I've just run into a problem and I'm pretty sure it's a bug.

I'm trying to use curl-dir, but need to send a cookie as well. Part of my gruntfile:

        'curl-dir': {
            validation: {
                src: [
                    {
                        url: backendUrl + "/patches/meta",
                        cookie: apiToken
                    },
                    {
                        url: backendUrl + "/devices/meta",
                        cookie: apiToken
                    }
                ],
                dest: 'lib/validation-rules'
            }
        }

Execution of the task stops with an error (verbose output):

Running "curl-dir:validation" (curl-dir) task
Verifying property curl-dir.validation exists in config...OK
Warning: Object #<Object> has no method 'indexOf' Use --force to continue.

If only strings are given in the src array it works fine, but according to the docs this should work as well.

Multiple files to folder fails

On Windows I try to do the following:

curl: {
        'ionic-nightly-css': {
            src: 'http://code.ionicframework.com/nightly/css/ionic.min.css',
            dest: 'app/vendor/ionic/css/ionic.min.css'
        },
        'ionic-nightly-js': {
            src: 'http://code.ionicframework.com/nightly/js/ionic.bundle.min.js',
            dest: 'app/vendor/ionic/js/ionic.bundle.min.js'
        },
        'ionic-nightly-fonts': {
            src: [
                'http://code.ionicframework.com/nightly/fonts/ionicons.eot',
                'http://code.ionicframework.com/nightly/fonts/ionicons.ttf',
                'http://code.ionicframework.com/nightly/fonts/ionicons.svg',
                'http://code.ionicframework.com/nightly/fonts/ionicons.woff'
            ],
            dest: 'app/vendor/ionic/fonts'
        }
    },

First two curl's work, last one (multiple src files) gives error: Fatal error: EISDIR, illegal operation on a directory 'C:\blablabla\app\vendor\ionic\fonts'�

Valid filepath on Windows filesystem

Hi

I have a var SERVER_URL = 'http://server.com',
LANGUAGE_DATA_FILE = "json/language_data.json";

and I use

curl: {
    'get-lang': {
        src: SERVER_URL+'/get/languagedata2.json',
        dest: LANGUAGE_DATA_FILE
    },...

but no file is created on windows OS
I mention I run the command in the folder where json is immediatelly beneath the current location so json path exist

Any ideea ?

Add configuration option for customizing error condition

Currently, this project uses a hard-coded failure condition based on the response' HTTP STATUS code as follows:

if (statusCode < 200 || statusCode >= 300) {
  return cb(new Error('Fetching ' + JSON.stringify(options) + ' failed with HTTP status code ' + statusCode));
}

This causes the grunt task runner to stop executing tasks post-error if not run with the -f or --force flag, and prevents the current executing method from completing its implementation. This is problematic when doing something like the following:

curl:
  geodatasource_cities_auth:
    src:
      url: 'http://www.geodatasource.com/login'
      method: 'POST'
      formData:
        password: 'password'
        emailAddress: '[email protected]'
      jar: true
    dest: '/dev/null'
  geodatasource_cities_free:
    src:
      url: 'http://www.geodatasource.com/download?id=8'
      jar: true
    dest: 'seed/geodatasource_cities_free.zip'

Where the geodatasource_cities_auth task returns an HTTP STATUS 302; a success condition in this scenario.

Please add some kind of configuration option to ignore warnings/errors or specify permissible status codes outside of the 200 block.

Proxy

Is it possible to have grunt-curl use a proxy server?

If not, where can I start for a quick fix / hack to make it work?

Handling of error: DEPTH_ZERO_SELF_SIGNED_CERT

If you have a self-signed certificate (for dev or testing purposes), you might get the following error:

Warning: DEPTH_ZERO_SELF_SIGNED_CERT Use --force to continue.

This is fixed by adding the following to your Gruntfile.

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;

It would be nice to either put this in your README file, or better-yet, add an option to the grunt-curl plugin to enable this functionality.

I can provide a pull-request for either, if you'd like. Thanks.

Error while running grunt-curl

hi , I am trying to set up this grunt-curl on my project. I am getting this erro while running this on my Web (.Net) application. could you please help me out.
Running "curl:http://localhost:61115/dev/" (curl) task
Verifying property curl.http exists in config...ERROR

Unable to process task.
Warning: Required config property "curl.http" missing. Use --force to continue.

this is my code:
'use strict';
module.exports = function (grunt) {

// Configure `curl` with URLs
// If you would like to download multiple files
// to the same directory, there is `curl-dir`
grunt.initConfig({
    curl : {
        'http://localhost:61115/dev/': 'http://aa.com/artifactory/api/npm/npm-local/'
    }
});

// Load in `grunt-curl`
grunt.loadNpmTasks('grunt-curl');

};

I am very new to grunt , so you can expect a preety minor issue too. 👍

Add support for authentication

Adding support for the -u CLI parameter, especially with :x-oauth-basic would enable (among, I'm sure, other things) downloading from private GIT repos, and negate the need for bower for simple dependencies

How to download files from auto generated URLs?

I need to download some files from site. Links to files I create automaticly in Gruntfile.js. My links looks like:
http://site.com/images/D83CDF81.png

Where D83CDF81 generated after some calculation in downloadEmoji method. My Gruntfile.js:

module.exports = function (grunt) {
    var link = '';
    /* Link to file. Example of link:
     * http://site.com/images/D83CDF81.png
     */

    var name = ''; // Required name of file
    /* Name of file. Example:
     * D83CDF81.png
     */

    var downloadEmoji = function () {
        /* Generating links anf file name here
         * and set this values to 'link' and 'name' vars
         */

        /* I have multiple files (over 500)
         * I use forEach loop for generate links to all files
         * and run grunt task here.
         * How to pass my 'link' and 'name' vars to grunt-curl?
         */
        grunt.task.run (
            'curl'
        );
    };

    grunt.initConfig (
        pkg: grunt.file.readJSON ('package.json'),

        curl: {
            'emoji/<%= code %>.png' : '<%= link %>'
            // ???
            // 'images/D83CDF81.png' : 'http://site.com/images/D83CDF81.png'
        }
    );
    grunt.loadNpmTasks ('grunt-curl');
    grunt.registerTask ('downloademoji', 'Download emoji', downloadEmoji);
}

Add authentication to the request

Is there a way to add authentication in the request ?

        test: {
            src: {
                url: 'http://localhost/nexus/service/local/artifact/maven/resolve?r=repo&g=fr.test&a=AAA&v=1.0.1-SNAPSHOT&e=apk',
                auth: {
                    user: 'user',
                    pass: 'mdp'
                }
            },
            dest: 'toto.apk'
        }

doesn't seems to work well.
This example is based on the request module.

If there is a way i think it's pretty cool to show an example in the readme.

grunt-curl 2.2.0
node 6.9.1
npm 3.10.8

Add formatter option

Sometimes we receive data in a format than when we want to save it to disk. It can be usually it piped into another grunt task but not always. We should add a new option for this:

curl: {
  src: {...},
  dest: ...,
  formatter: function (res, body, cb) {
    cb(JSON.stringify(body));
  }
}

This issue was created as a side effect of #10

Grunt 0.4 Release

I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.

If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.

If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8" as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0" to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.

Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.

Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!

If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.

Thanks, we really appreciate your work!

Can we use dynamic paths?

Can we use dynamic paths?

I have a file but the version number is appended to each file name. So:

file-1.2.3.zip

Can we achieve this with this repository?

Short format not working as expected?

Hello,

I have the following in Gruntfile.js:

module.exports = function(grunt) {
    grunt.initConfig({
        curl: {
            font_awesome: {
                'font-awesome-4.1.0.css': 'http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css'
            }
        }
    });

    grunt.loadNpmTasks('grunt-curl');
};

Trying to run it, I get:

$ grunt curl:font_awesome
Running "curl:font_awesome" (curl) task
Warning: Cannot read property 'src' of undefined Use --force to continue.

Aborted due to warnings.
$

If I comment out the task and place the files directly under curl: {, things work, but I'd like it to run as a specific task.

Am I doing something wrong, or is there a bug lurking here? :)

Create curl-dir task for easier fetching of multiple files

As mentioned in #2, we should create a new task called curl-dir. This should use the same curl helper as the curl task and provide an interface like:

grunt.initConfig({
  'grunt-dir': {
    batch: {
      // Files to download
      src: ['http://host.com/file1.js', 'http://host.com/file2.js'],

      // Directory to download file1.js and file2.js to
      dest: 'scripts'
    },
    braceExpansion: {
      // Expands to file1.js and file2.js
      src: ['http://host.com/{file1,file2}.js'],

      // Downloads file1.js and file2.js to scripts folder
      dest: 'scripts'
    },
  }
});

For determining the name of the file within the directory, shoot for the high level. By default, use the path.basename. With the option to manually specify a function to handle the logic for you:

grunt.initConfig({
  'grunt-dir': {
    batch: {
      // Files to download
      src: ['http://host1.com/nested/file1.js', 'http://host2.com/other_dir/file2.js'],

      // Route the files to their proper directories
      router: funciton (url) {
        // Ignore host and write to directory + filename
        return url.replace('http://host1.com/', '').replace('http://host2.com/', '');
      },

      // Download and write files as scripts/nested/file1.js and scripts/other_dir/file2.js
      dest: 'scripts'
    }
});

Add global options

I'm getting "Warning: UNABLE_TO_VERIFY_LEAF_SIGNATURE" because of the https certificate is not signed.
Solution: option: rejectUnauthorized: false

I'm using Short format (dest: src) & have at least 30 files defined. It's not appropriate to use Long format for each file, it's very disturbing. I've tried with "curl-dir" but without luck. Please add global & subtask options for this kind of problems.

Thank You

Can't get grunt-curl to save utf8

Hi,

No matter what options I put in, grunt-curl seems to be saving buffers instead of utf8-formatted files.

Here's my config:

curl:
    'assets/css/icomoon.css':        'https://s3.amazonaws.com/icomoon.io/27973/ActiveBuilding/style.css'
    'assets/css/fonts/icomoon.eot':  'https://s3.amazonaws.com/icomoon.io/27973/ActiveBuilding/icomoon.eot'
    'assets/css/fonts/icomoon.woff': 'https://s3.amazonaws.com/icomoon.io/27973/ActiveBuilding/icomoon.woff'
    'assets/css/fonts/icomoon.ttf':  'https://s3.amazonaws.com/icomoon.io/27973/ActiveBuilding/icomoon.ttf'
    'assets/css/fonts/icomoon.svg':  'https://s3.amazonaws.com/icomoon.io/27973/ActiveBuilding/icomoon.svg'

The outputted files all look like:

1f8b 0800 0000 0000 0203 ec7c 7b90 1bc7
79e7 74f7 bc5f c000 9819 6017 8b5d 00bb
001f e212 8ba7 b424 97cb e53b 91cc 25a9
074d 4a96 6949 7cd9 1465 4ba2 698b 9268
c9b4 655a b41e 96cf b12e 15c9 a944 be3c
ac53 524a 2c4a aa32 6d25 578a 4c2a 4a5c

I've tried putting encoding: 'utf8' in both the options parameter and in the 'src' of these things. Am I doing something wrong?

Node v0.10.25, Grunt v0.4.4

Thanks!

grunt-curl encoding css

When trying to download a css file, it is encoded or something - the output looks something like:

1f8b 0800 6eb5 af54 02ff e59a 5963 1bb7
11c7 dff5 2918 ba87 e57a 7551 07a5 d48d
7b24 6dd3 2b4d 7cb4 cdd1 824b 90dc 6a77
b106 b094 e8d4 fdec 05b0 cbc3 fccf 8cfa
503f f541 0f14 e747 0083 b930 c0f3 7ca1
acd3 7e30 7cf9 e2b3 6c3c fcf8 e0e0 f8c9
a075 453d 1f3c 393e 783e 33b5 cf66 2ad7
83ef 0f06 83fe 5355 94ab 9bc1 3049 0562...

is there a parameter i need to pass or something, thanks for the awesome plugin

Support grunt v0.4.x

I tried to use grunt-curl while running grunt v0.4.5, but I get the error:
Loading "curl.js" tasks...ERROR
>> TypeError: Object #<Object> has no method 'registerHelper'

grunt-curl currently makes uses of registerHelper, which has been removed in the 0.4.x versions of grunt. This seems to indicate that grunt-curl is written for grunt v0.3.x. Users of grunt v0.4.x such as myself would find grunt-curl useful.

Progress feedback in output

I assume there is no way you can output some feedback as to how much has been downloaded so far. I am handling ~100MB files and it's hard to stare at a blank screen for that long :)

Something like:

[======                                  ] 30% done

would be awesome.

Shouldn't have to specify destination file name

I would expect the following to work:

curl: {
  jquery: {
    src: 'http://code.jquery.com/jquery-1.10.1.js',
    dest: 'vendor/js/'
  },
  underscore: {
    src: 'http://underscorejs.org/underscore.js',
    dest: 'vendor/js/'
  }
}

I would expect grunt curl:jquery to result in vendor/js/jquery-1.10.1.js. According to the documentation, this should already be possible, but I presume it only works when src has more than one file. I even tried changing src to a single-element array to no avail.

Though, while we're on the subject, since this is a multiTask, shouldn't the destination directory be configurable at the top level?

curl: {
  options: { dest: 'vendor/js/' },

  jquery: { src: 'http://code.jquery.com/jquery-1.10.1.js' },
  underscore: { src: 'http://underscorejs.org/underscore.js'  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.