-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
93 lines (80 loc) · 1.97 KB
/
generate.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
const clipboardy = require('clipboardy');
const base = require('./data/base');
const svg = require('./data/svg');
const aria = require('./data/aria');
const events = require('./data/events');
const args = process.argv.slice(2).map(a => a.split('=')).reduce((acc, cur) => {
acc[cur[0]] = cur[1];
return acc;
}, {});
const generic = (data, callback) => {
let result = '';
data.forEach(d => {
result += callback(d) + '\n'
});
clipboardy.writeSync(result);
}
const iTag = (d) => {
return `${d}(inner?: Tag): Tag;`
}
const tag = (d) => {
return `Tag.prototype.${d} = function(inner?: Tag): Tag { return this.element('${d}', inner) };`
}
function kebabToCamel(s) {
return s.replace(/([-_][a-z])/ig, ($1) => {
return $1.toUpperCase().replace('-', '');
});
};
const iAttr = (d) => {
return `${kebabToCamel(d)}(value: string): Tag;`
}
const attr = (d) => {
return `Tag.prototype.${kebabToCamel(d)} = function(value: string): Tag { return this.attribute('${d}', value); }`
}
const iEl = (d) => `${d}: HaipaElement;`;
const el = (d) => `@Element() ${d}() {}`;
const iAt = (d) => `${kebabToCamel(d)}: HaipaAttribute;`;
const at = (d) => `@Attribute(${d.includes('-') ? "'" + d + "'" : ''}) ${kebabToCamel(d)}() {}`;
const ariaGen = (d) => `@Attribute('${d}') ${kebabToCamel(d)}() {}`
const renderers = {
'interface': {
'tags': iEl,
'attr': iAt,
},
'func': {
'tags': el,
'attr': at,
}
};
const chooseData = (set, key) => {
const allData = {
base: base,
aria: aria,
svg: svg,
events: events,
all: {
tags: [
...base.tags,
...svg.tags
],
attr: [
...base.attr,
...svg.tags,
...aria.attr,
...events.attr
]
}
};
return allData[key][set];
}
const start = () => {
const type = args.type ? args.type : 'func';
const set = args.set ? args.set : 'tags';
const data = args.data
? chooseData(set, args.data)
: chooseData(set, 'all')
const renderer = renderers[type][set];
console.log(type, set, args.data);
generic(data, renderer);
};
start();