forked from timqian/gql-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateModule.js
executable file
·174 lines (143 loc) · 6.12 KB
/
generateModule.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
#!/usr/bin/env node
const finder = require('find-package-json');
const shelljs = require('shelljs');
const getModuleInfos = require('./parsegraphql/getModuleInfos');
const getModuleNames = require('./parsegraphql/getModuleNames');
const checkIfGitStateClean = require('./helpers/checkIfGitStateClean');
const saveRenderedTemplate = require('./helpers/saveRenderedTemplate');
checkIfGitStateClean();
const f = finder(process.cwd());
const projectMainPath = f
.next()
.filename.split('/')
.filter((c) => c.indexOf('package.json') == -1)
.join('/');
const graphqlPaths = shelljs.ls(`${projectMainPath}/src/modules/*/graphql/*.graphql`).map((p) => ({ name: p }));
const moduleNames = getModuleNames(graphqlPaths);
const modules = getModuleInfos(moduleNames);
modules.forEach((module) => {
const moduleName = module.name;
const createModuleResolvers = () => {
const templateName = './templates/moduleResolvers.handlebars';
const context = { ...module, moduleName: module.name };
const filePath = `${projectMainPath}/src/modules/${moduleName}/graphql/`;
const fileName = `${moduleName}Resolvers.ts`;
saveRenderedTemplate(templateName, context, filePath, fileName);
};
createModuleResolvers();
const createQuery = (queryName) => {
const templateName = './templates/query.handlebars';
const context = { queryName, moduleName };
const filePath = `${projectMainPath}/src/modules/${moduleName}/graphql/queries/`;
const fileName = `${queryName}Query.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
};
const createQuerySpec = (queryName) => {
const templateName = './templates/query.spec.handlebars';
const context = { queryName, moduleName };
const filePath = `${projectMainPath}/src/modules/${moduleName}/graphql/queries/`;
const fileName = `${queryName}Query.spec.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
};
if (module.queries && module.queries.length) {
shelljs.mkdir('-p', `${projectMainPath}/src/modules/${moduleName}/graphql/queries`);
module.queries.forEach(({ name }) => {
createQuery(name);
createQuerySpec(name);
});
}
const createMutation = (mutationName) => {
const templateName = './templates/mutation.handlebars';
const context = { mutationName, moduleName };
const filePath = `${projectMainPath}/src/modules/${moduleName}/graphql/mutations/`;
const fileName = `${mutationName}Mutation.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
};
const createMutationSpec = (mutationName) => {
const templateName = './templates/mutation.spec.handlebars';
const context = { mutationName, moduleName };
const filePath = `${projectMainPath}/src/modules/${moduleName}/graphql/mutations/`;
const fileName = `${mutationName}Mutation.spec.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
};
if (module.mutations && module.mutations.length) {
shelljs.mkdir('-p', `${projectMainPath}/src/modules/${moduleName}/graphql/mutations`);
module.mutations.forEach(({ name }) => {
createMutation(name);
createMutationSpec(name);
});
}
});
const createGlobalResolvers = () => {
const templateName = './templates/resolvers.handlebars';
const context = { modules };
const filePath = `${projectMainPath}/src/graphql/`;
const fileName = `resolvers.ts`;
saveRenderedTemplate(templateName, context, filePath, fileName);
};
createGlobalResolvers();
const createTypes = () => {
const templateName = './templates/types.handlebars';
const context = { modules };
const filePath = `${projectMainPath}/src/`;
const fileName = `types.ts`;
saveRenderedTemplate(templateName, context, filePath, fileName);
};
createTypes();
const createStartupConfig = () => {
const templateName = './templates/startupConfig.handlebars';
const context = { modules };
const filePath = `${projectMainPath}/src/`;
const fileName = `startupConfig.ts`;
saveRenderedTemplate(templateName, context, filePath, fileName);
};
createStartupConfig();
const createIModuleNameContexts = () => {
modules.forEach(({ name }) => {
const templateName = './templates/IModuleNameContext.handlebars';
const context = { moduleName: name };
const filePath = `${projectMainPath}/src/modules/${name}/`;
const fileName = `I${name}Context.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
});
};
createIModuleNameContexts();
const createGetModuleNameContexts = () => {
modules.forEach(({ name }) => {
const templateName = './templates/getModuleNameContext.handlebars';
const context = { moduleName: name };
const filePath = `${projectMainPath}/src/modules/${name}/`;
const fileName = `get${name}Context.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
});
};
createGetModuleNameContexts();
// typeResolvers.handlebars
const createTypeResolvers = () => {
modules.forEach(({ name, typeDefinitions, types }) => {
if (types) {
shelljs.mkdir('-p', `${projectMainPath}/src/modules/${name}/graphql/types/`);
const templateName = './templates/typeResolvers.handlebars';
const context = { type: typeDefinitions };
const filePath = `${projectMainPath}/src/modules/${name}/graphql/types/`;
const fileName = `typeResolvers.ts`;
const keepIfExists = false;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
typeDefinitions.forEach((typeDef) => {
const templateName = './templates/typeTypeResolvers.handlebars';
const context = { typeName: typeDef.name };
const filePath = `${projectMainPath}/src/modules/${name}/graphql/types/`;
const fileName = `${typeDef.name}TypeResolvers.ts`;
const keepIfExists = true;
saveRenderedTemplate(templateName, context, filePath, fileName, keepIfExists);
});
}
});
};
createTypeResolvers();