-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-schema.js
138 lines (127 loc) · 3.89 KB
/
convert-schema.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const fs = require('fs');
const path = require('path');
/**
* JSON Schema から編集項目データへ変換
*/
+function (
/** 書式バージョン @type{string} */
version,
) {
/** プロジェクトルートのパス*/
const rootPath = __dirname;
// JSON Schema の読み込み
const jsonSchemas = (dirPath => {
const pathList = fs.readdirSync(dirPath)
.filter(filename => filename.endsWith('.json'))
.map(filename => path.join(dirPath, filename));
const result = [];
pathList.forEach(filePath => {
const file = fs.readFileSync(filePath).toString();
if (file.includes('"properties": {')) {
const obj = JSON.parse(file);
result.push({
name: path.basename(filePath).replace('.json', ''),
description: obj['description'],
schema: obj['properties'],
});
}
});
return result;
})(path.join(rootPath, 'schemas', 'json-schema'));
// 配列型のデフォルト値の読み込み
const arrayDefaults = (filePath => {
const file = fs.readFileSync(filePath).toString();
return file ? JSON.parse(file) : {};
})(path.join(rootPath, 'schemas', 'defaults.json'));
// 入力の書式データへ変換
const groups = [];
const fxGetType = (prop) => {
return prop['type']
?? prop['$ref']
?? (prop['anyOf'] ? 'anyOf' : undefined);
};
for (const domain of jsonSchemas) {
const list = [];
for (const [k, v] of Object.entries(domain.schema)) {
const isArray = v['type'] === 'array';
const type = isArray ? fxGetType(v['items']) : fxGetType(v);
const item = {
isArray: isArray,
key: k,
label: v['description'],
value: isArray ? arrayDefaults[k] : [v['default']],
};
switch (type) {
case 'anyOf': {
const options = isArray ? v['items']['anyOf'] : v['anyOf'];
if(options[0]['type'] === 'number') {
item['inputFormat'] = 'select_int';
} else {
item['inputFormat'] = 'select_text';
}
item['options'] = options.map(x => x['const']);
break;
}
case 'boolean':
item['inputFormat'] = 'checkbox';
break;
case 'integer':
item['inputFormat'] = 'number';
item['min'] = v['minimum'];
item['max'] = v['maximum'];
break;
case 'number':
item['inputFormat'] = 'double';
item['pattern'] = /^\d+\.\d+$/.source;
break;
case 'string':
switch (v['format']) {
case 'uri':
item['inputFormat'] = 'url';
break;
default:
item['inputFormat'] = 'text';
}
break;
case '#/$defs/argb':
item['inputFormat'] = 'color_alpha';
item['pattern'] = /^#[\dA-F]{8}$/.source;
break;
case '#/$defs/latitude':
item['inputFormat'] = 'number';
item['min'] = -90.0;
item['max'] = 90.0;
item['step'] = 0.000001;
break;
case '#/$defs/longitude':
item['inputFormat'] = 'number';
item['min'] = -180.0;
item['max'] = 180.0;
item['step'] = 0.000001;
break;
case '#/$defs/rgb':
item['inputFormat'] = 'color';
break;
default:
throw Error(`${k}: Undefined type!`);
}
list.push(item);
}
groups.push({
name: domain.name,
description: domain.description,
items: list,
});
}
// 入力書式データのファイル出力
const template = fs.readFileSync(path.join(rootPath, `convert-schema.template.ts`)).toString()
.replace('%%VERSION%%', version)
.replace('[/** Data */]', JSON.stringify(groups, null, 4));
fs.writeFileSync(
path.join(rootPath, 'src', 'schema.ts'),
template,
{ encoding: 'utf-8' },
);
}(
process.argv[2],
);