Created
September 9, 2013 16:13
-
-
Save IskenHuang/6497903 to your computer and use it in GitHub Desktop.
styles link insert to html head
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
fs = require('fs') | |
cheerio = require('cheerio') | |
staticFolder = 'static' | |
cssFolder = 'css' | |
cssRoot = __dirname + '/' + cssFolder | |
htmlFolder = 'views' | |
htmlRoot = __dirname + '/../application/' + htmlFolder | |
skipFiles = ['.DS_Store', 'mail'] | |
Core = module.exports = | |
getFileList: (folderPath = null, cb) -> | |
console.log 'getFileList begin ', arguments | |
return null unless folderPath | |
return fs.readdirSync(folderPath) unless cb | |
fs.readdir folderPath, (err, files) -> | |
(if (err) then cb(null) else cb(files)) | |
getFile: (path = null, cb) -> | |
console.log 'getFile begin ', arguments | |
return null unless path | |
unless cb | |
f = fs.readFileSync(path) | |
if f | |
return f.toString() | |
else | |
return null | |
fs.readFile path, (err, data) -> | |
(if (err) then cb(null) else cb(data.toString())) | |
saveFile: (path = null, data = null, cb) -> | |
console.log 'saveFile begin ', arguments | |
return null unless path | |
return null unless data | |
if typeof(data) is 'string' | |
data = new Buffer(data, 'utf8') | |
unless cb | |
return fs.writeFileSync(path, data) | |
fs.writeFile path, data, (err) -> | |
if(err) | |
console.log 'err = ' ,err | |
else | |
console.log 'save success = ', path | |
htmlPaths = Core.getFileList(htmlRoot) | |
for i of htmlPaths | |
htmlPath = htmlPaths[i] | |
if skipFiles.indexOf(htmlPath) >= 0 | |
continue | |
htmlPath = htmlRoot+ '/' + htmlPath | |
html = Core.getFile(htmlPath) | |
$ = cheerio.load(html) | |
style = '' | |
$('link').each (index, item)=> | |
$item = $(item) | |
src = $item.attr('href') | |
srcArray = src.split('/') | |
src = cssRoot + '/' + srcArray[srcArray.length-1] | |
style += Core.getFile(src) + '\n' | |
if style.length is 0 | |
continue | |
$('head').append('<style>\n' + style + '</style>\n') | |
Core.saveFile(htmlPath, $.html()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment