Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
242 lines (198 loc) · 5.82 KB
/
Copy pathindex.js
File metadata and controls
242 lines (198 loc) · 5.82 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
240
241
242
const fs = require("fs");
const util = require("util");
const glob = util.promisify(require("glob"));
const app = require("./src/app");
let args = process.argv.splice(2);
// args
{ // split
let a = [];
args.forEach((arg) => {
if (/^-[^-].+/gm.test(arg))
for (let i = 1; i < arg.length; i++)
a.push(`-${arg[i]}`);
else
a.push(arg);
});
args = a;
}
{ // expand
let a = app.cli.alias;
args = args.map((_arg) => {
if (/^-[^-]/gm.test(_arg)) {
let arg = _arg[1];
return a[arg] ? `--${a[arg]}` : `--${arg}`;
} else {
return _arg;
}
});
}
{ // set
args.forEach((_arg, i) => {
let arg = _arg.substring(2);
if (app.cli.options[arg] != undefined) {
app.cli.options[arg] = (args[i + 1] || "--").startsWith("--") ?
true : args[i + 1];
} else {
console.log(`script: invalid option -- '${arg}'`);
process.exit(-1);
}
});
}
// special flags
const options = app.cli.options;
if (options.help) { // --help
// prints usage and flag functions
console.log("usage: script [flags]\n\nflags:" + _(app.cli.usage));
process.exit();
function _(object) {
let markdown = "";
// for each flag
Object.keys(object).forEach((key) => {
// append flag name and usage
markdown += `\n --${key}\t${object[key]}`;
});
return markdown;
}
}
if (options.version) { // --version
let v = app.version;
// outputs "version: MAJOR.MINOR" (and ".PATCH" if >0)
console.log("version: " +
`${v.major}.${v.minor}${v.patch ? `.${v.patch}` : ""}`);
process.exit();
}
// run
getSettings()
.then(formatSettings)
.then(console.log)
.catch(console.error);
function formatSettings(settings) {
// variable to store finalized message
let s = "";
if (options.list && options.markdown)
s += "```css\n";
// loop over each category
Object.keys(settings).forEach((_category) => {
if (!options.list) {
// append category name
s += options.markdown ? "```css\n" : "\n";
s += `${_category[0].toUpperCase()}${_category.substring(1)} {\n`;
}
let category = settings[_category];
// loop over each module
Object.keys(category).forEach((_module) => {
let module = category[_module];
// get settings of module
let settings = Object.keys(module);
if (options.list) {
if (settings.length > 0) {
// for each variable in module
settings.forEach((setting) =>
// append setting and default value
s += `--${setting}: ${module[setting]};\n`);
}
} else {
if (settings.length == 0) {
// fallback if module does not contain any options
// append module name and stub block
s += options.markdown
? ` ${_module} { /* no variables */ }\n`
: ` ${_module} { no variables }\n`
} else {
// append module name
s += ` ${_module} {\n`;
// for each variable in module
settings.forEach((setting) =>
// append setting and default value
s += ` --${setting}: ${module[setting]};\n`);
// close module block
s += " }\n";
}
}
});
// close category block
if (!options.list)
s += options.markdown ? "}\n```\n" : "}\n";
});
if (options.list && options.markdown)
s += "```\n";
// append explaination
if (options.explain && !options.list) {
if (options.markdown) {
s += "```css\n";
s += "/* this is how this channel is organized */\n" +
"Category {\n" +
" module.m.css {\n" +
" /* list of options and modifications in the form of variables */\n" +
" }\n" +
"}\n\n" +
"/* your import link would then be `https://customa.gitlab.io/Customa-Discord/Category/module.m.css\` */";
s += "\n```\n";
} else {
s += "\nthis is how this channel is organized:\n\n" +
"Category {\n" +
" module.m.css {\n" +
" list of options and modifications in the form of variables\n" +
" }\n" +
"}\n\n" +
"your import link would then be `https://customa.gitlab.io/Customa-Discord/Category/module.m.css`\n";
}
}
return s;
}
function getSettings() {
return new Promise((resolve, reject) => {
// get all module files
glob("*/*.m.css").then((files) => {
// create category tree
let data = {};
// for each module found
files.forEach((path) => {
// get category and module from path of module
let category = path.split("/")[0];
let module = path.split("/")[1];
if (!data[category])
data[category] = [];
data[category].push(module);
});
// get all categories
Object.keys(data).forEach((category) => {
let modules = data[category];
let newCategory = {};
// get all modules in a category
modules.forEach((module) => {
// read module file
let content = fs.readFileSync(`${category}/${module}`, "UTF-8").split(/\r\n|\n/);
let variables = {};
// loop over each line
for (let i = 0; i < content.length; i++) {
let line = content[i];
// save time by only looping through :root
// :root should be at start of file so the first closing bracket belongs to it
if (line == "}")
break;
// ignore internal variables (settings module, variables module)
// ignore code block language variables (holy shit there are so many, and no need to configure)
if (/[ |\t]*--INT.*;/.test(line) || /[ |\t]*--ContentProgLang-.*;/.test(line))
continue;
// make ***sure*** line is variable
if (/[ |\t]*--.*(\*\/|;)/.test(line)) {
// get name and value of variable
let name = /[ |\t]*--(.*): +/g.exec(line);
let value = /[ |\t]*--.*: +(.*);/gm.exec(line);
if (!name || !value)
continue;
// assign
variables[name[1]] = value[1];
}
}
// assign
newCategory[module] = variables;
});
// replace list of modules with object of modules and values
data[category] = newCategory;
});
resolve(data);
}).catch(reject);
});
}