Commits

Vyatcheslav Zaytcev  committed 2c0fb3c

Added compress functionality

  • Participants
  • Parent commits 7247db2

Comments (0)

Files changed (2)

File package.json

 {
   "name": "grunt-samsung-widgetlist",
   "description": "Grunt task for build Samsung SmartTV packages.",
-  "version": "0.1.0",
+  "version": "0.2.0",
   "homepage": "https://bitbucket.org/interfaced/grunt-samsung-widgetlist/overview",
   "author": {
-    "name": "Vyatcheslav Zaytcev",
-    "email": "vyatcheslav.lacki@gmail.com",
+    "name": "Interfaced",
+    "email": "info@interfaced.ru",
     "url": "http://interfaced.ru/"
   },
   "repository": {
   "keywords": [
     "gruntplugin"
   ]
-}
+}

File tasks/samsung-widgetlist.js

 	var path = require('path'),
 		fs = require('fs'),
 		builder = require('xmlbuilder'),
+		archiver = require('archiver'),
 		fileInfoExp = /^([a-z0-9]+)_([0-9\.]+)_([0-9a-z]+)_([0-9]{8})$/i;
 
-	// ==========================================================================
-	// TASKS
-	// ==========================================================================
+	// TODO: remove this when grunt v0.4 is released
+	grunt.file.exists = grunt.file.exists || fs.existsSync || path.existsSync;
 
 	grunt.registerMultiTask('widgetlist', 'Build widgetlist.xml from given widgets archives.', function () {
 		var configPrefix = [this.name, this.target, ''].join('.');
 			configPrefix + 'downloadPrefix',
 			configPrefix + 'files'
 		);
-		var prefix = this.data.downloadPrefix;
-		var output = this.data.output;
-		var files = grunt.file.expandFiles(this.data.files);
-		var doc = builder.create('rsp', {'version': '1.0', 'encoding': 'UTF-8'})
+
+		var prefix, output, files, doc, list;
+		prefix = this.data.downloadPrefix;
+		output = this.data.output;
+		files = grunt.file.expandFiles(this.data.files);
+
+		doc = builder.create('rsp', {
+				version: '1.0',
+				encoding: 'UTF-8'
+			})
 			.att('stat', 'ok');
-		var list = doc.ele('list');
+		list = doc.ele('list');
 
 		grunt.log.writeln('Build widgetlist ' + output.green);
+
 		files.forEach(function(file, index) {
-			var baseName = path.basename(file, '.zip');
-			grunt.verbose.write('Found file ' + file.green + ' ');
+			var baseName, name, widget;
+
+			baseName = path.basename(file, '.zip');
+			grunt.verbose.write('Found file ' + file.cyan + ' ');
 			if (!fileInfoExp.test(baseName)) {
-				grunt.log.writeln('skipping'.red + ' as not suitable file name format');
+				grunt.log.writeln('skipping'.red + ' ' + file.cyan + ' as not suitable file name format');
 				return;
 			}
 			grunt.verbose.writeln('');
 
-			var info = fileInfoExp.exec(baseName);
-			var name = info[1];
+			name = fileInfoExp.exec(baseName)[1];
 
-			var widget = list.ele('widget', {id: 'widget' + index});
+			widget = list.ele('widget', {id: 'widget' + index});
 			widget.ele('title', name);
 			widget.ele('description', 'Widget ' + name);
 			widget.ele('download', prefix + path.basename(file));
 			widget.ele('compression', {type: 'zip', size: fs.statSync(file).size});
 		});
+
 		fs.writeFileSync(output, doc.toString({ pretty: true }), 'utf8');
 		grunt.log.ok();
 	});
 
+	grunt.registerMultiTask('widget', 'Build widgets archives.', function () {
+		var files, source, done;
+		files = [];
+
+		for (source in this.data) if (this.data.hasOwnProperty(source)) {
+			files.push({
+				destination: grunt.template.process(this.data[source]),
+				source: grunt.template.process(source)
+			});
+		}
+
+		done = this.async();
+		var finishTask =function() {
+			grunt.log.ok();
+			done();
+		};
+
+		grunt.util.async.forEachSeries(files, function(file, next) {
+			grunt.helper('widget-zip', file.source, file.destination, next);
+		}, finishTask);
+	});
+
+	grunt.registerHelper('widget-zip', function(sourceDirectory, destination, done) {
+		var zip, files, filenameMap;
+
+		zip = archiver.createZip({level: 1});
+		files = [];
+		filenameMap = {};
+
+		grunt.file.recurse(sourceDirectory, function(abspath, rootdir, subdir, filename) {
+			files.push(abspath);
+			filenameMap[abspath] = (subdir ? subdir + '/' : '') + filename;
+		});
+
+		zip.pipe(fs.createWriteStream(destination));
+
+		function addFile() {
+			if (!files.length) {
+				zip.finalize(function(written) {
+					grunt.log.writeln('File ' + destination.cyan + ' created (' + written + ' bytes written).');
+					done();
+				});
+				return;
+			}
+			var absPath = files.shift();
+			zip.addFile(fs.createReadStream(absPath), {name: filenameMap[absPath]}, addFile);
+		}
+
+		addFile();
+
+		zip.on('error', function(e) {
+			grunt.log.error(e);
+			grunt.fail.warn('zipHelper failed.');
+		});
+	});
 };