'use strict'; var events = require('events'); var fetch = require('fetch'); var fs = require('fs-extra'); var factory = require('./factory'); var config = require('./asset-assembler-config'); function buildEnd(builder, targetFile, resultUriList, callback) { fs.appendFile(targetFile, builder.config.assetFooter, function (err) { if (err) { callback(err); } else { var buildTime = new Date().getTime() - builder.startTime, buildResultSize = resultUriList.length, targetStats = fs.statSync(targetFile), targetSize = targetStats.size; if (builder.config.linkTargetSingleResult) { resultUriList = [builder.config.linkTarget]; } builder.emit('file-write-post',targetFile); builder.emit('log','debug','target size: '+targetSize); builder.emit('log','info','build result size: '+resultUriList.length+' from: '+buildResultSize); builder.emit('log','info','build done in: '+buildTime+' ms.'); builder.emit('result',resultUriList); builder.emit('end'); callback(); } }); } function downloadFile(builder, remoteUrl, localFile, callback) { builder.emit('file-download-pre',remoteUrl); builder.emit('log','debug','downloadFile: '+remoteUrl); fs.ensureFile(localFile, function(err) { if (err) { callback(err); } else { var stream = new fetch.FetchStream(remoteUrl); stream.on('error', function(err) { callback(err); }); stream.on('end', function() { builder.emit('file-download-post',remoteUrl); callback(); }); stream.pipe(fs.createWriteStream(localFile)); } }); } function downloadFileList(builder, downloadList, callback) { if (downloadList.length === 0) { callback(); return; } var download = downloadList.pop(); downloadFile(builder, download.remoteUrl, download.localFile, function(err) { if (err) { callback(err); } else { downloadFileList(builder, downloadList, callback); } }); } function aggregateFileList(builder, targetFile, aggregateList, readFile, callback) { if (aggregateList.length === 0) { callback(); return; } var aggregateFile = aggregateList.pop(); builder.emit('file-read-pre',aggregateFile); readFile(aggregateFile, function(err, data) { if (err) { callback(err); } else { builder.emit('file-read-post',aggregateFile); builder.emit('log','debug','readFile: '+aggregateFile+' size: '+data.length); if (aggregateList.length > 0) { data = data + builder.config.assetSeperator; } fs.appendFile(targetFile, data, function (err) { if (err) { callback(err); } else { aggregateFileList(builder, targetFile, aggregateList, readFile, callback); } }); } }); } function mapLocalFileSync(assembler, linkSource) { var uriMapping = assembler.config.linkMapping; if (uriMapping) { var uriMappingKeys = Object.keys(uriMapping); uriMappingKeys.sort(function(a, b) { return a.length < b.length; // longest first as we break on first hit }); for (var ii = 0; ii < uriMappingKeys.length; ii++) { var uriKey = uriMappingKeys[ii]; var localPath = uriMapping[uriKey]; var mapIndex = linkSource.indexOf(uriKey); if (mapIndex === 0) { return localPath+linkSource.substring(uriKey.length); } } } return linkSource; } function buildAsset(builder, targetFile, readFile, callback) { var uriList = builder.config.linkSources; var localFileList = []; var resultUriList = []; var downloadList = []; for (var i = 0; i < uriList.length; i++) { var assetItem = uriList[i]; var remoteUrl = null; var remoteForce = null; var remoteIndex = assetItem.indexOf('@'); if (remoteIndex > 0) { remoteUrl = assetItem.substring(remoteIndex + 1); if (remoteUrl.indexOf('@') === 0) { remoteUrl = assetItem.substring(remoteIndex + 2); remoteForce = true; } assetItem = assetItem.substring(0,remoteIndex); } var localFile = mapLocalFileSync(builder, assetItem); // override force on all files if (remoteForce===undefined && builder.config.downloadForce !== undefined) { remoteForce = builder.config.downloadForce; } if (remoteUrl && (remoteForce || !fs.existsSync(localFile))) { downloadList.push({ remoteUrl: remoteUrl, localFile: localFile }); } localFileList.push(localFile); resultUriList.push(assetItem); } downloadList.reverse(); localFileList.reverse(); downloadFileList(builder, downloadList, function(err) { if (err) { callback(err); } else { //for(.... //if (!fs.existsSync(localFile)) { // Log.warn("illegal entry: "+localFile); // continue; //} aggregateFileList(builder, targetFile, localFileList, readFile, function(err) { if (err) { callback(err); } else { buildEnd(builder, targetFile, resultUriList, callback); } }); } }); } function buildStart(assembler, readFile, callback) { assembler.startTime = new Date().getTime(); assembler.emit('begin'); assembler.emit('log','info','build begin for: '+assembler.config.linkTarget); if (assembler.config.downloadForce) { assembler.emit('log','info','build using forced downloads.'); } var targetFile = mapLocalFileSync(assembler, assembler.config.linkTarget); fs.ensureFile(targetFile, function (err) { if (err) { callback(err); } else { assembler.emit('file-write-pre',targetFile); fs.writeFile(targetFile, assembler.config.assetHeader, function(err) { if (err) { callback(err); } else { buildAsset(assembler, targetFile, readFile, callback); } }); } }); } function runAssembler(assembler, readFile, callback) { if (assembler.config.downloadStartDelay) { setTimeout(function() { buildStart(assembler, readFile, callback); }, assembler.config.downloadStartDelay); } else { buildStart(assembler, readFile, callback); } } function runAssemblerSafe(assembler, readFile, callback) { runAssembler(assembler, readFile, function(err) { // send error if (err) { assembler.emit('error',err); } // finish callback if (callback !== undefined) { callback(err); } }); } function AssetAssembler(config, readFile) { this.config = config; this.startTime = 0; this.readFile = readFile || factory.assembler.constructor.readFile(); events.EventEmitter.call(this); } AssetAssembler.prototype.__proto__ = events.EventEmitter.prototype; AssetAssembler.prototype.run = function(callback) { if (callback === undefined) { throw new Error('no callback'); } var self = this; config.checkConfig(self.config,function(err) { if (err) { callback(err); } else { config.fillDefaults(self.config, function (err) { if (err) { callback(err); } else { runAssemblerSafe(self, self.readFile, callback); } }); } }); }; module.exports = AssetAssembler;