1 | /* |
---|
2 | * grunt-curl |
---|
3 | * https://github.com/twolfson/grunt-curl |
---|
4 | * |
---|
5 | * Copyright (c) 2013 Todd Wolfson |
---|
6 | * Licensed under the MIT license. |
---|
7 | */ |
---|
8 | |
---|
9 | var fs = require('fs'), |
---|
10 | path = require('path'), |
---|
11 | gruntRetro = require('grunt-retro'), |
---|
12 | request = require('request'); |
---|
13 | module.exports = function (grunt) { |
---|
14 | // Load in grunt-retro |
---|
15 | grunt = gruntRetro(grunt); |
---|
16 | |
---|
17 | // Please see the grunt documentation for more information regarding task and |
---|
18 | // helper creation: https://github.com/gruntjs/grunt/blob/master/docs/toc.md |
---|
19 | |
---|
20 | // ========================================================================== |
---|
21 | // TASKS |
---|
22 | // ========================================================================== |
---|
23 | |
---|
24 | grunt.registerMultiTask('curl', 'Download files from the internet via grunt.', function () { |
---|
25 | // Collect the filepaths we need |
---|
26 | var file = this.file, |
---|
27 | data = this.data, |
---|
28 | src = file.src, |
---|
29 | dest = file.dest, |
---|
30 | done = this.async(), |
---|
31 | that = this; |
---|
32 | |
---|
33 | // Upcast the srcFiles to an array |
---|
34 | var srcFiles = src; |
---|
35 | if (!Array.isArray(srcFiles)) { |
---|
36 | srcFiles = [src]; |
---|
37 | } |
---|
38 | |
---|
39 | // Asynchronously fetch the files in parallel |
---|
40 | var async = grunt.utils.async; |
---|
41 | async.map(srcFiles, grunt.helper.bind(grunt, 'curl'), curlResultFn); |
---|
42 | |
---|
43 | function curlResultFn(err, files) { |
---|
44 | // If there is an error, fail |
---|
45 | if (err) { |
---|
46 | grunt.fail.warn(err); |
---|
47 | } |
---|
48 | |
---|
49 | // Concatenate the srcFiles, process the blob through our helper, |
---|
50 | var separator = data.separator || '\n', |
---|
51 | content = files.join(separator); |
---|
52 | |
---|
53 | // Write out the content |
---|
54 | var destDir = path.dirname(dest); |
---|
55 | grunt.file.mkdir(destDir); |
---|
56 | fs.writeFileSync(dest, content, 'binary'); |
---|
57 | |
---|
58 | // Otherwise, print a success message. |
---|
59 | grunt.log.writeln('File "' + dest + '" created.'); |
---|
60 | |
---|
61 | // Callback |
---|
62 | done(); |
---|
63 | } |
---|
64 | }); |
---|
65 | |
---|
66 | var defaultRouter = path.basename; |
---|
67 | grunt.registerMultiTask('curl-dir', 'Download collections of files from the internet via grunt.', function () { |
---|
68 | // Collect the filepaths we need |
---|
69 | var file = this.file, |
---|
70 | src = file.src, |
---|
71 | dest = file.dest, |
---|
72 | data = this.data, |
---|
73 | router = data.router || defaultRouter, |
---|
74 | done = this.async(), |
---|
75 | that = this; |
---|
76 | |
---|
77 | // Upcast the srcFiles to an array |
---|
78 | var srcFiles = src; |
---|
79 | if (!Array.isArray(srcFiles)) { |
---|
80 | srcFiles = [src]; |
---|
81 | } |
---|
82 | |
---|
83 | // Iterate over the array and expand the braces |
---|
84 | var minimatch = grunt.file.glob.minimatch, |
---|
85 | braceExpand = minimatch.braceExpand; |
---|
86 | srcFiles = srcFiles.reduce(function expandSrcFiles (retArr, srcFile) { |
---|
87 | var srcFileArr = braceExpand(srcFile); |
---|
88 | retArr = retArr.concat(srcFileArr); |
---|
89 | return retArr; |
---|
90 | }, []); |
---|
91 | |
---|
92 | // Asynchronously fetch the files in parallel |
---|
93 | var async = grunt.utils.async; |
---|
94 | async.map(srcFiles, grunt.helper.bind(grunt, 'curl'), curlResultFn); |
---|
95 | |
---|
96 | function curlResultFn(err, files) { |
---|
97 | // If there is an error, fail |
---|
98 | if (err) { |
---|
99 | grunt.fail.warn(err); |
---|
100 | } |
---|
101 | |
---|
102 | // Determine the destinations |
---|
103 | var destArr = srcFiles.map(function getDest (srcFile) { |
---|
104 | // Route the file, append it to dest, and return |
---|
105 | var filepath = router(srcFile), |
---|
106 | retStr = path.join(dest, filepath); |
---|
107 | return retStr; |
---|
108 | }); |
---|
109 | |
---|
110 | // Iterate over each of the files |
---|
111 | files.forEach(function curlWriteFiles (content, i) { |
---|
112 | // Write out the content |
---|
113 | var destPath = destArr[i], |
---|
114 | destDir = path.dirname(destPath); |
---|
115 | grunt.file.mkdir(destDir); |
---|
116 | fs.writeFileSync(destPath, content, 'binary'); |
---|
117 | }); |
---|
118 | |
---|
119 | // Otherwise, print a success message. |
---|
120 | grunt.log.writeln('Files "' + destArr.join('", "') + '" created.'); |
---|
121 | |
---|
122 | // Callback |
---|
123 | done(); |
---|
124 | } |
---|
125 | }); |
---|
126 | |
---|
127 | // ========================================================================== |
---|
128 | // HELPERS |
---|
129 | // ========================================================================== |
---|
130 | |
---|
131 | // Register our curl helper |
---|
132 | grunt.registerHelper('curl', function (url, cb) { |
---|
133 | // Request the url |
---|
134 | request.get({'url': url, 'encoding': 'binary'}, function (err, res, body) { |
---|
135 | // If there was response, assert the statusCode was good |
---|
136 | if (res) { |
---|
137 | var statusCode = res.statusCode; |
---|
138 | if (statusCode < 200 || statusCode >= 300) { |
---|
139 | err = new Error('Fetching "' + url + '" failed with HTTP status code ' + statusCode); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | // Callback with the error and body |
---|
144 | cb(err, body); |
---|
145 | }); |
---|
146 | }); |
---|
147 | |
---|
148 | }; |
---|