@modern-js/bff-generator 3.6.3 → 3.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1963 -64245
- package/package.json +10 -12
- package/src/index.ts +0 -319
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "3.
|
|
18
|
+
"version": "3.7.1",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./src/index.ts",
|
|
21
21
|
"main": "./dist/index.js",
|
|
@@ -23,22 +23,20 @@
|
|
|
23
23
|
"/templates",
|
|
24
24
|
"/dist/index.js"
|
|
25
25
|
],
|
|
26
|
-
"dependencies": {
|
|
27
|
-
"@modern-js/utils": "2.60.3"
|
|
28
|
-
},
|
|
29
26
|
"devDependencies": {
|
|
30
|
-
"@modern-js/codesmith": "2.5
|
|
31
|
-
"@modern-js/codesmith
|
|
32
|
-
"@modern-js/codesmith-api-
|
|
27
|
+
"@modern-js/codesmith-utils": "2.6.5",
|
|
28
|
+
"@modern-js/codesmith": "2.6.5",
|
|
29
|
+
"@modern-js/codesmith-api-app": "2.6.5",
|
|
30
|
+
"@modern-js/codesmith-api-json": "2.6.5",
|
|
33
31
|
"@types/jest": "^29",
|
|
34
32
|
"@types/node": "^14",
|
|
35
33
|
"jest": "^29",
|
|
36
34
|
"typescript": "^5",
|
|
37
|
-
"@modern-js/generator-
|
|
38
|
-
"@modern-js/plugin-i18n": "2.60.
|
|
39
|
-
"@
|
|
40
|
-
"@scripts/
|
|
41
|
-
"@
|
|
35
|
+
"@modern-js/generator-utils": "3.7.1",
|
|
36
|
+
"@modern-js/plugin-i18n": "2.60.5",
|
|
37
|
+
"@scripts/build": "2.60.5",
|
|
38
|
+
"@scripts/jest-config": "2.60.5",
|
|
39
|
+
"@modern-js/generator-common": "3.7.1"
|
|
42
40
|
},
|
|
43
41
|
"sideEffects": false,
|
|
44
42
|
"publishConfig": {
|
package/src/index.ts
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import type { GeneratorContext, GeneratorCore } from '@modern-js/codesmith';
|
|
3
|
-
import { AppAPI } from '@modern-js/codesmith-api-app';
|
|
4
|
-
import { JsonAPI } from '@modern-js/codesmith-api-json';
|
|
5
|
-
import {
|
|
6
|
-
BFFPluginDependence,
|
|
7
|
-
BFFPluginName,
|
|
8
|
-
BFFType,
|
|
9
|
-
Framework,
|
|
10
|
-
FrameworkAppendTypeContent,
|
|
11
|
-
Language,
|
|
12
|
-
Solution,
|
|
13
|
-
i18n as commonI18n,
|
|
14
|
-
getBFFSchema,
|
|
15
|
-
} from '@modern-js/generator-common';
|
|
16
|
-
import {
|
|
17
|
-
fs,
|
|
18
|
-
chalk,
|
|
19
|
-
getModernConfigFile,
|
|
20
|
-
getModernPluginVersion,
|
|
21
|
-
getPackageVersion,
|
|
22
|
-
isTsProject,
|
|
23
|
-
readTsConfigByFile,
|
|
24
|
-
} from '@modern-js/generator-utils';
|
|
25
|
-
import { i18n, localeKeys } from './locale';
|
|
26
|
-
|
|
27
|
-
function isEmptyApiDir(apiDir: string) {
|
|
28
|
-
const files = fs.readdirSync(apiDir);
|
|
29
|
-
if (files.length === 0) {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
return files.every(file => {
|
|
33
|
-
if (fs.statSync(path.join(apiDir, file)).isDirectory()) {
|
|
34
|
-
const childFiles = fs.readdirSync(path.join(apiDir, file));
|
|
35
|
-
return childFiles.length === 0;
|
|
36
|
-
}
|
|
37
|
-
return false;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const handleTemplateFile = async (
|
|
42
|
-
context: GeneratorContext,
|
|
43
|
-
generator: GeneratorCore,
|
|
44
|
-
appApi: AppAPI,
|
|
45
|
-
) => {
|
|
46
|
-
const jsonAPI = new JsonAPI(generator);
|
|
47
|
-
|
|
48
|
-
const ans = await appApi.getInputBySchemaFunc(getBFFSchema, context.config);
|
|
49
|
-
|
|
50
|
-
const appDir = context.materials.default.basePath;
|
|
51
|
-
const apiDir = path.join(appDir, 'api');
|
|
52
|
-
|
|
53
|
-
if (fs.existsSync(apiDir) && !isEmptyApiDir(apiDir)) {
|
|
54
|
-
const files = fs.readdirSync(apiDir);
|
|
55
|
-
if (files.length > 0) {
|
|
56
|
-
generator.logger.warn(`🟡 The 'api' directory already exists.`);
|
|
57
|
-
throw Error("The 'api' directory is already exist");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const { bffType, framework } = ans;
|
|
62
|
-
|
|
63
|
-
const language = isTsProject(appDir) ? Language.TS : Language.JS;
|
|
64
|
-
|
|
65
|
-
const getBffPluginVersion = (packageName: string) => {
|
|
66
|
-
return getModernPluginVersion(Solution.MWA, packageName, {
|
|
67
|
-
registry: context.config.registry,
|
|
68
|
-
distTag: context.config.distTag,
|
|
69
|
-
cwd: context.materials.default.basePath,
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// update bff plugin config
|
|
74
|
-
context.config.bffPluginName = BFFPluginName[framework as Framework];
|
|
75
|
-
context.config.bffPluginDependence =
|
|
76
|
-
BFFPluginDependence[framework as Framework];
|
|
77
|
-
|
|
78
|
-
let updateInfo: Record<string, string> = {};
|
|
79
|
-
|
|
80
|
-
if (framework === Framework.Express || framework === Framework.Koa) {
|
|
81
|
-
updateInfo = {
|
|
82
|
-
[`devDependencies.@types/${framework as string}`]: `~${await getPackageVersion(`@types/${framework as string}`)}`,
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
updateInfo = {
|
|
87
|
-
...updateInfo,
|
|
88
|
-
[`dependencies.${framework as string}`]: `~${await getPackageVersion(
|
|
89
|
-
framework as string,
|
|
90
|
-
)}`,
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
await jsonAPI.update(
|
|
94
|
-
context.materials.default.get(path.join(appDir, 'package.json')),
|
|
95
|
-
{
|
|
96
|
-
query: {},
|
|
97
|
-
update: {
|
|
98
|
-
$set: {
|
|
99
|
-
'dependencies.@modern-js/plugin-bff': `${await getBffPluginVersion(
|
|
100
|
-
'@modern-js/plugin-bff',
|
|
101
|
-
)}`,
|
|
102
|
-
[`dependencies.@modern-js/plugin-${framework as string}`]: `${await getBffPluginVersion(
|
|
103
|
-
`@modern-js/plugin-${framework as string}`,
|
|
104
|
-
)}`,
|
|
105
|
-
'devDependencies.ts-node': '~10.8.1',
|
|
106
|
-
'devDependencies.tsconfig-paths': '~3.14.1',
|
|
107
|
-
...updateInfo,
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
true,
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
if (language === Language.TS) {
|
|
115
|
-
const tsconfigJSON = readTsConfigByFile(path.join(appDir, 'tsconfig.json'));
|
|
116
|
-
|
|
117
|
-
if (!(tsconfigJSON.include || []).includes('api')) {
|
|
118
|
-
await jsonAPI.update(
|
|
119
|
-
context.materials.default.get(path.join(appDir, 'tsconfig.json')),
|
|
120
|
-
{
|
|
121
|
-
query: {},
|
|
122
|
-
update: {
|
|
123
|
-
$set: {
|
|
124
|
-
include: [...(tsconfigJSON.include || []), 'api'],
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
true,
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (bffType === BFFType.Func) {
|
|
134
|
-
if (language === Language.TS) {
|
|
135
|
-
await jsonAPI.update(
|
|
136
|
-
context.materials.default.get(path.join(appDir, 'tsconfig.json')),
|
|
137
|
-
{
|
|
138
|
-
query: {},
|
|
139
|
-
update: {
|
|
140
|
-
$set: {
|
|
141
|
-
'compilerOptions.paths.@api/*': ['./api/*'],
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
true,
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
await appApi.forgeTemplate(
|
|
149
|
-
'templates/function/base/*',
|
|
150
|
-
undefined,
|
|
151
|
-
resourceKey =>
|
|
152
|
-
resourceKey
|
|
153
|
-
.replace('templates/function/base/', 'api/')
|
|
154
|
-
.replace('.handlebars', `.${language}`),
|
|
155
|
-
);
|
|
156
|
-
await appApi.forgeTemplate(
|
|
157
|
-
`templates/function/info/*`,
|
|
158
|
-
resourceKey => resourceKey.includes(language),
|
|
159
|
-
resourceKey =>
|
|
160
|
-
resourceKey
|
|
161
|
-
.replace('templates/function/info/', 'api/info/')
|
|
162
|
-
.replace('.handlebars', ``),
|
|
163
|
-
);
|
|
164
|
-
await appApi.forgeTemplate(
|
|
165
|
-
`templates/function/app/${language}/${
|
|
166
|
-
framework as string
|
|
167
|
-
}.${language}.handlebars`,
|
|
168
|
-
undefined,
|
|
169
|
-
resourceKey =>
|
|
170
|
-
resourceKey.replace(
|
|
171
|
-
`templates/function/app/${language}/${
|
|
172
|
-
framework as string
|
|
173
|
-
}.${language}.handlebars`,
|
|
174
|
-
`api/_app.${language}`,
|
|
175
|
-
),
|
|
176
|
-
);
|
|
177
|
-
} else {
|
|
178
|
-
if (language === Language.TS) {
|
|
179
|
-
await jsonAPI.update(
|
|
180
|
-
context.materials.default.get(path.join(appDir, 'tsconfig.json')),
|
|
181
|
-
{
|
|
182
|
-
query: {},
|
|
183
|
-
update: {
|
|
184
|
-
$set: {
|
|
185
|
-
'compilerOptions.paths.@api/*': ['./api/lambda/*'],
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
true,
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
await appApi.forgeTemplate(
|
|
193
|
-
`templates/framework/lambda/*`,
|
|
194
|
-
undefined,
|
|
195
|
-
resourceKey =>
|
|
196
|
-
resourceKey
|
|
197
|
-
.replace(`templates/framework/`, 'api/')
|
|
198
|
-
.replace('.handlebars', `.${language}`),
|
|
199
|
-
);
|
|
200
|
-
await appApi.forgeTemplate(
|
|
201
|
-
`templates/framework/app/${framework as string}/**/*`,
|
|
202
|
-
resourceKey =>
|
|
203
|
-
framework === Framework.Koa ? resourceKey.includes(language) : true,
|
|
204
|
-
resourceKey =>
|
|
205
|
-
resourceKey
|
|
206
|
-
.replace(`templates/framework/app/${framework as string}/`, 'api/')
|
|
207
|
-
.replace(
|
|
208
|
-
'.handlebars',
|
|
209
|
-
framework === Framework.Express ? `.${language}` : '',
|
|
210
|
-
),
|
|
211
|
-
);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const appendTypeContent = FrameworkAppendTypeContent[framework as Framework];
|
|
215
|
-
|
|
216
|
-
if (appendTypeContent && language === Language.TS) {
|
|
217
|
-
const typePath = path.join(appDir, 'src', 'modern-app-env.d.ts');
|
|
218
|
-
if (fs.existsSync(typePath)) {
|
|
219
|
-
const npmrc = fs.readFileSync(typePath, 'utf-8');
|
|
220
|
-
if (!npmrc.includes(appendTypeContent)) {
|
|
221
|
-
fs.writeFileSync(typePath, `${npmrc}${appendTypeContent}\n`, 'utf-8');
|
|
222
|
-
}
|
|
223
|
-
} else {
|
|
224
|
-
fs.ensureFileSync(typePath);
|
|
225
|
-
fs.writeFileSync(typePath, appendTypeContent, 'utf-8');
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
|
231
|
-
const appApi = new AppAPI(context, generator);
|
|
232
|
-
|
|
233
|
-
const { locale } = context.config;
|
|
234
|
-
i18n.changeLanguage({ locale });
|
|
235
|
-
appApi.i18n.changeLanguage({ locale });
|
|
236
|
-
commonI18n.changeLanguage({ locale });
|
|
237
|
-
|
|
238
|
-
if (!(await appApi.checkEnvironment())) {
|
|
239
|
-
process.exit(1);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
generator.logger.debug(`🚀 [Start Run BFF Generator]`);
|
|
243
|
-
generator.logger.debug(
|
|
244
|
-
'💡 [Current Config]:',
|
|
245
|
-
JSON.stringify(context.config),
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
try {
|
|
249
|
-
await handleTemplateFile(context, generator, appApi);
|
|
250
|
-
} catch (e) {
|
|
251
|
-
process.exit(1);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
await appApi.runInstall(undefined, { ignoreScripts: true });
|
|
255
|
-
|
|
256
|
-
if (!context.config.isSubGenerator) {
|
|
257
|
-
await appApi.runInstall(undefined, { ignoreScripts: true });
|
|
258
|
-
if (!context.config.pluginName) {
|
|
259
|
-
appApi.showSuccessInfo();
|
|
260
|
-
} else {
|
|
261
|
-
const appDir = context.materials.default.basePath;
|
|
262
|
-
const configFile = await getModernConfigFile(appDir);
|
|
263
|
-
const isTS = configFile.endsWith('ts');
|
|
264
|
-
const {
|
|
265
|
-
pluginName,
|
|
266
|
-
bffPluginName,
|
|
267
|
-
pluginDependence,
|
|
268
|
-
bffPluginDependence,
|
|
269
|
-
shouldUsePluginNameExport,
|
|
270
|
-
} = context.config;
|
|
271
|
-
console.info(
|
|
272
|
-
chalk.green(`\n[INFO]`),
|
|
273
|
-
`${i18n.t(localeKeys.success)}`,
|
|
274
|
-
chalk.yellow.bold(`${configFile}`),
|
|
275
|
-
':',
|
|
276
|
-
'\n',
|
|
277
|
-
);
|
|
278
|
-
if (shouldUsePluginNameExport) {
|
|
279
|
-
console.info(
|
|
280
|
-
chalk.yellow.bold(
|
|
281
|
-
`import { ${pluginName} } from '${pluginDependence}';`,
|
|
282
|
-
),
|
|
283
|
-
);
|
|
284
|
-
console.info(
|
|
285
|
-
chalk.yellow.bold(
|
|
286
|
-
`import { ${bffPluginName} } from '${bffPluginDependence}';`,
|
|
287
|
-
),
|
|
288
|
-
);
|
|
289
|
-
} else {
|
|
290
|
-
console.info(
|
|
291
|
-
chalk.yellow.bold(`import ${pluginName} from '${pluginDependence}';`),
|
|
292
|
-
);
|
|
293
|
-
console.info(
|
|
294
|
-
chalk.yellow.bold(
|
|
295
|
-
`import ${bffPluginName} from '${bffPluginDependence}';`,
|
|
296
|
-
),
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
if (isTS) {
|
|
301
|
-
console.info(`
|
|
302
|
-
export default defineConfig({
|
|
303
|
-
...,
|
|
304
|
-
plugins: [..., ${chalk.yellow.bold(`${pluginName}(), ${bffPluginName}()`)}],
|
|
305
|
-
});
|
|
306
|
-
`);
|
|
307
|
-
} else {
|
|
308
|
-
console.info(`
|
|
309
|
-
module.exports = {
|
|
310
|
-
...,
|
|
311
|
-
plugins: [..., ${chalk.yellow.bold(`${pluginName}(), ${bffPluginName}()`)}],
|
|
312
|
-
};
|
|
313
|
-
`);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
generator.logger.debug(`🌟 [End Run BFF Generator]`);
|
|
319
|
-
};
|