node自動化構建前端專案原始碼
var projectData={
"name":"node-new",
"fileData":[
{
"name":"js",
"type":"dir"
},
{
"name":"css",
"type":"dir"
},
{
"name":"images",
"type":"dir"
},
{
"name":"index.html",
"type":"file",
"content":'<!DOCTYPE html>\n<html lang="en">\n\t<head>\n\t\t<meta charset"UTF-8">\n\t\t<meta name="viewport" content="width=device-width, initial-scale=1.0">\n\t\t<meta http-equiv="X-UA-Compatible" content="ie=edge">\n\t\t<title>hello node.js/title>\n\t\t<link rel="stylesheet" href="css/reset.css"></link>\n\t</head>\n\t<style>\n\t</style>\n\t<body>\n\t\t<script src="js/index.js"></script>\n\t\t<script>\n\t\t</script>\n\t</body>\n</html>'
},
]
};
var fs = require('fs');
if(projectData.name){
fs.mkdirSync(projectData.name);
var fileData = projectData.fileData;
if(fileData&&fileData.forEach){
fileData.forEach(function(f){
f.path=projectData.name+'/'+f.name;
f.content=f.content?f.content:'';
switch(f.type){
case 'dir':
fs.mkdirSync(f.path);
break;
case 'file':
fs.writeFileSync(f.path,f.content);
break;
case 'default':
break;
}
})
console.log('自動化構建成功...');
}
}
ps:使用方法:給檔案命名為create.js,在terminal裡面輸入node create.js即可執行,自動構建前端專案至當前資料夾,檔名以及檔案內容都可自行更改,對開發十分便利。