101 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| // Inspired from https://github.com/sapegin/fledermaus
 | |
| 
 | |
| var fs = require('fs')
 | |
| var path = require('path')
 | |
| 
 | |
| /**
 | |
|  * Read directory recursively
 | |
|  *
 | |
|  * @param {String} directory path
 | |
|  * @param {Function} callback
 | |
|  * @return {Arrays} dirs & files
 | |
|  * @api public
 | |
|  */
 | |
| 
 | |
| const readdirr = function (dpath, cb) {
 | |
|   var dirs = [], files = []
 | |
|   dirs.push(dpath)
 | |
|   ;(function loop (index) {
 | |
|     if (index == dirs.length) return cb(null, dirs, files)
 | |
|     fs.readdir(dirs[index], function (err, _files) {
 | |
|       if (err) return cb(err)
 | |
|       getstat(dirs[index], _files, function (err) {
 | |
|         if (err) return cb(err)
 | |
|         loop(++index)
 | |
|       })
 | |
|     })
 | |
|   }(0))
 | |
| 
 | |
|   /**
 | |
|    * Get stat
 | |
|    *
 | |
|    * @param {String} directory path
 | |
|    * @param {Array} files
 | |
|    * @param {Function} callback
 | |
|    * @api private
 | |
|    */
 | |
|   function getstat (dpath, _files, cb) {
 | |
|     ;(function loop (index) {
 | |
|       if (index == _files.length) return cb()
 | |
|       var fpath = path.join(dpath, _files[index])
 | |
|       fs.stat(fpath, function (err, stats) {
 | |
|         if (err) return cb(err)
 | |
|         if (stats.isDirectory()) {
 | |
|           dirs.push(fpath)
 | |
|         } else {
 | |
|           files.push(fpath)
 | |
|         }
 | |
|         loop(++index)
 | |
|       })
 | |
|     }(0))
 | |
|   }
 | |
| }
 | |
| 
 | |
| const getHtmlFiles = function (folder) {
 | |
|   return new Promise((resolve) => {
 | |
|     readdirr(folder, function (err, dirs, files) {
 | |
|       const filesMatch = []
 | |
|       for (const filePath of files) {
 | |
|         if (path.extname(filePath) === '.html') {
 | |
|           filesMatch.push(filePath)
 | |
|         }
 | |
|       }
 | |
|       resolve(filesMatch)
 | |
|     })
 | |
|   })
 | |
| }
 | |
| 
 | |
| const loadPages = function (folder) {
 | |
|   return new Promise((resolve, reject) => {
 | |
|     getHtmlFiles(folder).then(function (filesPath) {
 | |
|       const files = []
 | |
|       for (const filePath of filesPath) {
 | |
|         files.push({
 | |
|           path: filePath,
 | |
|           content: fs.readFileSync(filePath, { encoding: 'utf8' })
 | |
|         })
 | |
|       }
 | |
|       resolve(files)
 | |
|     }, function (error) {
 | |
|       reject(error)
 | |
|     });
 | |
|   })
 | |
| }
 | |
| 
 | |
| const savePage = function (page) {
 | |
|   fs.writeFileSync(page.path, page.content, { encoding: 'utf8' })
 | |
| }
 | |
| 
 | |
| const savePages = function (pages) {
 | |
|   for (const page of pages) {
 | |
|     savePage(page)
 | |
|   }
 | |
| }
 | |
| 
 | |
| module.exports = {
 | |
|   loadPages: loadPages,
 | |
|   savePages: savePages
 | |
| }
 |