/** * Created by littledu on 15/5/16. */ var fs = require('fs'), path = require('path'), localIp = require('quick-local-ip'); module.exports = function (config) { var html = [ '', '', '', '', '', '', '', '', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' ].join(''), tmpHtml = '', length = '/dev/html/'.length, collector = listdir('./dev/html'), ip = localIp.getLocalIP4(); showdir(collector, 0); function listdir(dir) { var collector = { 'name': dir, 'type': 'dir', 'url': '', 'child': [] }; files = fs.readdirSync(dir); files.forEach(function (file) { var absolutePath = dir + '/' + file, stats = fs.statSync(absolutePath); var url = absolutePath.substring(absolutePath.indexOf('./dev/html/') + length + 1); if (stats.isDirectory() && (stats.isDirectory() !== '.' || stats.isDirectory() !== '..')) { collector['child'].push(listdir(absolutePath)); } else { collector['child'].push({ 'name': path.basename(absolutePath), 'type': 'file', 'url': url }); } }); return collector; } function showdir(collector, level) { var file = collector['name'], basename = path.basename(file); var indent = 25 * (level - 1); if (indent <= 0) { indent = 10; } if (collector['type'] == 'dir') { if (level != 0) { html += ''; } collector['child'].forEach(function (item) { showdir(item, level + 1); }); } if (collector['type'] == 'file') { if (path.extname(file) === '.html' && basename !== 'TmTIndex.html') { if (level === 1) { tmpHtml += ''; } else { html += ''; } } } } html = html + tmpHtml; html += '
文件名二维码
[目录]: ' + basename + '
' + basename + '
' + basename + '
'; var out = fs.createWriteStream('./dev/html/TmTIndex.html', {encoding: "utf8"}); out.write(html, function (err) { if (err) console.log(err); }); out.end(); }