-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathbundle.js
More file actions
65 lines (58 loc) · 1.54 KB
/
Copy pathbundle.js
File metadata and controls
65 lines (58 loc) · 1.54 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
// @ts-check
const fs = require("fs");
const path = require("path");
const header = fs
.readFileSync(path.resolve(__dirname, "Header.md"), "utf-8")
.split(/\n/)
.map((line) => ` * ${line}`)
.join("\n");
const files = Array.from(findFiles(path.resolve(__dirname, "..", "src")));
const sources = files
.filter((file) => !file.endsWith(".test.ts"))
.map(loadFile);
const tests = files.filter((file) => file.endsWith(".test.ts")).map(loadFile);
const outputs = [
`/**\n${header}\n */\n`,
...tests,
`
/**
* ========================================================================================
*
*
* END OF EXAMPLES, START OF IMPLEMENTATION
*
*
* ========================================================================================
*/
`
.split(/\n/)
.map((line) => line.trim())
.join("\n"),
...sources,
];
console.log(outputs.join("\n\n"));
/**
* @param {string} dir The path to search in.
*/
function* findFiles(dir) {
for (const name of fs.readdirSync(dir)) {
const filename = path.join(dir, name);
if (name.endsWith(".ts")) {
yield filename;
} else if (fs.statSync(filename).isDirectory()) {
yield* findFiles(filename);
}
}
}
/**
* @param {string} filename
*/
function loadFile(filename) {
const raw = fs.readFileSync(filename, "utf-8");
const content = raw
.replace(/export\s+type/g, "type")
.replace(/export\s+\*\s+from\s+"(.*)";?/g, "")
.replace(/import\s+\{([\s\S]*)\}\s+from\s+"(.*)";?/g, "")
.trim();
return content;
}