This repository was archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeckdeckgo.desc.ts
105 lines (85 loc) · 2.82 KB
/
deckdeckgo.desc.ts
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
import {JsonDocs} from '@stencil/core/internal';
import {JsonDocsComponent, JsonDocsProp, JsonDocsSlot} from '@stencil/core/internal/stencil-public-docs';
import * as fs from 'fs';
interface DeckDeckGoAuthor {
name?: string;
url?: string;
}
interface DeckDeckGoSlot {
name: string;
placeholder?: string;
types?: string[];
author?: DeckDeckGoAuthor;
}
interface DeckDeckGoProp {
name: string;
type: string;
placeholder?: string;
}
const parseSlots = (slots: JsonDocsSlot[] | undefined): DeckDeckGoSlot[] | undefined => {
if (!slots || slots.length <= 0) {
return undefined;
}
return slots.map((slot: JsonDocsSlot) => {
const docs: string[] | undefined = slot.docs?.split('-');
const [, ...elements] = docs;
const types: string[] | undefined = elements
.join('-')
.trim()
.split(',')
.filter((element: string) => element !== '');
return {
name: slot.name,
...(docs && docs.length >= 1 && docs[0] !== '' && {placeholder: docs[0].trim()}),
...(types && types.length > 0 && {types})
};
});
};
const parseProps = (props: JsonDocsProp[] | undefined): DeckDeckGoProp[] | undefined => {
if (!props || props.length <= 0) {
return undefined;
}
return props
.filter((prop: JsonDocsProp) => prop.reflectToAttr && ['string', 'number', 'boolean'].includes(prop.type))
.map((prop: JsonDocsProp) => {
return {
name: prop.attr,
type: prop.type,
...(prop.docs && {placeholder: prop.docs})
};
});
};
const parseAuthor = (): DeckDeckGoAuthor | undefined => {
const packageJson = require('./package.json');
if (!packageJson?.author?.name) {
return undefined;
}
return {
name: packageJson.author.name,
...(packageJson.author.url && {url: packageJson.author.url})
};
};
export const generateDesc = (docs: JsonDocs) => {
if (!docs || !docs.components) {
console.warn('No docs or components provided.');
}
const author: DeckDeckGoAuthor | undefined = parseAuthor();
const components = docs.components.map((cmp: JsonDocsComponent) => {
const slots: DeckDeckGoSlot[] | undefined = parseSlots(cmp.slots);
const props: DeckDeckGoProp[] | undefined = parseProps(cmp.props);
const propType: DeckDeckGoProp | undefined = props?.find((prop: DeckDeckGoProp) => prop.name === 'type');
if (propType) {
throw new Error('The property "type" is a reserved keyword. Rename it to resolve the issue.');
}
if (/(?:deckgo|deckdeckgo|ddg)/.test(cmp.tag)) {
throw new Error('Your tag name should not contains one of the following keywords: deckgo, deckdeckgo or ddg');
}
return {
tag: cmp.tag,
...(author && {author}),
...(props && {props}),
...(slots && {slots})
};
});
fs.writeFileSync('./src/components.desc.json', JSON.stringify(components, null, 2));
};