-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
101 lines (88 loc) · 3.37 KB
/
build.js
File metadata and controls
101 lines (88 loc) · 3.37 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const path = require('path');
const del = require('del');
const webpack = require('webpack');
const webpackConfig = require('../webpack.config');
const fs = require('fs');
const apiPathFile = path.join(__dirname, './server/apiPath.js');
const feApiPath = path.join(__dirname, './fe/lib/api');
del.sync(path.resolve('build'));
del.sync(apiPathFile);
del.sync(feApiPath);
//读取api接口路径,写入文件
var writeToApiPath = function(){
var apiRootPath = path.join(__dirname, './server/api');
var files = fs.readdirSync(apiRootPath);
fs.appendFileSync(apiPathFile, "module.exports = function(app){\n");
files.forEach(function (filename) {
var fullname = path.join(apiRootPath, filename);
var stats = fs.statSync(fullname);
if (stats.isDirectory()){
fs.appendFileSync(apiPathFile, " app.use('/api/" + filename + "', require('./api/" + filename + "'));\n");
}
});
fs.appendFileSync(apiPathFile, "}");
}
var getArrByPattern = function(codeStr, pattern){
var arr = codeStr.match(pattern);
var newArr = [];
if(arr) {
for( let i = 0 ; i < arr.length; i++){
var funcName = arr[i].split(' ')[1];
newArr.push(funcName);
}
}
return newArr;
}
var writeApiFuncFile = function(filePath, funcName, funcType, funcUrl){
fs.appendFileSync(filePath, " " + funcName + "(data,cb) {\n $.ajax({\n url: '" + funcUrl + "',\n type: '" + funcType + "',\n data: data,\n cache: false\n }).done(data => {\n cb(data)\n })\n },\n\n");
}
//读取后端接口生成前端调用的函数文件
var generateFeApiFuncFile = function(){
var apiRootPath = path.join(__dirname, './server/api');
var files = fs.readdirSync(apiRootPath);
fs.mkdirSync(feApiPath);
files.forEach(function (filename) {
var fullname = path.join(apiRootPath, filename);
var stats = fs.statSync(fullname);
if (stats.isDirectory()){
var indexFile = path.join(fullname, 'index.js');
var codeStr = fs.readFileSync(indexFile, 'utf8');
var funcNameArr = getArrByPattern(codeStr, /apiName: (.*)/g);
var funcTypeArr = getArrByPattern(codeStr, /apiFuncType: (.*)/g);
var funcUrlArr = getArrByPattern(codeStr, /apiFuncUrl: (.*)/g);
if(funcNameArr.length != funcTypeArr.length || funcNameArr.length != funcUrlArr.length){
throw new Error("funcNameArr cannot match funcTypeArr length or funcUrlArr length");
}
if(funcNameArr.length > 0) {
var filePath = path.join(__dirname, './fe/lib/api', filename + '.js');
fs.appendFileSync(filePath, "export default {\n");
for (let i = 0; i < funcNameArr.length; i++) {
writeApiFuncFile(filePath, funcNameArr[i], funcTypeArr[i], funcUrlArr[i]);
}
fs.appendFileSync(filePath, "}");
}
}
});
}
generateFeApiFuncFile();
writeToApiPath();
webpack(webpackConfig.fe, function(err, stats) {
if (err) {
// Handle errors here
throw new Error(JSON.stringify(err.errors));
}else if(stats.hasErrors()){
throw new Error(stats.compilation.errors);
}
require('./runGulp')(function done() {
console.log('server webpack completely...');
});
});
webpack(webpackConfig.server, function(err, stats) {
if (err) {
// Handle errors here
throw new Error(JSON.stringify(err.errors));
}else if(stats.hasErrors()){
throw new Error(stats.compilation.errors);
}
console.log('server webpack completely...');
});