forked from zhouyk-library/zhouyk-library.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (79 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require("fs")
const path = require("path")
const express = require('express')
const args = process.argv.splice(2)
const isBuild = args.includes('build') || args.length === 0;
const isServer = args.includes('server')
const modulesPath = "./modules"
const configFileName = "config"
function parseConfigJSON(i18nModules,paths) {
const configPath = paths + "/" + configFileName
var config = parseConfigPath(i18nModules,paths);
config.forEach(item => {
if (item.isP) {
item.child = parseConfigJSON(i18nModules,paths + "/" + item.key)
} else {
item.child = []
item.path = paths.substring(1, paths.length) + "/" + item.key
}
})
return config;
};
function parseConfigPath(i18nModules,dirPath) {
const config = []
if(fs.existsSync(dirPath) ) {
fs.readdirSync(dirPath).forEach(function(file) {
var curPath = dirPath + "/" + file;
if(fs.statSync(curPath).isDirectory()) {
config.push( {
"key": file,
"name": i18nModules[file],
"isP": true
})
} else {
const text = fs.readFileSync(curPath).toString()
var startIndex = text.indexOf('# ');
if(startIndex>-1){
var context = text.substring(startIndex+2, text.length);
var index = context.indexOf('\n');
index = index === -1 ? 0:index
var title = context.substring(0, index);
}
config.push({
"key": file,
"name": title
})
}
})
}
return config;
};
function writeJSON(jsonPath, jsonstr) {
fs.writeFile(jsonPath, jsonstr, function(err) {
if (err) {
console.error(err);
} else {
console.log('构建成功!');
}
});
}
if(isBuild || isServer){
var i18nModules = {}
const modulespath = path.join(__dirname, 'i18n')
if (fs.existsSync(modulespath)) {
fs.readdirSync(modulespath).forEach(function(file) {
const module = require(modulespath + '/' + file)
i18nModules = {... i18nModules, ... module}
})
}
const config = parseConfigJSON(i18nModules,modulesPath);
writeJSON('./js/config.js', "window.treeMenue=" + JSON.stringify(config))
}
if(isServer){
const app = express();
// 静态服务器
app.use(express.static(__dirname));
app.listen(8088, () => {
console.log('localhost:8088');
});
}