/
index.js
73 lines (56 loc) · 1.83 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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var swfBuild = require('swf-build');
var beep = require('beepbeep');
var _ = require('lodash');
var jinxCompiler = require('jinx-compiler')
module.exports = function (output,params,options) {
if (!output) throw new gutil.PluginError('gulp-jinx-compiler', '`output` required');
var defaults = {
tmp:'.tmp'
}
var paramsDefault = {
'debug':true,
'library-path':[]
}
params = _.extend({},paramsDefault,params);
options = _.extend({},defaults,options);
return through.obj(function (file, enc, callback) {
var destAs = path.join(
path.join(options.tmp,'jinx'), // tmp dir
path.dirname(path.relative('./',file.path)), //file dir
path.basename(file.path,'.jinx')+'.as' // file name
);
params['library-path'].push(path.dirname(destAs));
var resp = jinxCompiler(file,destAs);
file.contents = resp.contents;
mkdirp.sync(path.dirname(destAs));
fs.writeFileSync(destAs,resp.contents);
file.path = destAs;
if(resp.swc && Array.isArray(resp.swc) && resp.swc.length){
for(var i in resp.swc) path.relative(resp.swc[i],destAs);
}
params['library-path'] = _.uniq(params['library-path'].concat(resp.swc));
var args = [file.path]; // input
if(typeof output==='string') { // if string are output path
args.push((!path.extname(path.resolve(output)) ? path.resolve(output,path.basename(file.path,'.as')+'.swf') : path.resolve(output)));
}
if(typeof output==='object') { //params
args.push(output);
} else {
if(params) args.push(params);
}
args.push(function(err, stdout, stderr){ // callback
if(err) {
beep();
console.log(err);
}
callback(null, file);
});
swfBuild.bind.apply(swfBuild, [null].concat(args))();
});
};