123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /*
- * 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;
- };
|