1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 | module.exports = function(grunt) {
|
10 | var detectLineFeed = function(eol){
|
11 | switch(eol){
|
12 | case "cr":
|
13 | return '\r';
|
14 | case "crlf":
|
15 | return '\r\n';
|
16 | case "lf":
|
17 | return '\n';
|
18 | }
|
19 | }
|
20 | var lineEnding = function(filepath, linefeed){
|
21 | var file = grunt.file.read(filepath);
|
22 |
|
23 | file = file.replace(/\r\n|\n|\r/g, linefeed);
|
24 | return file;
|
25 | };
|
26 | grunt.registerMultiTask('lineending', 'convert line ending', function() {
|
27 | var options = this.options(this, {
|
28 | });
|
29 | grunt.verbose.writeflags(options, 'Options');
|
30 | this.files.forEach(function(f) {
|
31 | var src = f.src.filter(function(filepath) {
|
32 |
|
33 | if (!grunt.file.exists(filepath)) {
|
34 | grunt.log.warn('Source file "' + filepath + '" not found.');
|
35 | return false;
|
36 | } else {
|
37 | return true;
|
38 | }
|
39 | });
|
40 |
|
41 |
|
42 | var linefeed = '\n';
|
43 | if(options.eol){
|
44 | linefeed = detectLineFeed(options.eol)
|
45 | }
|
46 |
|
47 |
|
48 | var output = [];
|
49 | src.forEach(function(_src){
|
50 | try {
|
51 | output.push(lineEnding(_src, linefeed));
|
52 | } catch (e) {
|
53 | var err = new Error('Uglification failed.');
|
54 | err.origError = e;
|
55 | grunt.fail.warn(err);
|
56 | }
|
57 | })
|
58 |
|
59 |
|
60 | grunt.file.write(f.dest, output.join(linefeed));
|
61 |
|
62 |
|
63 | grunt.log.writeln('File "' + f.dest + '" created.');
|
64 | });
|
65 | });
|
66 | };
|