Skip to content

Commit

Permalink
Update build script.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 27, 2019
1 parent 9566c02 commit b0ed323
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions build.js
Original file line number Diff line number Diff line change
@@ -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 += ' <h2 class="title">'+a+'</h2>\n';
UL_str += ' <ul>\n';
if(data[a].length>0){
svg_total += data[a].length;
for (var i = 0; i < data[a].length; i++) {
UL_str += ' <li><img src="'+data[a][i]+'" /></li>\n'
}
}
UL_str += ' </ul>\n';
}
console.log('\n => 共有' + svg_total + '个SVG文件');
let ulStr = '';
let svgTotal = 0;

fs.writeFileSync(path.join(path_root, 'index.html'), html_str.replace('{{content}}',UL_str));
console.log("\n写入文件ok!!\n");
Object.keys(data).forEach((keyName) => {
ulStr += ` <h2 class="title">${keyName}</h2>\n`;
ulStr += ' <ul>\n';
if (data[keyName].length > 0) {
svgTotal += data[keyName].length;
data[keyName].forEach((item) => {
ulStr += ` <li><img src="${item}" /></li>\n`;
});
}
ulStr += ' </ul>\n';
});
console.log('\n => 共有' + svgTotal + '个SVG文件');
const htmlStr = fs.readFileSync(path.join(path_root, 'template.html'), 'utf8');
fs.writeFileSync(path.join(path_root, 'index.html'), htmlStr.replace('{{content}}', ulStr));
console.log("\n写入文件ok!!\n");
})();

0 comments on commit b0ed323

Please sign in to comment.