From b0ed32358776ca81efc476bcc31227cabdb5f14a Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 27 Jul 2019 14:36:28 +0800 Subject: [PATCH] Update build script. --- build.js | 95 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/build.js b/build.js index 86be40e..fb9c74a 100644 --- a/build.js +++ b/build.js @@ -1,62 +1,63 @@ -var fs = require("fs"); -var path = require('path'); +const fs = require('fs'); +const path = require('path'); //检查指定路径的文件或者目录是否存在 -function exists(_path){ - return fs.existsSync(_path); +function exists(_path) { + return fs.existsSync(_path); } //判断是不是文件 -function isFile(_path){ - return exists(_path) && fs.statSync(_path).isFile(); -} +function isFile(_path) { + return exists(_path) && fs.statSync(_path).isFile(); +} //判断是不是目录 -function isDir(_path){ - return exists(_path) && fs.statSync(_path).isDirectory(); +function isDir(_path) { + return exists(_path) && fs.statSync(_path).isDirectory(); } - -var path_root = process.cwd(); -var data = {}; - -// 初始化数据 -initData(path_root) -function initData(dirname){ - var files = fs.readdirSync(dirname); - var filesArr = [] - files.forEach(function(_filename,idx){ - _filepath = path.join(dirname,_filename) - if (_filename !== '.git' && _filename !== '.github' && _filename !== 'svg' && _filename !== '.DS_Store'){ - if(isDir(_filepath)){ - data[_filename] = initData(_filepath); - }else if(isFile(_filepath)){ - filesArr.push(_filepath.replace(path_root,'').replace('/','')) - } +const path_root = process.cwd(); +const data = {}; + +function initData(dirname) { + return new Promise((resolve, reject) => { + const files = fs.readdirSync(dirname); + const filesArr = [] + files.forEach(async (_filename, idx) => { + _filepath = path.join(dirname, _filename) + if (_filename !== '.git' && _filename !== '.github' && _filename !== 'svg' && _filename !== '.DS_Store') { + if (isDir(_filepath)) { + data[_filename] = await initData(_filepath); + } else if (isFile(_filepath)) { + filesArr.push(_filepath.replace(path_root, '').replace('/', '')) } - }) - return filesArr + } + }); + resolve(filesArr) + }); } -// 这里没什么卵用 -fs.writeFileSync(path.join(path_root, 'data.json'), JSON.stringify(data,null,4)); +; (async () => { + await initData(path_root); + fs.writeFileSync(path.join(path_root, 'data.json'), JSON.stringify(data, null, 4)); -var html_str = fs.readFileSync(path.join(path_root,'template.html'), 'utf8'); -var UL_str = ''; -var svg_total = 0; -for(var a in data){ - UL_str += '