contrib.js 2.9 KB

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