摘要: 本文要實(shí)現(xiàn)的內(nèi)容,使用nodejs 對(duì)文件的增刪改查,演示的例子-》分離出一個(gè)html 文件里面的script 和style 里面的內(nèi)容,然后單獨(dú)生成js文件和css 文件。中間處理異步的api-》async/await , Promise
項(xiàng)目托管:extract-js-css , 歡迎star
直接上代碼:
// import fs from 'fs' var fs = require('fs') // import csscomb from 'csscomb' // var csscomb = require('csscomb') // var comb = new csscomb('zen'); // console.log(comb) // 刪除文件 const deleteFile = (path)=>{ return new Promise(resolve => { fs.unlink(path, (err) => { if (err) { console.log(err) return }; console.log(`已成功刪除 ${path}文件`); resolve() }); }) } // 刪除文件夾 const deleteDir = async (path)=>{ let _files = await new Promise (resolve => { fs.readdir(path, (err,files) => { if (err) { console.log(err) }; console.log(`已成功讀取 ${path} 文件夾`); resolve(files) }) }) if(_files && _files.length) { for(let i =0;i<_files.length;i++) { // console.log(_files[i],'innnnnn') await deleteFile('./test/'+ _files[i]) } } // console.log('delete hou') await new Promise(resolve => { fs.rmdir(path, (err) => { if (err) { console.log(err) }; console.log(`已成功刪除空 ${path}文件夾`); resolve() }) }); } const emptyDir = (path) => { return new Promise(resolve => { fs.rmdir(path, (err) => { if (err) { console.log(err) }; console.log(`已成功刪除空 ${path}文件夾`); resolve() }) }) } // 新建文件夾 /** * */ const mkdirTest = ()=>{ return new Promise(resolve => { fs.mkdir('./test', { recursive: true }, (err, data)=>{ if (err) { console.log(err) }; console.log('新建文件夾成功') resolve() }) }) } // 讀取html 內(nèi)容 /** * */ const readHtml = ()=>{ return new Promise(resolve => { fs.readFile('./test.html', 'utf-8', (err, data)=>{ if(err) { throw Error(err) } console.log('test.html 讀取成功!--NO1') resolve(data) }) }) } // 寫入css 和js /** * 向文件中追加內(nèi)容 * @param {是文件名字} path * @param {寫入文件的內(nèi)容} data * @param {文件類型} type * @author erlinger * @time */ const appendFile = (path, data, type) => { return new Promise(resolve => { fs.appendFile(path, data, (err) => { if (err) { console.log(err) }; console.log(`${type}數(shù)據(jù)已追加到文件`); resolve() }); }) } // 寫一個(gè)html const writeHtml = (path, data) => { return new Promise(resolve => { fs.writeFile(path, data, (err) =>{ if(err) { console.log('err', err) return } console.log(`${path} 寫入成功,功能結(jié)束!`); resolve() // 必須resolve 。不然 promise 就到此為止,調(diào)用該方法后面的代碼將不執(zhí)行 }) }) } // 插件 方法入口 (async ()=>{ console.log('==========================game-start============================='); await deleteDir('./test'); console.log('我應(yīng)該是等---刪除文件夾后---才出現(xiàn)') await mkdirTest(); console.log('我應(yīng)該是在---文件夾新建成功---后出現(xiàn)!'); let cssReg = /<style>[\s|\S]*?<\/style>/ig; let jsReg = /<script>[\s|\S]*?<\/script>/ig; let allStyleReg = /<\/style>[\s|\S]*?<style>/ig; let allScriptReg = /<\/script>[\s|\S]*?<script>/ig; let cssLink = '<link rel="stylesheet" href="./test.css" rel="external nofollow" >'; let jsrc = '<script src="./test.js"></script>'; let styleCollection, scriptColletion; let cssContent = '', jsContent = '', htmlContentStr = ''; let originContent = await readHtml(); styleCollection = originContent.match(cssReg); scriptColletion = originContent.match(jsReg); // 處理 css for (let i =0;i<styleCollection.length;i++) { cssContent += JSON.stringify(styleCollection[i]); } cssContent = cssContent.replace(/<style>/g,'').replace(/<\/style>/g, '').replace(/("")/g,'') for (let i =0;i<scriptColletion.length;i++) { jsContent += JSON.stringify(scriptColletion[i]); } jsContent = jsContent.replace(/<script>/g,'').replace(/<\/script>/g, '') .replace(/<\/script>"*<script>/g, '').replace(/("")/g,'') await appendFile('./test/test.css', JSON.parse(cssContent), 'css'); console.log('我應(yīng)該是在---css寫入成功---后出現(xiàn)!'); await appendFile('./test/test.js', JSON.parse(jsContent), 'js'); console.log('我應(yīng)該是在---js寫入成功---后出現(xiàn)!'); htmlContentStr = originContent .replace(allStyleReg, '') .replace(cssReg, cssLink) .replace(allScriptReg, '') .replace(jsReg, jsrc); console.log('copyTest.html 文本已經(jīng)格式化,準(zhǔn)備寫入'); await writeHtml('./test/copyTest.html', htmlContentStr); console.log('==========================game-over============================='); })()
代碼確實(shí)沒什么好解釋的,慢慢看就明白了。需要提醒以下:
下面是 執(zhí)行一個(gè)主 async方法的一個(gè)過程
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com