index.js const fs = require('fs');const common = require('./library/common');const fileHandle = require('./library/fileHandle');/** * 读取处理文件夹里的文件 */let path = '/Users/mingwang/tmp/docs/nodejs.org/api';fs.readdir(path,
const fs = require('fs'); const common = require('./library/common'); const fileHandle = require('./library/fileHandle'); /** * 读取处理文件夹里的文件 */ let path = '/Users/mingwang/tmp/docs/nodejs.org/api'; fs.readdir(path, (err, files) => { files.forEach((v) => { // 只处理文件 let filePath = path + '/' + v; if (common.isFile(filePath)) { fileHandle.removeGoogleAnalyze(filePath); } }); });fileHandle.js
const readline = require('readline'); const fs = require('fs'); module.exports = { /** * 移除 谷歌统计 相关的脚本 * @param file */ removeGoogleAnalyze: (file) => { // 按行读取文件 const rl = readline.createInterface({ input: fs.createReadStream(file) }) // 保存内容 let html = ''; rl.on('line', (line) => { if (line.indexOf('dnt_helper') < 0) { html = html + line + '\n'; } }); // 读取完成的时候,写入文件 rl.on('close', () => { fs.writeFile(file, html); }); } }common.js - 见公共模块
common.js - 见公共模块