-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathReactNativeGeneratorV2.js
177 lines (157 loc) · 4.7 KB
/
ReactNativeGeneratorV2.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
166
167
168
169
170
171
172
173
174
175
176
177
import chalk from "chalk";
import handlebars from "handlebars";
import BaseGenerator from "./BaseGenerator.js";
import hbhComparison from "handlebars-helpers/lib/comparison.js";
export default class extends BaseGenerator {
constructor(params) {
super(params);
handlebars.registerHelper("ifNotResource", function (item, options) {
if (item === null) {
return options.fn(this);
}
return options.inverse(this);
});
this.registerTemplates(`react-native-v2/`, [
"app/(tabs)/foos.tsx",
"app/_layout.tsx.dist",
"lib/hooks/data.ts",
"lib/hooks/mercure.ts",
"lib/hooks/modal.ts",
"lib/hooks/notifications.ts",
"lib/types/ApiResource.ts",
"lib/types/HydraView.ts",
"lib/types/HydraResponse.ts",
"lib/types/foo.ts",
"lib/utils/Logs.ts",
"lib/utils/mercure.ts",
"lib/utils/icons.tsx",
"lib/api/fooApi.ts",
"components/Main.tsx",
"components/Navigation.tsx",
"components/ConfirmModal.tsx",
"components/foo/CreateEditModal.tsx",
"components/foo/Form.tsx",
"components/foo/LogsRenderer.tsx",
"components/foo/Context.ts",
]);
handlebars.registerHelper("compare", hbhComparison.compare);
}
help(resource) {
const titleLc = resource.title.toLowerCase();
console.log(
'Code for the "%s" resource type has been generated!',
resource.title
);
console.log(
"You should replace app/_layout.tsx by the generated one and add the following route:"
);
console.log(
chalk.green(`
<Tabs.Screen
name="(tabs)/${titleLc}s"
options={options.tabs.${titleLc}}
/>
tabs: {
...
${titleLc}: {
title: '${titleLc}',
headerShown: false,
tabBarIcon: ({ color }) => <TabBarIcon name="home" color={color} />,
},
}
`)
);
}
generate(api, resource, dir) {
const lc = resource.title.toLowerCase();
const titleUcFirst =
resource.title.charAt(0).toUpperCase() + resource.title.slice(1);
const fields = this.parseFields(resource);
const context = {
title: resource.title,
name: resource.name,
lc,
uc: resource.title.toUpperCase(),
fields,
formFields: this.buildFields(fields),
hydraPrefix: this.hydraPrefix,
ucf: titleUcFirst,
};
// Create directories
// These directories may already exist
[
`${dir}/app/(tabs)`,
`${dir}/config`,
`${dir}/components`,
`${dir}/components/${lc}`,
`${dir}/lib`,
`${dir}/lib/api`,
`${dir}/lib/types`,
`${dir}/lib/hooks`,
`${dir}/lib/utils`,
].forEach((dir) => this.createDir(dir, false));
// static files
[
"lib/types/ApiResource.ts",
"lib/hooks/data.ts",
"lib/hooks/mercure.ts",
"lib/hooks/modal.ts",
"lib/hooks/notifications.ts",
"lib/utils/Logs.ts",
"lib/utils/mercure.ts",
"lib/utils/icons.tsx",
"components/Main.tsx",
"components/Navigation.tsx",
"components/ConfirmModal.tsx",
].forEach((file) => this.createFile(file, `${dir}/${file}`));
// templated files ucFirst
["lib/types/%s.ts"].forEach((pattern) =>
this.createFileFromPattern(pattern, dir, [titleUcFirst], context)
);
// templated files lc
[
"app/(tabs)/%ss.tsx",
"app/_layout.tsx.dist",
"lib/api/%sApi.ts",
"components/%s/Context.ts",
"components/%s/CreateEditModal.tsx",
"components/%s/Form.tsx",
"components/%s/LogsRenderer.tsx",
"lib/types/HydraView.ts",
"lib/types/HydraResponse.ts",
].forEach((pattern) =>
this.createFileFromPattern(pattern, dir, [lc], context)
);
this.createEntrypoint(api.entrypoint, `${dir}/config/entrypoint.js`);
}
getDescription(field) {
return field.description ? field.description.replace(/"/g, "'") : "";
}
parseFields(resource) {
const fields = [
...resource.writableFields,
...resource.readableFields,
].reduce((list, field) => {
if (list[field.name]) {
return list;
}
const isReferences = Boolean(
field.reference && field.maxCardinality !== 1
);
const isEmbeddeds = Boolean(field.embedded && field.maxCardinality !== 1);
return {
...list,
[field.name]: {
...field,
type: this.getType(field),
description: this.getDescription(field),
readonly: false,
isReferences,
isEmbeddeds,
isRelations: isEmbeddeds || isReferences,
},
};
}, {});
return Object.values(fields);
}
}