Skip to content

Instantly share code, notes, and snippets.

@shellphon
Created December 15, 2014 15:54
Show Gist options
  • Save shellphon/92b59aa1e4d72f0633cd to your computer and use it in GitHub Desktop.
Save shellphon/92b59aa1e4d72f0633cd to your computer and use it in GitHub Desktop.
node-my-tool-html2jsp
/*
API:
获取参数 process.argv
运行nodejs时,node xxx.js yyy
argv[0] : node
argv[1] : xxx.js //当前脚本名
argv[2] : yyy //文件名,不包含后缀
……
读取文件 fs readFileSync 该方法会返回一个流对象,通过toString()可以转换为字符串
*/
var fs = require('fs'),
filename = process.argv[2],
change = function(prefile){//传入文件路径加文件前缀如: xxx.html => xxx ./ddd/yyy.html => ./ddd/yyy
var buf = fs.readFileSync(prefile+".html"),
str = buf.toString(),
res = str.replace(/include([^%>]*)\.html/g, "include$1.jsp");
fs.writeFile(prefile+'.jsp',res,function(err){
if(err)throw err;
console.log(prefile+'.html 替换搞定');
});
};
change(filename);
if(fs.existsSync('./pub')){
fs.readdir('./pub',function(err, files){
if(err)console.log("oops");
[].forEach.call(files, function(item,index){
if(/.*\.html/.test(item)){
change("./pub/"+item.substring(0, item.indexOf('.')));
}
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment