将一个案例代码分离html,css,js,时钟html文件名为dataTime下的index.html
const fs = require("fs");
const path = require("path");
fs.readFile("./dataTime/index.html","utf-8",function(err,user){
if(err){
return console.log("获取文件失败"+err.message);
}
resolveCss(user);
resolveJs(user);
resolveHtml(user);
})
function resolveHtml(user){
let patt = /<style>[\s\S]*<\/style>/;
let requsCss = patt.exec(user);
user = user.replace(requsCss[0],"<link rel='stylesheet' href='./dataTime/index.css'>")
let patts = /<script>[\s\S]*<\/script>/;
let requsJs = patts.exec(user);
user = user.replace(requsJs[0],"<script src='./dataTime/index.js'></script>")
fs.writeFile("./dataTime/index.html",user,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}
function resolveJs(user){
let patt = /<script>[\s\S]*<\/script>/;
let requsCss = patt.exec(user);
let textStyle = requsCss[0].replace("<script>","").replace("</script>","")
fs.writeFile("./dataTime/index.js",textStyle,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}
function resolveCss(user){
let patt = /<style>[\s\S]*<\/style>/;
let requsCss = patt.exec(user);
let textStyle = requsCss[0].replace("<style>","").replace("</style>","")
fs.writeFile("./dataTime/index.css",textStyle,"utf-8",function(err){
if(err){
return console.log("创建文件失败"+err.message);
}
})
}