-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
83 lines (63 loc) · 2.48 KB
/
build.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
import fs from 'fs';
import path from 'path'
async function Build() {
const conf = await import(path.join(process.cwd(), 'micro.conf.js') );
for(const microservice of conf.default.microservices) {
const folderPath = path.join(process.cwd(), microservice.actionsPath);
const files = fs.readdirSync(folderPath);
const actionsImport = []
const actionsAction = []
const folderPath2 = path.join(process.cwd(), microservice.paramsPath);
const files2 = fs.readdirSync(folderPath2);
const paramsImport = []
const paramsParam = []
for(const file of files2 ) {
const filePath = path.join(folderPath2, file);
const fileStats = fs.statSync(filePath);
if (fileStats.isFile() && path.extname(file) === '.js') {
const fileName = path.basename(file, '.js');
paramsImport.push(`import param_${fileName} from './${microservice.paramsPath}/${file}'`);
paramsParam.push(` ${fileName}: param_${fileName} `)
}
}
for(const file of files ) {
const filePath = path.join(folderPath, file);
const fileStats = fs.statSync(filePath);
if (fileStats.isFile() && path.extname(file) === '.js') {
const fileName = path.basename(file, '.js');
actionsImport.push(`import action_${fileName} from './${microservice.actionsPath}/${file}'`)
const f = await import(`${filePath}`);
const args = getArguments(f.default.toString()).map(arg => `"${arg}"`);
args.push('action_' + fileName);
actionsAction.push(` ${fileName}: [${args.join(', ')}] `)
}
}
const str = `/*
*
* Este archivo ha sido generado automáticamente NO MOVER
* ${ (new Date).toISOString() }
*/
${actionsImport.join("\n")}
${paramsImport.join("\n")}
const actions = {
${actionsAction.join(',\n')}
};
const params = {
${paramsParam.join(',\n')}
};
export default {actions, params}
`;
fs.writeFileSync( path.join(`micro.df.${microservice.name}.js`), str);
}
}
Build();
function getArguments(functionString) {
var argumentPattern = /(?:\s*function\s*[^\(]*)?\s*\(\s*([^)]*)\)/;
var match = argumentPattern.exec(functionString);
if (match && match[1]) {
return match[1].split(',').map(function(arg) {
return arg.trim();
});
}
return [];
}