UNPKG

mooncake-cli

Version:
92 lines (78 loc) 2.01 kB
const fs = require('fs') const path = require('path') const os = require('os') const copyFile = function(srcPath, tarPath, cb) { const rs = fs.createReadStream(srcPath) rs.on('error', function(err) { if (err) { console.log('read error', srcPath) } cb && cb(err) }) const ws = fs.createWriteStream(tarPath) ws.on('error', function(err) { if (err) { console.log('write error', tarPath) } cb && cb(err) }) ws.on('close', function(ex) { cb && cb(ex) }) rs.pipe(ws) } const copyTempalte = function(srcDir, tarDir, cb) { fs.readdir(srcDir, function(err, files) { let count = 0 const checkEnd = function() { ++count == files.length && cb && cb() } if (err) { checkEnd() return } files.forEach(function(file) { const srcPath = path.join(srcDir, file) const tarPath = path.join(tarDir, file) fs.stat(srcPath, function(err, stats) { if (stats.isDirectory()) { fs.mkdir(tarPath, function(err) { if (err) { console.log(err) return } copyTempalte(srcPath, tarPath, checkEnd) }) } else { copyFile(srcPath, tarPath, checkEnd) } }) }) //为空时直接回调 files.length === 0 && cb && cb() }) } const newPage = function(pageName, cb) { const tarDir = path.join(process.cwd(), 'src/pages', pageName) fs.mkdir(tarDir, function(err) { if (err) { console.log(err) return } copyTempalte(path.join(__dirname, '../template/src/pages/index'), tarDir, cb) }) } const ip = () => { const ifaces = os.networkInterfaces() const ips = [] for (const dev in ifaces) { ifaces[dev].forEach(function(details) { if (details.family === 'IPv6' || details.address == '127.0.0.1') return ips.push(details.address) }) } return ips[0] }; exports.copyTempalte = copyTempalte exports.newPage = newPage exports.ip = ip