-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateFrontend.js
37 lines (31 loc) · 1.69 KB
/
createFrontend.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
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
const targetFile = 'src/main/resources/META-INF/resources/frontend/data-generator.ts';
const template = 'data-generator.tpl.ts';
const inputFilePath = 'src/main/resources/com/vaadin/exampledata';
let generatorRandomOptions = '';
let generatorRandomOptionGenerators = '';
fs.readdirSync(inputFilePath).filter((file) => fs.statSync(inputFilePath + '/' + file).isFile()).forEach((file) => {
const textFile = file.endsWith('.txt');
const optionName = file.replace('.txt', '');
const options = fs.readFileSync(inputFilePath + '/' + file, { encoding: 'UTF-8' });
const optionsArray = JSON.stringify(options.split('\n').filter((value) => value.trim().length > 0));
// options.FirstName = [...];
generatorRandomOptions += `options["${optionName}"] = ${optionsArray};\n\n`;
// [DataType.CompanyName]: { createValue: () => random(options.companyName) },
if (textFile && optionName != 'FoodProducts') {
generatorRandomOptionGenerators += ` [DataType.${optionName}]: { createValue: (seed, _refTime) => random(options.${optionName}, seed)},\n`;
}
});
let contents = fs.readFileSync(template, { encoding: 'UTF-8' });
contents = contents.replace(' /* Generator: randomOptionGenerators */', generatorRandomOptionGenerators);
contents = contents.replace('/* Generator: randomOptions */', generatorRandomOptions);
fs.mkdirSync(path.dirname(targetFile), { recursive: true });
prettier.resolveConfigFile().then((filePath) => {
prettier.resolveConfig(filePath).then((options) => {
options.parser = 'typescript';
contents = prettier.format(contents, options);
fs.writeFileSync(targetFile, contents);
});
});