1 | # grunt-zip [](https://www.gittip.com/twolfson/) |
---|
2 | |
---|
3 | Zip and unzip files via a grunt plugin |
---|
4 | |
---|
5 | ## Getting Started |
---|
6 | Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: `npm install grunt-zip` |
---|
7 | |
---|
8 | Then add this line to your project's `grunt.js` gruntfile: |
---|
9 | |
---|
10 | ```javascript |
---|
11 | grunt.loadNpmTasks('grunt-zip'); |
---|
12 | ``` |
---|
13 | |
---|
14 | [grunt]: http://gruntjs.com/ |
---|
15 | [getting_started]: https://github.com/gruntjs/grunt/blob/master/docs/getting_started.md |
---|
16 | |
---|
17 | ## Documentation |
---|
18 | `grunt-zip` introduces two grunt tasks: zip and unzip. |
---|
19 | |
---|
20 | ### zip |
---|
21 | ```js |
---|
22 | grunt.initConfig({ |
---|
23 | zip: { |
---|
24 | // We accept short syntax |
---|
25 | // 'destinationZip': ['firstFileToZip', 'secondFileToZip', ...] |
---|
26 | 'acme.zip': ['index.html', 'rocket.js'], |
---|
27 | |
---|
28 | // As well as standard grunt sytax |
---|
29 | widgets: { |
---|
30 | // Files to zip together |
---|
31 | src: ['corkscrew.js', 'sillyStraw.js'], |
---|
32 | |
---|
33 | // Destination of zip file |
---|
34 | dest: 'widgets.zip' |
---|
35 | }, |
---|
36 | |
---|
37 | // Specify working directory to zip from via `cwd` |
---|
38 | 'more-widgets': { |
---|
39 | cwd: 'nested/' |
---|
40 | // Files will zip to 'corkscrew.js' and 'sillyStraw.js' |
---|
41 | src: ['nested/corkscrew.js', 'nested/sillyStraw.js'], |
---|
42 | dest: 'moreWidgets.zip' |
---|
43 | }, |
---|
44 | |
---|
45 | // Adjust file paths via `router` for complex cases |
---|
46 | site: { |
---|
47 | // `router` receives the path from grunt (e.g. js/main.js) |
---|
48 | // The path it returns is what the file contents are saved as (e.g. all/main.js) |
---|
49 | // This CANNOT be used with `cwd` since there are potential ordering issues. |
---|
50 | router: function (filepath) { |
---|
51 | // Route each file to all/{{filename}} |
---|
52 | var filename = path.basename(filepath); |
---|
53 | return 'all/' + filename; |
---|
54 | } |
---|
55 | |
---|
56 | // Files will zip to 'main.js' and 'main.css' |
---|
57 | src: ['js/main.js', 'css/main.css'], |
---|
58 | dest: 'site.zip' |
---|
59 | }, |
---|
60 | |
---|
61 | // If you want to use the 'DEFLATE' compression algorithm, encode data in base64, or include dotfiles, you must opt-in to it |
---|
62 | 'even-more-widgets': { |
---|
63 | src: ['corkscrew.js', 'sillyStraw.js'], |
---|
64 | dest: 'evenMoreWidgets.zip', |
---|
65 | |
---|
66 | // Setting for DEFLATE compression |
---|
67 | compression: 'DEFLATE', |
---|
68 | |
---|
69 | // Setting for base64 encoding |
---|
70 | base64: true, |
---|
71 | |
---|
72 | // Setting to include dotfiles (e.g. .travis.yml) |
---|
73 | dot: true |
---|
74 | }, |
---|
75 | |
---|
76 | // Skip/exclude files via `router` |
---|
77 | zipJsOnly: { |
---|
78 | // If router returns a falsy varaible, the file will be skipped |
---|
79 | router: function (filepath) { |
---|
80 | // Grab the extension |
---|
81 | var extname = path.extname(filepath); |
---|
82 | |
---|
83 | // If the file is a .js, add it to the zip |
---|
84 | if (extname === '.js') { |
---|
85 | return filepath; |
---|
86 | } else { |
---|
87 | // Otherwise, skip it |
---|
88 | return null; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | src: ['js/main.js', 'css/main.css'], |
---|
93 | dest: 'jsOnly.zip' |
---|
94 | } |
---|
95 | } |
---|
96 | }); |
---|
97 | ``` |
---|
98 | |
---|
99 | ### unzip |
---|
100 | ```js |
---|
101 | grunt.initConfig({ |
---|
102 | 'unzip': { |
---|
103 | // Short syntax |
---|
104 | // 'folderToExtractFilesTo': 'zipFileToExtract' |
---|
105 | gallery: 'photos.zip', |
---|
106 | |
---|
107 | // Long syntax |
---|
108 | catalog: { |
---|
109 | src: 'electronics.zip', |
---|
110 | dest: 'catalog' |
---|
111 | } |
---|
112 | |
---|
113 | // Note: If you provide multiple src files, they will all be extracted to the same folder. |
---|
114 | // This is not well-tested behavior so use at your own risk. |
---|
115 | |
---|
116 | // Adjust file paths of zipped files via `router` |
---|
117 | site: { |
---|
118 | // `router` receives the path that was used during zipping (e.g. css/bootstrap.css) |
---|
119 | // The path it returns is where the file contents will be written to (e.g. dist/bootstrap.css) |
---|
120 | router: function (filepath) { |
---|
121 | // Route each file to dist/{{filename}} |
---|
122 | var filename = path.basename(filepath); |
---|
123 | return 'dist/' + filename; |
---|
124 | } |
---|
125 | |
---|
126 | // Collects all nested files in same directory |
---|
127 | // css/bootstrap.css -> bootstrap.css, js/bootstrap.js -> bootstrap.js |
---|
128 | src: 'bootstrap.zip', |
---|
129 | dest: 'bootstrap/' |
---|
130 | }, |
---|
131 | |
---|
132 | // If you want to disable the CRC32 check or decode data from base64, you must opt-in to it |
---|
133 | 'unzip-more': { |
---|
134 | src: 'bootstrap.zip', |
---|
135 | dest: 'public', |
---|
136 | |
---|
137 | // Setting for disabling the CRC32 check |
---|
138 | checkCRC32: false, |
---|
139 | |
---|
140 | // Setting for decoding from base64 |
---|
141 | base64: true |
---|
142 | }, |
---|
143 | |
---|
144 | // Skip/exclude files via `router` |
---|
145 | unzipCssOnly: { |
---|
146 | // If router returns a falsy varaible, the file will be skipped |
---|
147 | router: function (filepath) { |
---|
148 | // Grab the extension |
---|
149 | var extname = path.extname(filepath); |
---|
150 | |
---|
151 | // If the file is a .css, extract it |
---|
152 | if (extname === '.css') { |
---|
153 | return filepath; |
---|
154 | } else { |
---|
155 | // Otherwise, skip it |
---|
156 | return null; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | src: ['bootstrap.css'], |
---|
161 | dest: 'bootstrap-css/' |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | } |
---|
166 | }); |
---|
167 | ``` |
---|
168 | |
---|
169 | ## Contributing |
---|
170 | 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][grunt] and test via `npm test`. |
---|
171 | |
---|
172 | ## License |
---|
173 | Copyright (c) 2013 Todd Wolfson |
---|
174 | Licensed under the MIT license. |
---|