forked from megahertz/gulp-audiosprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (103 loc) · 3.24 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var path = require('path');
var fs = require('fs');
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var audiosprite = require('audiosprite');
var PLUGIN_NAME = 'gulp-audiosprite';
module.exports = gulpAudioSprite;
function gulpAudioSprite(options) {
var files = [];
options = setDefaults(options);
return through.obj(transform, flush);
function transform(file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
files.push(file.path);
callback();
}
function flush(callback) {
var self = this;
audiosprite(files, options, function(err, result) {
if (err) {
return callback(new gutil.PluginError(PLUGIN_NAME, err));
}
result = convertData(result, options, self);
var jsonName = (options.output || 'output') + '.json';
self.push(new gutil.File({
base: path.dirname(jsonName),
path: jsonName,
contents: new Buffer(JSON.stringify(result, null, 2), 'binary')
}));
callback();
});
}
}
function convertData(data, options, self) {
var urls = data.urls || data.resources || (data.src ? [data.src] : []) || [];
if (options.format == 'createjs') {
urls = [];
var extentions = options.export.split(',');
for (var i = 0; i < extentions.length; i++) {
var ext = extentions[i];
urls.push(data.src.replace(new RegExp('\\.' + extentions[0] + '$'), '.' + ext));
}
}
urls.forEach(function(url) {
self.push(new gutil.File({
base: path.dirname(url),
path: url,
contents: fs.createReadStream(url)
}));
});
urls = urls.map(function(url) {
var base = path.basename(url);
return options._path ? options._path + '/' + base : base;
});
switch (options.format) {
case 'howler':
data.urls = urls;
break;
case 'createjs':
data.src = urls[0];
break;
default:
data.resources = urls;
}
return data;
}
function setDefaults(options) {
options = options || {};
if (!options.export) options.export = 'ogg,m4a,mp3,ac3'
var tmp = require('os').tmpDir() || '.';
options.output = tmp + '/' + (options.output ? options.output : 'sprite');
if (options.path) {
options._path = options.path;
delete options.path;
}
setLogger(options);
return options;
}
function setLogger(options) {
var DEBUG = 'debug';
var INFO = 'info';
var NOTICE = 'notice';
var levels = [DEBUG, INFO, NOTICE];
function log(level, text, data) {
var userLevel = levels.indexOf(options.log || 'info');
var logLevel = levels.indexOf(level);
if (logLevel < userLevel) {
return;
}
gutil.log(text, data);
}
options.logger = {
debug: log.bind(null, DEBUG),
info: log.bind(null, INFO),
log: log.bind(null, NOTICE)
};
}