Last active
December 22, 2015 08:49
-
-
Save IskenHuang/6447764 to your computer and use it in GitHub Desktop.
make html scripts merge to one uglify script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
sys = require('sys'), | |
exec = require('child_process').exec, | |
staticFolder = 'static', | |
jsFolder = 'js', | |
jsRoot = __dirname + '/' + jsFolder + '/', | |
htmlRoot = __dirname+ '/../application/views', | |
suffix = '.min', | |
skipFiles = ['.DS_Store', 'mail'], | |
sctiptsTag = '<script src="%s"></script>', | |
jsTagRegex = new RegExp( '\<script.*src\=\"\/' + staticFolder + '.*\".*\>\<\/script\>', 'ig'), | |
srcRegex = /src\=\".*\"/ig, | |
Core; | |
Core = module.exports = { | |
getFileList: function(path, cb) { | |
fs.readdir(path, function(err, files){ | |
return (err) ? cb(null) : cb(files); | |
}); | |
}, | |
getFile: function(path, cb) { | |
fs.readFile( path, function(err, data){ | |
return (err) ? cb(null) : cb(data.toString()); | |
}); | |
}, | |
getScriptsPath: function(fileData, cb) { | |
var scripts = fileData.match(jsTagRegex); | |
if(scripts && scripts.length){ | |
var fps = []; | |
for(var i in scripts){ | |
var script = scripts[i].match(srcRegex)[0], | |
fp = script.substr( 4, script.length); | |
fps.push(fp); | |
} | |
cb(fps); | |
}else{ | |
return cb(null); | |
} | |
}, | |
getViewScriptsJSPath: function( filePath, cb) { | |
var _this = this; | |
_this.getFile(filePath, function(fileData){ | |
_this.getScriptsPath(fileData, function(jsPaths){ | |
return cb(jsPaths); | |
}); | |
}); | |
}, | |
replaceFileNameExtension: function(fileName) { | |
return fileName.replace(/\.[^.]+$/i, '') + suffix + '.js'; | |
}, | |
getHtmlScriptTag: function(fName) { | |
fName = this.replaceFileNameExtension(fName); | |
return sctiptsTag.replace(/%s/i, '/' + staticFolder + '/' + jsFolder + '/' + fName); | |
}, | |
saveViewFileAndReplaceScripts: function( filePath, cb) { | |
var fna = filePath.split(/\//ig), | |
fName = fna[fna.length-1], | |
_this = this; | |
_this.getFile(filePath, function(fileData){ | |
fileData = fileData.replace(jsTagRegex, ''); | |
var fileDataArray = fileData.split(/\<\/body\>/i), | |
jsName = _this.getHtmlScriptTag(fName), | |
dataString = fileDataArray[0] + jsName + '</body>' + fileDataArray[1], | |
// save file | |
bufferString = new Buffer(dataString, 'utf8'); | |
fs.writeFile(htmlRoot + '/' + fName, bufferString, function(err){ | |
if(err){ | |
return console.log('save file error = ', err); | |
}else{ | |
return console.log('save file success = ', htmlRoot + fName); | |
} | |
}); | |
return; | |
}); | |
}, | |
commandline: function(cmd, cb) { | |
var _this = this; | |
exec( cmd, function (error, stdout, stderr) { | |
sys.print('stdout: ' + stdout); | |
sys.print('stderr: ' + stderr); | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
} | |
return _this; | |
}); | |
}, | |
uglify: function(fileName, cb) { | |
var _this = this, | |
fPath = htmlRoot + '/' + fileName, | |
_fName = fileName, | |
_regex = new RegExp( '\/' + staticFolder + '\/' + jsFolder, 'i'); | |
this.getViewScriptsJSPath( fPath, function(jsPaths){ | |
if(!jsPaths){ | |
return; | |
} | |
for(var i in jsPaths){ | |
jsPaths[i] = jsPaths[i].replace( _regex, jsRoot); | |
} | |
var jsString = jsPaths.join(' '), | |
uglifyStr = 'uglifyjs ', | |
_fName = fileName.replace(/\.[^.]+$/i, ''); | |
uglifyStr += jsString + ' -o ' + jsRoot + _fName + suffix + '.js -c'; | |
return _this.commandline(uglifyStr); | |
}); | |
}, | |
}; | |
Core.getFileList(htmlRoot, function(viewFiles){ | |
for(var i in viewFiles){ | |
var fName = viewFiles[i], | |
fPath = htmlRoot + '/' + fName; | |
if(skipFiles.indexOf(fName) >= 0){ | |
continue; | |
} | |
Core.saveViewFileAndReplaceScripts(fPath); | |
Core.uglify(fName); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment