-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
422 lines (304 loc) · 11.6 KB
/
build.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
const { log, cls, getFilesSync, writeFile, concatFiles } = require("aftc-node-tools")
const { JSODoc } = require("jsodoc")
const fs = require("fs")
const p = require("path")
const path = require('path');
const fse = require("fs-extra")
const version = require("./package.json").version
cls();
log("# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #".green);
log("AFTC-MODULES: Build starting...".yellow);
// Get files to work on
let jsFiles = getFilesSync("./src", ".js", true);
let tsFiles = getFilesSync("./src", ".ts", true);
let dtsFiles = getDTSFiles("./src");
// log(jsFiles);
// log(tsFiles);
let importList = [];
let exportList = [];
let tsTypes = [];
start();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
async function start() {
console.clear();
// build index.js
await buildIndexJs();
log(`index.js built`.green);
await buildIndexTs();
log(`index.d.ts built`.green);
// Build concatinated bundle aftc-modules.js
let out = concatFiles(jsFiles);
await writeFile("./aftc-modules.js", out)
log("aftc-modules.js - generated")
cleanConcatinatedFile();
// Docs
// buildDocs()
log("\nAFTC-MODULES: Build complete!".yellow);
log("# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #".green);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function getDTSFiles(currentDir) {
let dtsFiles = [];
function findFiles(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
findFiles(fullPath); // Recursive call
} else if (file.endsWith('.d.ts')) {
dtsFiles.push(fullPath);
}
});
}
findFiles(currentDir);
return dtsFiles;
}
async function cleanConcatinatedFile() {
const f = p.resolve("./aftc-modules.js");
let first2Chars = "";
let first2CharFilterList = ["//", "*", "*/"];
let processLine = true;
let processLineDone = false;
let outputString = "";
let debugString = "";
const fileContents = await fs.readFileSync(f, 'utf-8');
fileContents.split(/\r?\n/).forEach(line => {
// reset
first2Chars = "";
processLine = true;
// clean up line
line = replaceTabs(line);
// log(line);
// check line doesn't start with any first2CharFilterList
// let a = first2CharFilterList.some(substring=>line.includes(substring));
first2Chars = line.substring(0, 2);
if (isArrayInString(first2Chars, first2CharFilterList) !== true) {
outputString += (line) + "\n";
} else {
debugString += line + "\n";
}
});
await writeFile("./aftc-modules.js", outputString)
// await writeFile("./aftc-modules-debug.js", debugString)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
async function buildIndexTs() {
let outputFile = p.resolve("./index.d.ts");
let fileContents = "";
let currentDirName = p.basename(process.cwd())
let pathStringToRemove = process.cwd() + "\\"; // process.cwd().replace(currentDirName, "")
// log(pathStringToRemove);
// let tsImportPath = "";
// log(tsImportPath);
// jsImportPath = replaceAll("\\", "/",jsImportPath)
// jsImportPath = jsImportPath.replace(/\\/g, '/')
let tsFilePath = "";
log(dtsFiles);
for (const f of dtsFiles) {
tsFilePath = "";
tsFilePath = f.replace(pathStringToRemove, "");
tsFilePath = ".\\" + tsFilePath;
tsFilePath = tsFilePath.replace(/\\/g, '/')
log(tsFilePath)
// /// <reference path="../src/array/arrayGetMax.d.ts"/>
fileContents += `/// <reference path="${tsFilePath}"/>` + "\n";
}
await fse.writeFile(outputFile, fileContents);
}
// - - - - - - - - - - - - - - - - - - - - - - - -
async function buildIndexJs() {
const outputFile = p.resolve("./index.js")
let fileContents = "";
let first2Chars = "";
let first2CharFilterList = ["//", "*", "*/"];
let exp = ""; // will hold the exportString to add to exportList
let functionName = ""; // will hold the function name
let currentDirName = p.basename(process.cwd())
let pathStringToRemove = "";
let jsImportPath = "";
let exportList = []; // functions / class's names to add to export section
jsFiles.sort();
for (const f of jsFiles) {
first2Chars = "";
exp = "";
functionName = "";
pathStringToRemove = process.cwd().replace(currentDirName, "")
jsImportPath = f.replace(pathStringToRemove, "")
jsImportPath = jsImportPath.replace(/\\/g, '/')
// log(jsImportPath);
if (jsImportPath.includes("does")) {
// log(jsImportPath);
}
const allFileContents = await fs.readFileSync(f, 'utf-8');
allFileContents.split(/\r?\n/).forEach(line => {
// reset
first2Chars = "";
exp = "";
// clean up line
line = replaceRN(line);
first2Chars = line.substring(0, 2);
if (isArrayInString(first2Chars, first2CharFilterList) === false) {
// log(line);
if (
line.includes("export function") ||
line.includes("export default function") ||
line.includes("export async function") ||
line.includes("export default async function") ||
line.includes("export class") ||
line.includes("export const")
// line.includes("export default class") === false &&
) {
// log(line)
functionName = cleanLineString(line)
exp = `import { ${functionName} } from '${jsImportPath}';`
// log(exp);
fileContents += exp + "\n";
// importList.push(exp);
exportList.push(functionName);
// log(exp);
}
// Handle multiline export export {\n fn, etc }
// TODO
// if (processLineDone === false){
// if (line.includes("export {") || line.includes("export default {")) {
// log(line);
// }
// }
}
});
}
// build export part of file output
let endComma = "";
fileContents += "\n";
fileContents += "export { \n";
for (let i = 0; i < exportList.length; i++) {
if (i < exportList.length) {
endComma = ",";
}
fileContents += "\t" + exportList[i] + endComma + "\n"
}
fileContents += "}";
await fse.writeFile(outputFile, fileContents);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
async function getJsImportsAndExportsFromFile(f) {
// const f = files[0];
// const f = p.resolve("./test.js")
let first2Chars = "";
let first2CharFilterList = ["//", "*", "*/"];
let processLine = true;
let processLineDone = false;
let exp = ""; // will hold the exportString to add to exportList
let functionName = ""; // will hold the function name
let currentDirName = p.basename(process.cwd())
let pathStringToRemove = process.cwd().replace(currentDirName, "")
let jsImportPath = f.replace(pathStringToRemove, "")
// jsImportPath = replaceAll("\\", "/",jsImportPath)
jsImportPath = jsImportPath.replace(/\\/g, '/')
// jsImportPath = jsImportPath.replace("\\\\","/")
log(jsImportPath);
const allFileContents = await fs.readFileSync(f, 'utf-8');
allFileContents.split(/\r?\n/).forEach(line => {
// reset
first2Chars = "";
exp = "";
// clean up line
line = replaceRN(line);
// line = replaceAll("\t", "TestObject", line);
// log(line);
// check line doesn't start with any first2CharFilterList
// let a = first2CharFilterList.some(substring=>line.includes(substring));
first2Chars = line.substring(0, 2);
if (isArrayInString(first2Chars, first2CharFilterList) === false) {
// log(line);
if (
line.includes("export function") ||
line.includes("export default function") ||
line.includes("export async function") ||
line.includes("export default async function") ||
line.includes("export class")
// line.includes("export default class") === false &&
) {
// log(line)
functionName = cleanLineString(line)
exp = `import { ${functionName} } from '${jsImportPath}';`
importList.push(exp);
exportList.push(functionName);
// log(exp);
}
// Handle multiline export export {\n fn, etc }
// TODO
// if (processLineDone === false){
// if (line.includes("export {") || line.includes("export default {")) {
// log(line);
// }
// }
}
});
// await fse.writeFile(outputFile, fileContents);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function cleanLineString(line) {
line = line.replace("export const", "")
line = line.replace("export let", "")
line = line.replace("export function", "")
line = line.replace("export async function", "")
line = line.replace("export default function", "")
line = line.replace("export default async function", "")
line = line.replace("export class", "")
line = line.replace("export default class", "")
line = line.replace(/ *\{[^}]*\) */g, ""); // replace {} and contents"
line = line.replace(/ *\([^)]*\) */g, ""); // replace () and contents"
line = replaceAll("=", "", line)
line = replaceAll(";", "", line)
line = replaceAll(">", "", line)
line = replaceAll("{", "", line)
line = replaceAll("}", "", line)
line = replaceAll(" ", " ", line)
line = replaceAll(" ", " ", line)
line = replaceAll(" ", " ", line)
line = line.trim();
return line;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function buildDocs() {
let subs = {
"[[version]]": version
}
const jsoDoc = new JSODoc({
dir: './src',
recursive: true,
ext: ['js', 'ts'],
// files: ['./aftc-modules.js'],
template: './docs/template.md',
substitutions: subs,
output: './readme.md'
})
jsoDoc.start();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Utils
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function replaceAll(search, replacement, source) {
return source.replace(new RegExp(search, 'g'), replacement);
};
// function replaceAll(find, replace, source) {
// return source.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
// };
function replaceRN(str) {
//remove line breaks from str
str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
str = str.toString().trim().replace(/(\r\n|\n|\r)/g, "");
return str;
}
function replaceTabs(str) {
//remove line breaks from str
// str = str.replace(/\s{2,}/g, ' ');
str = str.replace(/\t/g, ' ');
return str;
}
function isArrayInString(input, arr) {
return arr.some(substring => input.includes(substring));
}