-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-elements.js
106 lines (105 loc) · 3.51 KB
/
generate-elements.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require('fs');
const FeMapper = {
boolean: "BooleanElement",
date: "DateElement",
display: "DisplayElement",
string: "InputElement",
int: "NumberElement",
float: "NumberElement",
encrypted: "PasswordElement",
text: "TextElement",
smart_text: "SmartTextElement"
}
let allInputs = {};
let dir = fs.readdirSync('./schema');
dir.forEach((file) => {
let data = fs.readFileSync(`./schema/${file}`);
let schema = JSON.parse(data);
for (let key in schema.inputs) {
// For embedded objects
allInputs[schema.inputs[key].name] = schema.modelName;
}
});
fs.readdir('./schema', function (err, files) {
files.forEach((file) => {
let requiredImports = {};
fs.readFile(`./schema/${file}`, (err, data) => {
let finalOutput = ``;
let schema = JSON.parse(data);
let inputs = schema.inputs;
if (inputs) {
for (let key in inputs) {
let currentInput = inputs[key];
finalOutput += `
@Injectable({
providedIn: 'root'
})
export class ${currentInput.name} {
public elements = {`;
if (currentInput.requiredFields) {
finalOutput += setFields(schema, currentInput.requiredFields, true, requiredImports);
}
if (currentInput.optionalFields) {
finalOutput += setFields(schema, currentInput.optionalFields, false, requiredImports);
}
finalOutput += `
}
}`;
}
}
let importsOutput = `// Auto generated file do not edit.
`;
importsOutput += `import {Injectable} from '@angular/core';
`;
importsOutput += `import {`
Object.keys(requiredImports).forEach((name) => {
importsOutput += `
${name},`
});
importsOutput += `} from '@neftx/generic-form';
`
fs.writeFile(`./src/app/elements-generated/${schema.modelName}.ts`, importsOutput + finalOutput, function (err) {
if (err)
console.error(err);
});
});
});
});
const setFields = (schema, fields, required, requiredImports) => {
let output = ``;
let fieldType = '';
fields.forEach((field, index) => {
let currentField = schema.fields[field];
if (currentField && currentField.fieldType != "association") {
fieldType = FeMapper[currentField.fieldType] ? FeMapper[currentField.fieldType] : "InputElement";
if (currentField.ngSelectArray || currentField.isReference) {
fieldType = "NgSelectElement";
requiredImports["NgSelectElement"] = true;
} else {
requiredImports[fieldType] = true;
}
let options = ``;
if (currentField.ngSelectArray) {
options += `,{
async: ${currentField.ngSelectArray.async ? currentField.ngSelectArray.async : false},
multiple: ${currentField.ngSelectArray.multiple ? currentField.ngSelectArray.multiple : false},
tag: ${currentField.ngSelectArray.tag ? currentField.ngSelectArray.tag : false},
searchable: ${currentField.ngSelectArray.searchable ? currentField.ngSelectArray.searchable : false},
options: [${currentField.ngSelectArray.options ? currentField.ngSelectArray.options.toString() : []}]
}`
}
output += `
${field} : new ${fieldType}({
key: "${field}",
label: "${currentField.label}",
placeHolder: "${currentField.placeHolder}",
defaultValue: ${currentField.defaultValue ? currentField.defaultValue : null},
required: ${required},
helpText: "${currentField.helpText ? currentField.helpText : ""}"
}`
output += `
${currentField.ngSelectArray ? options : ''}),`
}
});
return output;
}