/
breakUpAriaJSON.js
executable file
·314 lines (289 loc) · 8.65 KB
/
breakUpAriaJSON.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env node --harmony
const fs = require('fs');
const path = require('path');
const baseDir = 'src/etc/roles';
const literalRequires = [];
const abstractRequires = [];
const dpubRequires = [];
function createCamelName (name) {
const nameArr = name.split('-');
if (nameArr.length > 1) {
nameArr[1] = nameArr[1].slice(0, 1).toUpperCase()
+ nameArr[1].slice(1);
}
return nameArr.join('') + 'Role';
}
function createKeyName (key) {
if (key.includes('-')) {
return `'${key}'`;
} else {
return key;
}
}
function getTypeof(value) {
return Array.isArray(value)
? 'array'
: (value == null)
? 'null'
: typeof value;
}
function stringifyBoolean(value) {
return [(value) ? 'true' : 'false'];
}
function stringifyArray(arr, depth) {
const output = [];
if (arr.length === 0) {
output.push('[]');
} else {
output.push('[');
let tmp = [];
(new Set(arr)).forEach(
value => {
if (value != null) {
tmp.push(`${constructIndent(depth)}${triageValue(value, (depth))},`);
}
}
);
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)}]`);
}
return output.join('\n');
}
function stringifyObject(value, depth) {
let output = [];
const keys = Object.keys(value);
if (keys.length === 0) {
output.push('{}');
} else {
output.push(`{`);
const tmp = [];
keys.forEach(key => {
tmp.push(`${constructIndent(depth)}${createKeyName(key)}: ${triageValue(value[key], (depth))},`);
});
output.push(tmp.join('\n'));
output.push(`${constructIndent(depth - 1)}}`);
}
return output.join('\n');
}
function constructIndent(depth) {
return Array(depth).fill(' ').join('');
}
function triageValue(value, depth = 0) {
let output = [];
switch(getTypeof(value)) {
case 'object':
output = output.concat(stringifyObject(value, (depth + 1)));
break;
case 'array':
output = output.concat(stringifyArray(value, (depth + 1)));
break;
case 'boolean':
output = output.concat(stringifyBoolean(value, (depth + 1)));
break;
case 'string':
output = output.concat(`\'${value}\'`);
break;
default:
output.push('null');
}
return output;
}
fs.readFile(path.join('scripts/roles.json'), {
encoding: 'utf8'
}, (error, data) => {
if (error) {
console.error(error);
process.exit();
}
let aria = JSON.parse(data);
function superClassWalker (klasses, stack = []) {
let output = [];
for (let klass of klasses) {
const newStack = stack.slice();
newStack.unshift(klass);
let superClasses = aria[klass]['superClass'];
if (superClasses.length > 0) {
output = output.concat(
superClassWalker(superClasses, newStack)
);
} else {
output.push(newStack);
}
}
return output;
}
// Get a map of superClasses
const accumulatedSuperClasses = new Map([]);
Object.keys(aria)
.forEach((name) => {
// Create a set of all the props of the super classes.
let superClasses = aria[name]['superClass'];
const accumulation = superClassWalker(superClasses);
const output = accumulatedSuperClasses.set(name, accumulation);
// console.log(`${name} => ${accumulatedSuperClasses.get(name).map(group => `[${group}]`).join('\n')}`);
});
Object.keys(aria)
.forEach((name) => {
const camelName = createCamelName(name);
// Deprecate requireContextRole prop. Replace with requiredContextRole.
// requireContextRole will be removed in a future version.
aria[name]['requireContextRole'] = aria[name]['requiredContextRole'];
const file = [
'/**',
' * @flow',
' */',
`const ${camelName}: ARIARoleDefinition = {`,
Object.keys(aria[name])
.sort()
.filter((prop) => {
// Create a set of all the props of the super classes.
if (prop === 'props') {
const inheritedProps = new Set([]);
let superClasses = aria[name]['superClass'];
while (superClasses.length > 0) {
const cloned = superClasses.slice();
superClasses = [];
cloned.forEach(superClass => {
aria[superClass]['props'].forEach(
superProp => {
const name = (Array.isArray(superProp))
? superProp[0]
: superProp;
inheritedProps.add(name);
}
);
superClasses = superClasses.concat(aria[superClass]['superClass']);
});
}
// Remove the props that exist in the super classes unless the
// prop has a default value.
aria[name]['props'] = aria[name]['props'].filter(
prop => {
let keep = true;
if (
!Array.isArray(prop)
&& inheritedProps.has(prop)
) {
keep = false;
}
return keep;
});
}
// Always returns true. Using filter simply to chain.
return true;
})
.map((prop) => {
let value = aria[name][prop];
if (['props', 'requiredProps'].includes(prop)) {
value = value.reduce((acc, item) => {
let name;
let defaultVal;
if (Array.isArray(item)) {
name = item[0];
defaultVal = item[1];
} else {
name = item;
defaultVal = null;
}
acc[name] = defaultVal;
return acc;
}, {});
}
if (prop === 'superClass') {
value = accumulatedSuperClasses.get(name);
}
let depth = 1;
return `${constructIndent(depth)}${createKeyName(prop)}: ${triageValue(value, depth).join('\n')},`;
}).join('\n'),
'};',
'',
`export default ${camelName};`,
];
let subDir = '';
if (aria[name].abstract) {
subDir = 'abstract';
abstractRequires.push(camelName);
} else if (name.indexOf('doc-') === 0) {
subDir = 'dpub';
dpubRequires.push(camelName);
} else {
subDir = 'literal';
literalRequires.push(camelName);
}
fs.mkdir(path.join(baseDir, subDir), function () {
fs.writeFile(
path.join(baseDir, subDir, `${camelName}.js`),
file.join('\n'),
{
encoding: 'utf8'
},
function (err) {
if (err) throw err;
console.log(`Created file ${baseDir}/${subDir}/${camelName}.js`);
});
});
});
});
function requiresMapper (roles, path, depth) {
return roles.map(role => {
return `${constructIndent(depth)}import ${role[1]} from \'./${path}/${role[1]}\';`;
}).join('\n');
}
function requiresCombiner(roles, depth) {
return roles.map(role => {
return `${constructIndent(depth)}[\'${role[0]}\', ${role[1]}]`;
}).join(',\n');
}
const srcDir = 'src';
fs.readFile(path.join('scripts/roles.json'), {
encoding: 'utf8'
}, (error, data) => {
if (error) {
console.error(error);
process.exit();
}
const literalRequires = [];
const abstractRequires = [];
const dpubRequires = [];
let aria = JSON.parse(data);
Object.keys(aria)
.forEach((name) => {
const camelName = createCamelName(name);
if (aria[name].abstract) {
subDir = 'abstract';
abstractRequires.push([name, camelName]);
} else if (name.indexOf('doc-') === 0) {
subDir = 'dpub';
dpubRequires.push([name, camelName]);
} else {
literalRequires.push([name, camelName]);
}
});
// Create a rollup maps.
[
[literalRequires, 'ariaLiteralRoles', 'literal'],
[abstractRequires, 'ariaAbstractRoles', 'abstract'],
[dpubRequires, 'ariaDpubRoles', 'dpub'],
].forEach(requires => {
fs.writeFile(
path.join(baseDir, `${requires[1]}.js`),
'/**\n'
+ ' * @flow\n'
+ ' */\n'
+ `${requiresMapper(requires[0], path.join(requires[2]), 0)}\n`
+ `\n`
+ `const ${requires[1]}: MapOfRoleDefinitions = new Map([\n`
+ `${requiresCombiner(requires[0], 1)}\n`
+ `]);\n`
+ `\n`
+ `export default ${requires[1]};`,
{
encoding: 'ascii'
},
function (err) {
if (err) throw err;
console.log(`Created file ${baseDir}/${requires[1]}.js`);
}
);
});
});