-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (140 loc) · 3.38 KB
/
index.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
#!/usr/bin/env node
const hb = require("handlebars");
const path = require("path");
const fs = require("fs");
const [, , ...args] = process.argv;
let files = {};
function resolveFilesRecursively(absPath) {
let currentDir = fs.readdirSync(absPath, {
withFileTypes: true
});
currentDir.forEach(ent => {
if (ent.isFile())
files[path.join(absPath, ent.name)] = [];
else if (ent.isDirectory())
resolveFiles(path.join(absPath, ent.name));
});
}
function resolveFiles(absPath) {
let currentDir = fs.readdirSync(absPath, {
withFileTypes: true
});
currentDir.forEach(ent => {
if (ent.isFile())
files[path.join(absPath, ent.name)] = [];
});
}
const definedOut = args.findIndex(_ => _ === "-o");
let outPath = path.join(process.cwd(), "qidoc");
if (definedOut !== -1 && args[definedOut + 1]) {
outPath = path.join(process.cwd(), args[definedOut + 1]);
}
if (!fs.existsSync(outPath)) {
fs.mkdirSync(outPath);
}
// Whether should recurse the provided directory
const recurse = args.findIndex(_ => _ === "-r") !== -1;
// Determine the path we wish to search for the files in
let where = args.find(_ => _.charAt(0) !== "-");
// Default to the cwd
if (where === undefined) {
where = process.cwd();
} else if (where.charAt(1) !== ":") {
where = path.join(process.cwd(), where);
}
// Poll for the files
if (recurse) {
resolveFilesRecursively(where);
} else {
resolveFiles(where);
}
// Captures comment blocks
const COMMENT_BLOCKS = /\/\*\*((?:.)*?)\*\//gsi;
// Captures method names
const METHOD_NAMES = /\*\/(?:\n|\s)*([$_A-Za-z][$_A-Za-z0-9]*)\(/gsi;
const MODULE = /ig\.module\("(.*)"/gmi;
const EXTENDS = /(?:\s|\n)*([\n.a-z$_ ][ \n.a-z$_0-9]*)(?:\s|\n)*=(?:\s|\n)*([\n.a-z$_ ][ \n.a-z$_0-9]*)(?:\s|\n)*\.extend/gsi
Object.keys(files).forEach(file => {
const contents = fs.readFileSync(file, "utf-8");
const modules = [...contents.matchAll(MODULE)];
const comment_blocks = [...contents.matchAll(COMMENT_BLOCKS)];
const methods = [...contents.matchAll(METHOD_NAMES)];
const extend = [...contents.matchAll(EXTENDS)];
let all = [];
all.push(
...modules.map(_ => ({
index: _.index,
match: _[1],
type: 0,
})),
...comment_blocks.map(_ => ({
index: _.index,
match: _[1],
type: 1,
})),
...methods.map(_ => ({
index: _.index,
match: _[1],
type: 2,
})),
...extend.map(_ => ({
index: _.index,
match: {
class: _[1],
ext: _[2]
},
type: 3,
})),
);
all.sort((a, b) => a.index - b.index);
files[file] = all;
});
let docTree = {}
Object.keys(files).forEach(file => {
const fileName = path.parse(file).base + ".html";
const matches = files[file];
const relative = path.parse(path.relative(where, file)).dir;
const rawOutPath = path.join(outPath, "raw", relative);
if (!fs.existsSync(rawOutPath)) {
fs.mkdirSync(rawOutPath);
}
fs.writeFileSync(path.join(rawOutPath, fileName), fs.readFileSync(file, "utf-8"));
/**
* @type {
* file
* modules
* classes
* properties
* methods
*
* }
*/
let currentModule = "";
let modules = [];
matches.forEach(match => {
switch (match.type) {
// module
case 0: {
currentModule = match.match;
modules.push(match.match);
break;
}
// methods
case 1: {
break;
}
// comment blocks
case 2: {
break;
}
// classes
case 3: {
break;
}
default: {
throw new Error("Expected match type");
}
}
});
});
console.log(files);