transcoding.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * grunt-transcoding
  3. * \
  4. *
  5. * Copyright (c) 2013 hancong03
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. module.exports = function(grunt) {
  10. // Please see the Grunt documentation for more information regarding task
  11. // creation: http://gruntjs.com/creating-tasks
  12. grunt.registerMultiTask('transcoding', 'File encoding conversion tool', function() {
  13. // Merge task-specific and/or target-specific options with these defaults.
  14. var options = this.options({
  15. punctuation: '.',
  16. separator: ', '
  17. });
  18. // Iterate over all specified file groups.
  19. this.files.forEach(function(f) {
  20. // Concat specified files.
  21. var src = f.src.filter(function(filepath) {
  22. // Warn on and remove invalid source files (if nonull was set).
  23. if (!grunt.file.exists(filepath)) {
  24. grunt.log.warn('Source file "' + filepath + '" not found.');
  25. return false;
  26. } else {
  27. return true;
  28. }
  29. }).map(function(filepath) {
  30. // Read file source.
  31. return grunt.file.read(filepath);
  32. }),
  33. fileCount = src.length,
  34. iconvLite = require( "iconv-lite" );
  35. src.forEach( function ( content, index ) {
  36. grunt.file.write( f.src[index], iconvLite.encode( content, options.charset ), {
  37. encoding: options.charset
  38. } );
  39. } );
  40. // Print a success message.
  41. grunt.log.writeln( grunt.log.wordlist( [fileCount] ) +' files transcoded.' );
  42. });
  43. });
  44. };