forked from frontainer/gulp-sprite-glue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·161 lines (152 loc) · 4.25 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// through2はnodeのtransform streamをラップするもの
var through = require('through2');
var gutil = require('gulp-util');
var exec = require('child_process').exec;
// コマンド群
var COMMANDS = {
algorithm: '--algorithm=%val%',
crop: '--crop',
caat: '--caat%val%',
cachebuster: '--cachebuster',
cachebusterFilename: '--cachebuster-filename',
cachebusterFilenameOnlySprites: '--cachebuster-filename-only-sprites',
cocos2d: '--cocos2d%val%',
css: '--css=%val%',
img: '--img=%val%',
cssTemplate: '--css-template=%val%',
force: '--force',
followLinks: '--follow-links',
html: '--html%val%',
json: '--json%val%',
jsonFormat: '--json-format=%val%',
less: '--less%val%',
lessTemplate: '--less-template=%val%',
margin: '--margin=%val%',
namespace: '--namespace=%val%',
noImg: '--no-img',
noCss: '--no-css',
ordering: '--ordering=%val%',
padding: '--padding=%val%',
png8: '--png8',
project: '--project',
pseudoClassSeparator: '--pseudo-class-separator=%val%',
quiet: '--quiet',
recursive: '--recursive',
ratios: '--ratios=%val%',
retina: '--retina',
source: '--source=%val%',
output: '--output=%val%',
scss: '--scss%val%',
scssTemplate: '--scss-template=%val%',
separator: '--separator=%val%',
spriteNamespace: '--sprite-namespace=%val%',
url: '--url=%val%',
watch: '--watch'
};
// デフォルトオプション
var DEFAULT_OPTIONS = {
algorithm: null,//square|vertical|hortizontal|diagonal|vertical-right|horizontal-bottom
crop: false,
caat: false,
cachebuster: false,
cachebusterFilename: null,
cachebusterFilenameOnlySprites: false,
cocos2d: false,
css: null,
img: null,
cssTemplate: null,
force: false,
followLinks: false,
html: false,
json: false,
jsonFormat: null,
less: false,
lessTemplate: null,
margin: null,
namespace: null,
noImg: false,
noCss: false,
ordering: null,
padding: null,
png8: false,
project: false,
pseudoClassSeparator: null,
quiet: false,
recursive: false,
ratios: null,
retina: false,
source: null,
output: null,
scss: false,
scssTemplate: null,
separator: null,
spriteNamespace: null,
url: null,
watch: false
};
/**
* プラグイン関数
* @param options
* @returns {*}
*/
function glue(dest,options) {
// 入力ファイルを受け取って処理するstreamオブジェクトを生成
var commandOptions = [];
if (options) {
if (options.cmd) {
//cmdオプションがある場合はそのまま流し込み
commandOptions.push(options.cmd);
} else {
//プロパティ見てオプションコマンドの追加
for (var key in options) {
var v = parseOption(key, options[key]);
if (v) {
commandOptions.push(v);
}
}
}
}
var stream = through.obj(function (file, enc, callback) {
if (!dest) {
this.emit('error', new gutil.PluginError('gulp-sprite-glue', 'required dest path.'));
callback();
}
if (file.isDirectory()) {
var command = ['glue'];
command.push('"'+file.path+'"');
command.push('"'+dest+'"');
command = command.concat(commandOptions);
console.log('Execute: ' + command.join(' '));
exec(command.join(' '), function (err, stdout, stderr) {
console.log(stdout);
callback();
});
this.push(file);
} else {
this.push(file);
}
});
return stream;
}
/**
* オプションパース関数
* @param key
* @param value
* @returns {*|XML|string|void}
*/
function parseOption(key,value) {
var val = COMMANDS[key];
if (!val) {
grunt.log.warn('Not found command ' + key + '(' + value + ')');
return;
}
if (!value || value === false) {
return;
}
if (/^(caat|html|json|less|scss|cocos2d)$/.test(key)) {
value = value === true ? '' : '=' + value;
}
return val.replace('%val%',value);
}
// プラグイン関数をエクスポート
module.exports = glue;