-
Notifications
You must be signed in to change notification settings - Fork 5
/
builder.js
263 lines (220 loc) · 6.34 KB
/
builder.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
/**
* Written by Denis Power.
*
* globalconfig.json options.
*
* {
* files: [],
* dir: "./core",
* types: "./types"
* }
*
*
*
*/
const { writeFileSync, readFileSync, readdirSync } = require("fs");
const { format } = require("prettier");
const config = getConfig();
const Package = JSON.parse(readFileSync("./package.json"));
const packageLock = JSON.parse(readFileSync("./package-lock.json"));
const buildVersion = process.argv[2];
const buildYear = process.argv[3];
const buildType = process.argv[4];
const typeVersion = process.argv[5];
const isGlobalBuild = buildType.toUpperCase() == "GLOBAL";
const isModuleBuild = buildType.toUpperCase() == "MODULE";
const isTsDeclaration = buildType.toUpperCase() == "TYPES";
class Inter {
#source;
#token;
#peaceOfCode;
#parsingExport = false;
#parsingImport = false;
#reStartTheLoop = false;
#importConfig = {
body: "",
};
#exportConfig = {
name: void 0,
Default: false,
};
#removeExportDeclaration = () => {
const { name, Default } = this.#exportConfig;
const pattern1 = new RegExp(`export\\s*${name}`, "g");
const pattern2 = new RegExp(`export(?:\\s*)default(?:\\s*)${name}`, "g");
const pattern = Default ? pattern2 : pattern1;
this.#source = this.#source.replace(pattern, name);
this.#parsingExport = false;
this.#reStartTheLoop = true;
};
#removeImportDeclaration = () => {
const { body } = this.#importConfig;
this.#source = this.#source.replace(body, "");
this.#importConfig.body = "";
this.#parsingImport = false;
this.#reStartTheLoop = true;
};
constructor(codeString) {
this.#source = codeString;
}
removeImport() {}
removeExportAndImportDeclaration(module) {
for (let i = 0; i < this.#source.length; i++) {
if (this.#reStartTheLoop) {
this.#reStartTheLoop = false;
i = 0;
}
this.#peaceOfCode = this.#source[i];
if (!isBlankSpace(this.#peaceOfCode) && !this.#parsingImport)
this.#token += this.#peaceOfCode;
else if (this.#parsingImport) {
this.#importConfig.body += this.#peaceOfCode;
if (this.#importConfig.body.endsWith(";")) {
this.#removeImportDeclaration();
}
} else if (isBlankSpace(this.#peaceOfCode) && this.#token) {
if (
this.#token == "import" &&
!this.#parsingImport &&
!this.#parsingExport
) {
this.#parsingImport = true;
this.#importConfig.body += "import ";
} else if (
this.#token == "export" &&
!this.#parsingExport &&
!this.#parsingImport &&
module !== true
) {
this.#parsingExport = true;
} else if (this.#parsingExport) {
const { name } = this.#exportConfig;
if (!name) {
if (this.#token !== "default") {
this.#exportConfig.name = this.#token;
this.#removeExportDeclaration();
} else this.#exportConfig.Default = true;
}
}
this.#token = "";
}
}
return this.#source;
}
}
function isBlankSpace(code) {
return /\s/.test(code);
}
function getConfig() {
const resultObject = {
hasFile: false,
file: void 0,
};
try {
resultObject.file = JSON.parse(readFileSync("./builderconfig.json"));
resultObject.hasFile = true;
} catch (e) {
/*No globalconfig.json*/
}
return resultObject;
}
if (!config.hasFile) throw new Error("No `globalconfig.json` file found");
function buildTsDeclaration() {
const { types } = config.file;
const files = readdirSync(types);
let body = "";
for (const file of files) {
const fileString = readFileSync(`${types}/${file}`);
body += fileString;
}
body = `
/***
* MIT LICENSED BY - Denis Power(Inter creator)
* Typescript declaration file for Inter version ${buildVersion}
* Version - ${typeVersion}
* Repo - https://github.com/interjs/inter/types
* GENERATED BY INTER BUILDER
*/
${body}
`;
writeFileSync("inter.m.d.ts", body);
}
function build() {
const { files, dir } = config.file;
let body = "";
for (const file of files) {
let fileString = readFileSync(dir ? `${dir}/${file}` : file);
/**
* In module build, the exports declaration won't be removed in regular
* files which contain the Inter core, but the export declaration from
* the helpers and errors files must be removed, that's why here we're
* considering them as special.
*/
const specialFiles = new Set([
"helpers.js",
"template/errors.js",
"renderList/errors.js",
"ajax/errors.js",
"ref/errors.js",
"renderif/errors.js",
"toattrs/errors.js",
]);
if (specialFiles.has(file) && isModuleBuild) {
let fileStringBody = "";
fileStringBody += fileString;
const frame = new Inter(fileStringBody);
fileString = frame.removeExportAndImportDeclaration();
}
body += fileString;
}
const frame = new Inter(body);
let builtCode;
if (isGlobalBuild) builtCode = frame.removeExportAndImportDeclaration();
else if (isModuleBuild)
builtCode = frame.removeExportAndImportDeclaration(true);
if (isGlobalBuild) {
body = `
/**
* Interjs
* Version - ${buildVersion}
* MIT LICENSED BY - Denis Power
* Repo - https://github.com/interjs/inter
* 2021 - ${buildYear}
* GENERATED BY INTER BUILDER
*
*/
(function () {
${builtCode};
window.Ref = Ref;
window.renderIf = renderIf;
window.renderList = renderList;
window.template = template;
window.toAttrs = toAttrs;
window.Backend = Backend;
console.log("The global version ${buildVersion} of Inter was loaded successfully.")
})();
`;
} else if (isModuleBuild) {
body = `
/**
* Interjs
* Version - ${buildVersion}
* MIT LICENSED BY - Denis Power
* Repo - https://github.com/interjs/inter
* 2021 - ${buildYear}
* GENERATED BY INTER BUILDER
* Module version
*/
export const interVersion = "${buildVersion}";
${builtCode}
`;
}
if (isGlobalBuild) writeFileSync("inter.js", format(body));
else if (isModuleBuild) writeFileSync("inter.m.js", format(body));
Package.version = buildVersion;
packageLock.version = buildVersion;
writeFileSync("package.json", JSON.stringify(Package));
writeFileSync("package-lock.json", JSON.stringify(packageLock));
}
if (isTsDeclaration) buildTsDeclaration();
else build();