Last active
September 7, 2016 16:22
-
-
Save rvause/dcc9deba98eda2e2df8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var http = require('http'); | |
| var https = require('https'); | |
| var zlib = require('zlib'); | |
| module.exports = function (grunt) { | |
| grunt.registerMultiTask('httpfetch', 'Fetch file over HTTP', function () { | |
| var _this = this; | |
| this.files.forEach(function (file) { | |
| var req, done; | |
| done = _this.async() | |
| if (file.src_url.indexOf('https') === 0) { | |
| req = https; | |
| } | |
| else { | |
| req = http; | |
| } | |
| req.get(file.src_url, function (response) { | |
| var content = ''; | |
| var writeChunk = function (chunk) { | |
| content += chunk; | |
| }; | |
| var writeFile = function () { | |
| grunt.file.write(file.dest, content); | |
| grunt.log.writeln('Fetched: ' + file.src_url); | |
| done(true); | |
| }; | |
| if (response.headers['content-encoding'] == 'gzip') { | |
| var gunzip = zlib.createGunzip();; | |
| response.pipe(gunzip); | |
| gunzip.on('data', writeChunk); | |
| gunzip.on('end', writeFile); | |
| } | |
| else { | |
| response.on('data', writeChunk); | |
| response.on('end', writeFile); | |
| } | |
| }).on('error', function (error) { | |
| grunt.log.error('Could not get source: ' + file.src_url); | |
| done(false); | |
| }); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment