contrib.js
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* grunt-lib-contrib
* http://gruntjs.com/
*
* Copyright (c) 2012 Tyler Kellen, contributors
* Licensed under the MIT license.
*/
exports.init = function(grunt) {
'use strict';
var exports = {};
var path = require('path');
exports.getNamespaceDeclaration = function(ns) {
var output = [];
var curPath = 'this';
if (ns !== 'this') {
var nsParts = ns.split('.');
nsParts.forEach(function(curPart, index) {
if (curPart !== 'this') {
curPath += '[' + JSON.stringify(curPart) + ']';
output.push(curPath + ' = ' + curPath + ' || {};');
}
});
}
return {
namespace: curPath,
declaration: output.join('\n')
};
};
// Convert an object to an array of CLI arguments
exports.optsToArgs = function(options) {
var args = [];
Object.keys(options).forEach(function(flag) {
var val = options[flag];
flag = flag.replace(/[A-Z]/g, function(match) {
return '-' + match.toLowerCase();
});
if (val === true) {
args.push('--' + flag);
}
if (grunt.util._.isString(val)) {
args.push('--' + flag, val);
}
if (grunt.util._.isNumber(val)) {
args.push('--' + flag, '' + val);
}
if (grunt.util._.isArray(val)) {
val.forEach(function(arrVal) {
args.push('--' + flag, arrVal);
});
}
});
return args;
};
// Strip a path from a path. normalize both paths for best results.
exports.stripPath = function(pth, strip) {
if (strip && strip.length >= 1) {
strip = path.normalize(strip);
pth = path.normalize(pth);
pth = grunt.util._(pth).strRight(strip);
pth = grunt.util._(pth).ltrim(path.sep);
}
return pth;
};
// Log min and max info
function gzipSize(src) {
return src ? require('zlib-browserify').gzipSync(src).length : 0;
}
exports.minMaxInfo = function(min, max, report) {
if (report === 'min' || report === 'gzip') {
grunt.log.writeln('Original: ' + String(max.length).green + ' bytes.');
grunt.log.writeln('Minified: ' + String(min.length).green + ' bytes.');
}
if (report === 'gzip') {
// Note this option is pretty slow so it is not enabled by default
grunt.log.write('Gzipped: ');
grunt.log.writeln(String(gzipSize(min)).green + ' bytes.');
}
};
exports.formatForType = function(string, type, namespace, filename) {
namespace = namespace || false;
if (type === 'amd' && namespace === false) {
string = 'return ' + string;
} else if (type === 'commonjs' && namespace === false) {
string = 'module.exports = ' + string;
} else if (type === 'amd' && namespace !== false || type === 'commonjs' && namespace !== false || type === 'js' && namespace !== false) {
string = namespace+'['+JSON.stringify(filename)+'] = '+string+';';
}
return string;
};
return exports;
};