cirandas.net

ref: master

public/javascripts/tinymce/tools/tasks/moxiezip.js


 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
var fs = require("fs");
var path = require("path");
var ZipWriter = require('moxie-zip').ZipWriter;

module.exports = function(grunt) {
	grunt.registerMultiTask("moxiezip", "Creates zip files.", function() {
		var target = grunt.config([this.name, this.target]);
		var archive = new ZipWriter();
		var done = this.async();
		var options = target.options || {}, excludePaths = {};

		function addExcludes(excludes) {
			if (Array.isArray(excludes)) {
				excludes.forEach(function(excludePath) {
					excludePaths[path.resolve(excludePath)] = true;
				});
			}
		}

		function process(filePath, zipFilePath) {
			var args, stat = fs.statSync(filePath);

			if (excludePaths[path.resolve(filePath)]) {
				return;
			}

			zipFilePath = zipFilePath || filePath;
			filePath = filePath.replace(/\\/g, '/');
			zipFilePath = zipFilePath.replace(/\\/g, '/');

			if (options.pathFilter) {
				args = {filePath: filePath, zipFilePath: zipFilePath};
				options.pathFilter(args);
				zipFilePath = args.zipFilePath;
			}

			if (stat.isFile()) {
				var data = fs.readFileSync(filePath);

				if (options.dataFilter) {
					args = {filePath: filePath, zipFilePath: zipFilePath, data: data};
					options.dataFilter(args);
					data = args.data;
				}

				archive.addData(path.join(options.baseDir, zipFilePath), data);
			} else if (stat.isDirectory()) {
				fs.readdirSync(filePath).forEach(function(fileName) {
					process(path.join(filePath, fileName), path.join(zipFilePath, fileName));
				});
			}
		}

		options.baseDir = (options.baseDir || '').replace(/\\/g, '/');

		if (target.options.excludes) {
			addExcludes(grunt.file.expand(target.options.excludes));
		}

		this.files.forEach(function(filePair) {
			filePair.src.forEach(function(src) {
				process(src);
			});
		});

		if (options.onBeforeSave) {
			options.onBeforeSave(archive);
		}

		grunt.file.mkdir(path.dirname(options.to));

		archive.saveAs(options.to, function() {
			grunt.log.ok('Created zip file:', options.to);
			done();
		});
	});
};