摘要: 本文要實現的內容,使用nodejs 對文件的增刪改查,演示的例子-》分離出一個html 文件裏面的script 和style 裏面的內容,而後單獨生成js文件和css 文件。中間處理異步的api-》async/await , Promisecss
項目託管:extract-js-css , 歡迎starhtml
直接上代碼:node
// 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 內容 /** * */ 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 /** * 向文件中追加內容 * @param {是文件名字} path * @param {寫入文件的內容} 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}數據已追加到文件`); resolve() }); }) } // 寫一個html const writeHtml = (path, data) => { return new Promise(resolve => { fs.writeFile(path, data, (err) =>{ if(err) { console.log('err', err) return } console.log(`${path} 寫入成功,功能結束!`); resolve() // 必須resolve 。否則 promise 就到此爲止,調用該方法後面的代碼將不執行 }) }) } // 插件 方法入口 (async ()=>{ console.log('==========================game-start============================='); await deleteDir('./test'); console.log('我應該是等---刪除文件夾後---纔出現') await mkdirTest(); console.log('我應該是在---文件夾新建成功---後出現!'); 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">'; 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('我應該是在---css寫入成功---後出現!'); await appendFile('./test/test.js', JSON.parse(jsContent), 'js'); console.log('我應該是在---js寫入成功---後出現!'); htmlContentStr = originContent .replace(allStyleReg, '') .replace(cssReg, cssLink) .replace(allScriptReg, '') .replace(jsReg, jsrc); console.log('copyTest.html 文本已經格式化,準備寫入'); await writeHtml('./test/copyTest.html', htmlContentStr); console.log('==========================game-over============================='); })()
歡迎評閱和指正!我是貳伶邇git