-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocs.mjs
executable file
·144 lines (115 loc) · 3.52 KB
/
docs.mjs
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
#!/usr/bin/env node
import { readFile, writeFile } from 'node:fs/promises';
const regex =
/[\s\n]*\/\/-n \w(\w|-)*[\s\n]*\/\/-d (\w).*[\s\n]*(\/\/-i \w+\s*:\s*\w(\w|`|\'|\{|}|\$|%| |\|)*)?[\s\n]*(\/\/-o (\w|\.|\[|])+:\s*\'?(\w|-|\[|]|\{|}|:| |,|%|#|\.)+'?[\s\n]*)+/g;
const imports = {
colord: '{ Colord }',
};
const tsFiles = {
'colors.ts': 'Color Utils',
'display.ts': 'Utils for flex & co',
'font.ts': 'Utils for texts',
'other.ts': 'Other Utils',
'position.ts': 'Position & Size Utils',
'spacing.ts': 'Utils for margin, padding & co',
'border.ts': 'Utils for borders',
};
String.prototype.splitOnce = function (searchFor) {
const index = this.indexOf(searchFor);
if (index === -1) {
return [this, ''];
} else {
const start = this.substring(0, index);
const end = this.substring(index + 1, this.length);
return [start, end];
}
};
let readme = '# Attributes for `react-native-alam`';
readme +=
'\n\nTip: Click the list icon at the to right to search for properties / alams';
let dTs = '';
for (const namespace in imports) {
const value = imports[namespace];
dTs += `import type ${value} from '${namespace}';\n`;
}
dTs += '\nexport interface DefaultProps {';
for (const fileName in tsFiles) {
const title = tsFiles[fileName];
const content = await readFile(`./src/definitions/${fileName}`, 'utf8');
const matches = content.match(regex);
if (!matches) continue;
/**
* @type {{ name: string, desc: string, input: [string, string] | undefined, outputs: [string, string][] }[]}
*/
const found = [];
for (const match of matches) {
const lines = match
.split('\n')
.map((s) => s.trim())
.filter(Boolean);
if (lines.length < 3) {
console.warn('Invalid comment:', match);
}
const name = lines
.splice(0, 1)[0]
.replace(/^\/\/-n/, '')
.trim();
const desc = lines
.splice(0, 1)[0]
.replace(/^\/\/-d/, '')
.trim();
const maybeInput = lines[0];
let input;
if (/^\/\/-i/.test(maybeInput)) {
input = lines
.splice(0, 1)[0]
.replace(/^\/\/-i/, '')
.trim()
.splitOnce(':')
.map((s) => s.trim());
}
const outputs = lines.map((l) =>
l
.replace(/^\/\/-o/, '')
.trim()
.splitOnce(':')
.map((s) => s.trim())
);
if (outputs.some((o) => o.length !== 2)) {
console.warn('Invalid comment output:', match);
}
found.push({ name, desc, input, outputs });
}
for (const { name, input, desc } of found) {
if (desc) {
dTs += `\n // ${desc}`;
}
dTs += `\n ${/^[a-z]+$/i.test(name) ? name : `'${name}'`}?: ${
input ? input[1] : 'true'
};`;
}
readme += '\n\n## ' + title;
for (const { name, desc, input, outputs } of found) {
const nameFormat = `### ${name}`;
const descFormat = desc;
const escapeInput = input?.some((s) => s.includes('`'));
const ticks = escapeInput ? '``' : '`';
const inputFormat = input ? `${ticks}${input[0]}: ${input[1]}${ticks}` : '';
const outputFormat =
`Changes:\n\n\`\`\`js\n({` +
outputs
.map(([name, value]) => {
return `\n ${name}: ${value},`;
})
.join('') +
'\n ...styles,\n});\n```';
readme += '\n\n' + nameFormat;
readme += '\n\n' + descFormat;
readme += inputFormat ? '\n\n' + inputFormat : '';
readme += '\n\n' + outputFormat;
}
}
dTs += '\n}\n';
readme += '\n';
await writeFile('src/definitions/index.d.ts', dTs, 'utf8');
await writeFile('ATTR.md', readme, 'utf8');