* Compile
(options, callback)
| 13 | */ |
| 14 | |
| 15 | function compile(options, callback) { |
| 16 | var input = options.files[0] |
| 17 | , output = options.o || options.output || options.files[1] |
| 18 | , stream |
| 19 | , frames |
| 20 | , writer; |
| 21 | |
| 22 | if (output) { |
| 23 | if (output === '-') { |
| 24 | stream = process.stdout; |
| 25 | output = '[stdout]'; |
| 26 | } else { |
| 27 | stream = fs.createWriteStream(output); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | options.stream = stream; |
| 32 | options.delay = options.delay || 100; |
| 33 | options.png = options.png || /\.png$/i.test(output); |
| 34 | |
| 35 | if (Array.isArray(input)) { |
| 36 | frames = input; |
| 37 | } else if (typeof input === 'string') { |
| 38 | if (~input.indexOf('.json')) { |
| 39 | log('parsing json'); |
| 40 | frames = JSON.parse(fs.readFileSync(input, 'utf8')); |
| 41 | } else { |
| 42 | log('reading file'); |
| 43 | frames = [fs.readFileSync(input, 'utf8')]; |
| 44 | } |
| 45 | } else { |
| 46 | return callback(new Error('No input file specified.')); |
| 47 | } |
| 48 | |
| 49 | if (options.range) { |
| 50 | frames = frames.slice(options.range[0], options.range[1]); |
| 51 | } |
| 52 | |
| 53 | log('initializing writer'); |
| 54 | writer = new SGRWriter(frames, options); |
| 55 | |
| 56 | writer.on('done', function(event) { |
| 57 | log('stream: ' + event); |
| 58 | log('wrote image to %s', output); |
| 59 | return callback(); |
| 60 | }); |
| 61 | |
| 62 | log('writing image'); |
| 63 | writer.write(); |
| 64 | |
| 65 | return writer; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Helpers |