-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathgenerate.mjs
More file actions
239 lines (221 loc) · 10.4 KB
/
generate.mjs
File metadata and controls
239 lines (221 loc) · 10.4 KB
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
// NOTICE: This is a build script; do not try to import it.
// Generate ESM modules and modular TS definitions
// Dependency: Node.js
console.log("Generating ECMAScript modules using Node");
import { open, mkdir, access, constants } from 'node:fs/promises';
const AUTOGENERATED_NOTICE = "// NOTICE: This code is @generated from code outside the esm directory. Please do not edit it to contribute!\n\n";
// Main file
// code-input.mjs: (../code-input.js without the templates) + ESM
{
const codeInputJs = await open("../code-input.js", "r");
const codeInputMjs = await open("code-input.mjs", "w");
await codeInputMjs.writeFile(AUTOGENERATED_NOTICE);
// Imports: Nothing
// Miss out templates
let copyingCode = true;
for await (const line of codeInputJs.readLines()) {
if(line.includes("ESM-SUPPORT-START-TEMPLATES-BLOCK-1") || line.includes("ESM-SUPPORT-START-TEMPLATES-BLOCK-2")) {
copyingCode = false;
}
if(copyingCode) {
await codeInputMjs.writeFile(line+"\n");
}
if(line.includes("ESM-SUPPORT-END-TEMPLATES-BLOCK-1") || line.includes("ESM-SUPPORT-END-TEMPLATES-BLOCK-2")) {
copyingCode = true; // After code to copy - this line missed out
}
}
// Exports
await codeInputMjs.writeFile("export const Plugin = codeInput.Plugin;\n");
await codeInputMjs.writeFile("export const Template = codeInput.Template;\n");
await codeInputMjs.writeFile("export const CodeInput = codeInput.CodeInput;\n");
await codeInputMjs.writeFile("export const registerTemplate = codeInput.registerTemplate;\n");
await codeInputMjs.writeFile("export default { Plugin, Template, CodeInput, registerTemplate };\n");
codeInputJs.close();
codeInputMjs.close();
}
// code-input.d.mts: (../code-input.d.ts without the plugin/template namespaces) + ESM
{
const codeInputDTs = await open("../code-input.d.ts", "r");
const codeInputDMts = await open("code-input.d.mts", "w");
await codeInputDMts.writeFile(AUTOGENERATED_NOTICE);
// Miss out no-ESM specific code at the top
// Code before first namespace, after no-ESM specific code
let copyingCode = false;
for await (const line of codeInputDTs.readLines()) {
if(line.includes("ESM-SUPPORT-START-NAMESPACE-1") || line.includes("ESM-SUPPORT-START-NAMESPACE-2")) {
copyingCode = false;
}
if(copyingCode) {
await codeInputDMts.writeFile(line+"\n");
}
if(line.includes("ESM-SUPPORT-END-NOESM-SPECIFIC") || line.includes("ESM-SUPPORT-END-NAMESPACE-1") || line.includes("ESM-SUPPORT-END-NAMESPACE-2")) {
copyingCode = true; // After is code to copy - this line missed out
}
}
await codeInputDMts.writeFile("export default { Plugin, Template, CodeInput, registerTemplate };\n");
await codeInputDTs.close();
await codeInputDMts.close();
}
// Templates
{
try {
await mkdir("templates");
} catch(error) {
if(error.code !== "EEXIST") {
// Ignore directory already existing; throw all other errors.
throw error;
}
}
let codeInputDTs = await open("../code-input.d.ts", "r");
// For each template name prepared for ESM support from the d.ts file:
const templateNames = [];
for await (const line of codeInputDTs.readLines()) {
const templatesOnThisLine = line.match(/(?<=ESM\-SUPPORT\-START\-TEMPLATE\-)[A-Za-z]+/g);
if(templatesOnThisLine !== null) {
for(const templateName of templatesOnThisLine) {
templateNames.push(templateName);
}
}
}
await codeInputDTs.close();
// templates/*.mjs
templateNames.forEach(async (templateName) => {
const codeInputJs = await open("../code-input.js", "r");
const templateMjs = await open("templates/"+templateName+".mjs", "w")
await templateMjs.writeFile(AUTOGENERATED_NOTICE);
// Imports
await templateMjs.writeFile("import { Template } from \"../code-input.mjs\";\n");
// Code after start and before end of this template, making use of the imported Template, not codeInput.Template
let copyingCode = false;
let templateClassName = null;
for await (const line of codeInputJs.readLines()) {
if(line.includes("ESM-SUPPORT-END-TEMPLATE-"+templateName)) {
break;
}
if(copyingCode) {
if(templateClassName === null) {
const templateClassNameThisLine = line.match(/(?<=class )[A-Za-z]+(?= extends codeInput.Template)/);
if(templateClassNameThisLine !== null && templateClassNameThisLine.length > 0) {
templateClassName = templateClassNameThisLine;
}
}
await templateMjs.writeFile(line.replaceAll("codeInput.Template", "Template")+"\n");
}
if(line.includes("ESM-SUPPORT-START-TEMPLATE-"+templateName)) {
copyingCode = true; // After is code to copy - this line missed out
}
}
// Export, assuming the name of the function is the same as the name of the file.
await templateMjs.writeFile("export default "+templateClassName+";\n");
await codeInputJs.close();
await templateMjs.close();
});
// templates/*.d.mts
templateNames.forEach(async (templateName) => {
const codeInputDTs = await open("../code-input.d.ts", "r");
const templateDMts = await open("templates/"+templateName+".d.mts", "w")
await templateDMts.writeFile(AUTOGENERATED_NOTICE);
// Imports
await templateDMts.writeFile("import { Template, Plugin } from \"../code-input.d.mts\";\n");
// Code after start and before end of this template, making use of the imported Template, not codeInput.Template
let copyingCode = false;
let classSeen = false;
for await (let line of codeInputDTs.readLines()) {
if(line.includes("ESM-SUPPORT-END-TEMPLATE-"+templateName)) {
break;
}
if(copyingCode) {
if(/( |\t)*class.*/.test(line) && !classSeen) {
// Replace only first occurrence
line = line.replace("class", "export default class");
classSeen = true;
}
await templateDMts.writeFile(line.replaceAll("codeInput.Template", "Template").replaceAll("codeInput.Plugin", "Plugin")+"\n");
}
if(line.includes("ESM-SUPPORT-START-TEMPLATE-"+templateName)) {
copyingCode = true; // After is code to copy - this line missed out
}
}
await codeInputDTs.close();
await templateDMts.close();
});
}
// Plugins
{
try {
await mkdir("plugins");
} catch(error) {
if(error.code !== "EEXIST") {
// Ignore directory already existing; throw all other errors.
throw error;
}
}
let codeInputDTs = await open("../code-input.d.ts", "r");
// For each plugin name prepared for ESM support from the d.ts file:
const pluginNames = [];
for await (const line of codeInputDTs.readLines()) {
const pluginsOnThisLine = line.match(/(?<=ESM\-SUPPORT\-START\-PLUGIN\-)[A-Za-z\-]+/g);
if(pluginsOnThisLine !== null) {
for(const templateName of pluginsOnThisLine) {
pluginNames.push(templateName);
}
}
}
await codeInputDTs.close();
// plugins/*.mjs
for(let i = 0; i < pluginNames.length; i++) {
const pluginName = pluginNames[i];
const pluginJs = await open("../plugins/"+pluginName+".js", "r");
const pluginMjs = await open("plugins/"+pluginName+".mjs", "w")
await pluginMjs.writeFile(AUTOGENERATED_NOTICE);
// Imports
await pluginMjs.writeFile("import { Plugin } from \"../code-input.mjs\";\n")
// Plugin syntax is to be stored in an object; do so temporarily.
await pluginMjs.writeFile("const plugins = {};\n");
// Code from this plugin"s file, making use of the imported Plugin, not codeInput.Plugin, and of the created plugins object, not codeInput.plugins
let pluginClassName = null;
for await (const line of pluginJs.readLines()) {
if(pluginClassName === null) {
const pluginClassNameThisLine = line.match(/(?<=codeInput\.plugins\.)[A-Za-z]+/);
if(pluginClassNameThisLine !== null && pluginClassNameThisLine.length > 0) {
pluginClassName = pluginClassNameThisLine;
}
}
await pluginMjs.writeFile(line.replaceAll("codeInput.Plugin", "Plugin").replaceAll("codeInput.plugins", "plugins")+"\n");
}
// Export, assuming the name of the function is the same as the name of the file.
await pluginMjs.writeFile("export default plugins."+pluginClassName+";\n");
await pluginJs.close();
await pluginMjs.close();
}
// plugins/*.d.mts
for(let i = 0; i < pluginNames.length; i++) {
const pluginName = pluginNames[i];
const codeInputDTs = await open("../code-input.d.ts", "r");
const pluginDMts = await open("plugins/"+pluginName+".d.mts", "w")
await pluginDMts.writeFile(AUTOGENERATED_NOTICE);
// Imports
await pluginDMts.writeFile("import { Plugin, CodeInput } from \"../code-input.d.mts\";\n");
// Code after start and before end of this plugin, making use of the imported Template, not codeInput.Template
let copyingCode = false;
let functionSeen = false;
for await (let line of codeInputDTs.readLines()) {
if(line.includes("ESM-SUPPORT-END-PLUGIN-"+pluginName)) {
break;
}
if(copyingCode) {
if(/( |\t)*class.*/.test(line) && !functionSeen) {
// Replace only first occurrence
line = line.replace("class", "export default class");
functionSeen = true;
}
await pluginDMts.writeFile(line.replaceAll("codeInput.Plugin", "Plugin").replaceAll("codeInput.CodeInput", "CodeInput")+"\n");
}
if(line.includes("ESM-SUPPORT-START-PLUGIN-"+pluginName)) {
copyingCode = true; // After is code to copy - this line missed out
}
}
await codeInputDTs.close();
await pluginDMts.close();
}
}