Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
m1k1o
GitHub Repository: m1k1o/neko
Path: blob/master/webpage/src/components/Configuration/generate.js
1007 views
1
/* This script reads a help.txt file and generates a help.json file with the configuration options. */
2
const fs = require('fs');
3
4
const parseConfigOptions = (text) => {
5
const lines = text.split('\n');
6
return lines.map(line => {
7
const match = line.match(/--([\w.]+)(?:\s(\w+))?\s+(.*?)(?:\s+\(default\s+"?([^"]+)"?\))?$/);
8
if (match) {
9
let [, key, type, description, defaultValue] = match;
10
// if the type is not specified, it is a boolean
11
if (!type) {
12
type = 'boolean';
13
// if the default value is not specified, it is false
14
if (!defaultValue) {
15
defaultValue = 'false';
16
} else if (defaultValue !== 'false') {
17
defaultValue = 'true';
18
}
19
}
20
// this is an opaque object
21
if (type === 'string' && defaultValue === '{}') {
22
type = 'object';
23
defaultValue = {};
24
}
25
// this is an opaque array
26
if (type === 'string' && defaultValue === '[]') {
27
type = 'array';
28
defaultValue = [];
29
}
30
return { key: key.split('.'), type, defaultValue: defaultValue || undefined, description };
31
}
32
return null;
33
}).filter(option => option !== null);
34
};
35
36
const [,, inputFilePath, outputFilePath] = process.argv;
37
38
if (!inputFilePath || !outputFilePath) {
39
console.error('Usage: node generate.js <inputFilePath> <outputFilePath>');
40
process.exit(1);
41
}
42
43
fs.readFile(inputFilePath, 'utf8', (err, data) => {
44
if (err) {
45
console.error('Error reading input file:', err);
46
return;
47
}
48
const configOptions = parseConfigOptions(data);
49
fs.writeFile(outputFilePath, JSON.stringify(configOptions, null, 2), (err) => {
50
if (err) {
51
console.error('Error writing output file:', err);
52
} else {
53
console.log('Output file generated successfully.');
54
}
55
});
56
});
57
58