const fs = require('fs'); const path = require('path'); module.exports = function(root, filePath, content) { let filePathList = filePath.split('/'); let len = filePathList.length; let parentPath = root; if(!isFileExist(parentPath)) { try { fs.mkdirSync(parentPath); } catch(e) { console.log(e); } } filePathList.map((item, index) => { console.log('create html:', parentPath); let absPath = path.join(parentPath, item); if(!isFileExist(absPath)) { if(len - 1 !== index) { try { fs.mkdirSync(absPath); } catch(e) { console.log(e); } } } if(len - 1 === index) { try { fs.writeFileSync(absPath, content); } catch(e) { console.log(e); } } parentPath = absPath; }) } function isFileExist(filePath) { try { fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK); return true; } catch(e) { return false; } }