-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgenerate-constants.js
193 lines (158 loc) · 5.09 KB
/
generate-constants.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
'use strict';
const { readFile, writeFile } = require('fs').promises;
function parseConstants(constantsSource) {
const parse = (line, idx) => {
if (line.length === 0) {
return { isWhitespace: true };
}
const commentMatch = /^\s*\/\/ (?<comment>.*)$/.exec(line);
if (commentMatch && commentMatch.groups.comment) {
return { isComment: true, comment: commentMatch.groups.comment };
}
const constantMatch = /^(?<label>.*)=(?<literal>.*)$/.exec(line);
if (
constantMatch &&
constantMatch.groups.label &&
constantMatch.groups.literal
) {
return {
isConstant: true,
label: constantMatch.groups.label,
literal: constantMatch.groups.literal,
};
}
throw new Error(`Constants source malformed on line: ${idx}.`);
};
const denotedConstants = constantsSource.split('\n').map(parse);
if (
denotedConstants.length > 0 &&
denotedConstants[denotedConstants.length - 1].isWhitespace
) {
denotedConstants.pop();
}
return denotedConstants;
}
function parseTemplate(templateSource) {
const parse = (line) => {
if (line.length === 0) {
return { isWhitespace: true };
}
const commentMatch = /{{COMMENT}}/.exec(line);
if (commentMatch) {
return { isComment: true, content: line };
}
const labelMatch = /{{LABEL}}/.exec(line);
const literalMatch = /{{LITERAL}}/.exec(line);
if (labelMatch && literalMatch) {
return { isConstant: true, content: line };
} else if (labelMatch || literalMatch) {
throw new Error(
'Template file malformed. \
Constant template must have a {{LABEL}} and {{LITERAL}}.'
);
}
return { content: line };
};
const denotedTemplate = templateSource.split('\n').map(parse);
const commentTemplates = denotedTemplate.reduce(
(acc, { isComment, content }, idx) =>
isComment ? [...acc, { content, idx }] : acc,
[]
);
if (commentTemplates.length !== 1) {
throw new Error(
`Template file malformed. Incorrect number of lines \
(${commentTemplates.length}) describing "comment" template. \
Should be (1).`
);
}
const commentTemplateIdx = commentTemplates[0].idx;
const constantTemplates = denotedTemplate.reduce(
(acc, { isConstant, content }, idx) =>
isConstant ? [...acc, { content, idx }] : acc,
[]
);
if (constantTemplates.length !== 1) {
throw new Error(
`Template file malformed. Incorrect number of lines \
(${constantTemplates.length}) describing "constant" template. \
Should be (1).`
);
}
const constantTemplateIdx = constantTemplates[0].idx;
if (commentTemplateIdx + 1 !== constantTemplateIdx) {
throw new Error(
'Template file malformed. \
Comment template and constant template must be sequential.'
);
}
denotedTemplate.splice(commentTemplateIdx, 2, {
isBody: true,
});
return {
denotedTemplate,
constantTemplate: constantTemplates[0].content,
commentTemplate: commentTemplates[0].content,
};
}
function merge(
denotedConstants,
denotedTemplate,
commentTemplate,
constantTemplate
) {
const mapConstant = (constant) => {
if (constant.isWhitespace) {
return '';
}
if (constant.isComment) {
return String(commentTemplate).replace(/{{COMMENT}}/g, constant.comment);
}
if (constant.isConstant) {
return String(constantTemplate)
.replace(/{{LABEL}}/g, constant.label)
.replace(/{{LITERAL}}/g, constant.literal);
}
};
const mapTemplate = (templateLine) => {
if (templateLine.isWhitespace) {
return '';
}
if (templateLine.isBody) {
return denotedConstants.map(mapConstant);
}
return templateLine.content;
};
return denotedTemplate.flatMap(mapTemplate);
}
async function transform(constantsPath, templatePath, outputPath) {
const constantsSource = (await readFile(constantsPath)).toString('utf-8');
const templateSource = (await readFile(templatePath)).toString('utf-8');
const denotedConstants = parseConstants(constantsSource);
const { denotedTemplate, commentTemplate, constantTemplate } =
parseTemplate(templateSource);
const mergedConstants = merge(
denotedConstants,
denotedTemplate,
commentTemplate,
constantTemplate
);
await writeFile(outputPath, mergedConstants.join('\n'));
}
async function main() {
const constantsPath = './constants/constants.src';
// Transform TS files
const tsTemplatePath = './constants/constants.typescript.template';
const tsOutputPath = './src/constants.ts';
await transform(constantsPath, tsTemplatePath, tsOutputPath);
// Transform Java files
const javaTemplatePath = './constants/constants.java.template';
const javaOutputPath =
'./android/src/main/java/com/twiliovoicereactnative/CommonConstants.java';
await transform(constantsPath, javaTemplatePath, javaOutputPath);
// Transform ObjC files
const objcTemplatePath = './constants/constants.objc.template';
const objcOutputPath = './ios/TwilioVoiceReactNativeConstants.h';
await transform(constantsPath, objcTemplatePath, objcOutputPath);
}
main();