@modern-js/mwa-generator 2.4.15 → 2.4.17
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/package.json +11 -11
- package/src/index.ts +287 -0
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "2.4.
|
|
14
|
+
"version": "2.4.17",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"files": [
|
|
@@ -27,16 +27,16 @@
|
|
|
27
27
|
"@types/node": "^14",
|
|
28
28
|
"jest": "^27",
|
|
29
29
|
"typescript": "^4",
|
|
30
|
-
"@modern-js/base-generator": "2.4.
|
|
31
|
-
"@modern-js/dependence-generator": "2.4.
|
|
32
|
-
"@modern-js/electron-generator": "2.4.
|
|
33
|
-
"@modern-js/entry-generator": "2.4.
|
|
34
|
-
"@modern-js/generator-common": "2.4.
|
|
35
|
-
"@modern-js/generator-utils": "2.4.
|
|
36
|
-
"@modern-js/packages-generator": "2.4.
|
|
37
|
-
"@modern-js/plugin-i18n": "1.22.
|
|
38
|
-
"@scripts/build": "1.22.
|
|
39
|
-
"@scripts/jest-config": "1.22.
|
|
30
|
+
"@modern-js/base-generator": "2.4.17",
|
|
31
|
+
"@modern-js/dependence-generator": "2.4.17",
|
|
32
|
+
"@modern-js/electron-generator": "2.4.17",
|
|
33
|
+
"@modern-js/entry-generator": "2.4.17",
|
|
34
|
+
"@modern-js/generator-common": "2.4.17",
|
|
35
|
+
"@modern-js/generator-utils": "2.4.17",
|
|
36
|
+
"@modern-js/packages-generator": "2.4.17",
|
|
37
|
+
"@modern-js/plugin-i18n": "1.22.6",
|
|
38
|
+
"@scripts/build": "1.22.6",
|
|
39
|
+
"@scripts/jest-config": "1.22.6"
|
|
40
40
|
},
|
|
41
41
|
"sideEffects": false,
|
|
42
42
|
"modernConfig": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { 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
|
+
i18n as commonI18n,
|
|
7
|
+
BaseGenerator,
|
|
8
|
+
Solution,
|
|
9
|
+
MWASchema,
|
|
10
|
+
Language,
|
|
11
|
+
BooleanConfig,
|
|
12
|
+
ClientRoute,
|
|
13
|
+
RunWay,
|
|
14
|
+
EntryGenerator,
|
|
15
|
+
ElectronGenerator,
|
|
16
|
+
PackagesGenerator,
|
|
17
|
+
} from '@modern-js/generator-common';
|
|
18
|
+
import {
|
|
19
|
+
getMWAProjectPath,
|
|
20
|
+
getAllPackages,
|
|
21
|
+
i18n as utilsI18n,
|
|
22
|
+
validatePackageName,
|
|
23
|
+
validatePackagePath,
|
|
24
|
+
getPackageManagerText,
|
|
25
|
+
getModernVersion,
|
|
26
|
+
} from '@modern-js/generator-utils';
|
|
27
|
+
import { i18n, localeKeys } from './locale';
|
|
28
|
+
|
|
29
|
+
const getGeneratorPath = (generator: string, distTag: string) => {
|
|
30
|
+
if (process.env.CODESMITH_ENV === 'development') {
|
|
31
|
+
return path.dirname(require.resolve(generator));
|
|
32
|
+
} else if (distTag) {
|
|
33
|
+
return `${generator}@${distTag}`;
|
|
34
|
+
}
|
|
35
|
+
return generator;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const handleTemplateFile = async (
|
|
39
|
+
context: GeneratorContext,
|
|
40
|
+
generator: GeneratorCore,
|
|
41
|
+
appApi: AppAPI,
|
|
42
|
+
) => {
|
|
43
|
+
const jsonAPI = new JsonAPI(generator);
|
|
44
|
+
|
|
45
|
+
const { isMonorepoSubProject, isTest, projectDir = '' } = context.config;
|
|
46
|
+
|
|
47
|
+
const { outputPath } = generator;
|
|
48
|
+
|
|
49
|
+
let packages: string[] = [];
|
|
50
|
+
|
|
51
|
+
if (isMonorepoSubProject) {
|
|
52
|
+
try {
|
|
53
|
+
packages = getAllPackages(outputPath);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
generator.logger.debug('get all packages error', e);
|
|
56
|
+
generator.logger.warn(i18n.t(localeKeys.get_packages_error));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { hasPlugin, generatorPlugin, ...extra } = context.config;
|
|
61
|
+
|
|
62
|
+
let schema = MWASchema;
|
|
63
|
+
let inputValue = {};
|
|
64
|
+
|
|
65
|
+
if (hasPlugin) {
|
|
66
|
+
await generatorPlugin.installPlugins(Solution.MWA, extra);
|
|
67
|
+
schema = generatorPlugin.getInputSchema(Solution.MWA);
|
|
68
|
+
inputValue = generatorPlugin.getInputValue();
|
|
69
|
+
context.config.gitCommitMessage =
|
|
70
|
+
generatorPlugin.getGitMessage() || context.config.gitCommitMessage;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const ans = await appApi.getInputBySchema(
|
|
74
|
+
schema,
|
|
75
|
+
{ ...context.config, ...inputValue, isMwa: true, isEmptySrc: true },
|
|
76
|
+
{
|
|
77
|
+
packageName: input =>
|
|
78
|
+
validatePackageName(input as string, packages, {
|
|
79
|
+
isMonorepoSubProject,
|
|
80
|
+
}),
|
|
81
|
+
packagePath: input =>
|
|
82
|
+
validatePackagePath(
|
|
83
|
+
input as string,
|
|
84
|
+
path.join(process.cwd(), projectDir),
|
|
85
|
+
{ isTest, isMwa: true },
|
|
86
|
+
),
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const modernVersion = await getModernVersion(
|
|
91
|
+
Solution.MWA,
|
|
92
|
+
context.config.registry,
|
|
93
|
+
context.config.distTag,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
generator.logger.debug(`inputData=${JSON.stringify(ans)}`, ans);
|
|
97
|
+
|
|
98
|
+
const {
|
|
99
|
+
packageName,
|
|
100
|
+
packagePath,
|
|
101
|
+
language,
|
|
102
|
+
runWay,
|
|
103
|
+
packageManager,
|
|
104
|
+
needModifyMWAConfig,
|
|
105
|
+
clientRoute,
|
|
106
|
+
} = ans;
|
|
107
|
+
|
|
108
|
+
const projectPath = getMWAProjectPath(
|
|
109
|
+
packagePath as string,
|
|
110
|
+
isMonorepoSubProject,
|
|
111
|
+
isTest,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const dirname = path.basename(generator.outputPath);
|
|
115
|
+
|
|
116
|
+
await appApi.runSubGenerator(
|
|
117
|
+
getGeneratorPath(BaseGenerator, context.config.distTag),
|
|
118
|
+
undefined,
|
|
119
|
+
{ ...context.config, hasPlugin: false },
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
await appApi.forgeTemplate(
|
|
123
|
+
'templates/base-template/**/*',
|
|
124
|
+
undefined,
|
|
125
|
+
resourceKey =>
|
|
126
|
+
resourceKey
|
|
127
|
+
.replace('templates/base-template/', projectPath)
|
|
128
|
+
.replace('.handlebars', ''),
|
|
129
|
+
{
|
|
130
|
+
name: packageName || dirname,
|
|
131
|
+
packageManager: getPackageManagerText(packageManager),
|
|
132
|
+
isMonorepoSubProject,
|
|
133
|
+
modernVersion,
|
|
134
|
+
},
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
if (language === Language.TS) {
|
|
138
|
+
await jsonAPI.update(
|
|
139
|
+
context.materials.default.get(path.join(projectPath, 'package.json')),
|
|
140
|
+
{
|
|
141
|
+
query: {},
|
|
142
|
+
update: {
|
|
143
|
+
$set: {
|
|
144
|
+
'devDependencies.typescript': '^4',
|
|
145
|
+
'devDependencies.@types/react': '^17',
|
|
146
|
+
'devDependencies.@types/react-dom': '^17',
|
|
147
|
+
'devDependencies.@types/node': '^14',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
await appApi.forgeTemplate(
|
|
154
|
+
'templates/ts-template/**/*',
|
|
155
|
+
undefined,
|
|
156
|
+
resourceKey =>
|
|
157
|
+
resourceKey
|
|
158
|
+
.replace('templates/ts-template/', projectPath)
|
|
159
|
+
.replace('.handlebars', ''),
|
|
160
|
+
);
|
|
161
|
+
} else {
|
|
162
|
+
await appApi.forgeTemplate(
|
|
163
|
+
'templates/js-template/**/*',
|
|
164
|
+
undefined,
|
|
165
|
+
resourceKey =>
|
|
166
|
+
resourceKey
|
|
167
|
+
.replace('templates/js-template/', projectPath)
|
|
168
|
+
.replace('.handlebars', ''),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
await appApi.runSubGenerator(
|
|
173
|
+
getGeneratorPath(EntryGenerator, context.config.distTag),
|
|
174
|
+
`./${projectPath}`,
|
|
175
|
+
{
|
|
176
|
+
...context.config,
|
|
177
|
+
clientRoute:
|
|
178
|
+
needModifyMWAConfig === BooleanConfig.NO
|
|
179
|
+
? ClientRoute.SelfControlRoute
|
|
180
|
+
: clientRoute,
|
|
181
|
+
isSubGenerator: true,
|
|
182
|
+
},
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
if (runWay === RunWay.Electron) {
|
|
186
|
+
await appApi.runSubGenerator(
|
|
187
|
+
getGeneratorPath(ElectronGenerator, context.config.distTag),
|
|
188
|
+
undefined,
|
|
189
|
+
{
|
|
190
|
+
...context.config,
|
|
191
|
+
projectPath,
|
|
192
|
+
isSubGenerator: true,
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (isMonorepoSubProject) {
|
|
198
|
+
await appApi.updateWorkspace({
|
|
199
|
+
name: packagePath as string,
|
|
200
|
+
path: projectPath,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const { packagesInfo } = context.config;
|
|
205
|
+
if (packagesInfo && Object.keys(packagesInfo).length > 0) {
|
|
206
|
+
await appApi.runSubGenerator(
|
|
207
|
+
getGeneratorPath(PackagesGenerator, context.config.distTag),
|
|
208
|
+
undefined,
|
|
209
|
+
context.config,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { projectPath, isElectron: runWay === RunWay.Electron };
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
|
217
|
+
const appApi = new AppAPI(context, generator);
|
|
218
|
+
|
|
219
|
+
const { locale, successInfo } = context.config;
|
|
220
|
+
commonI18n.changeLanguage({ locale });
|
|
221
|
+
utilsI18n.changeLanguage({ locale });
|
|
222
|
+
appApi.i18n.changeLanguage({ locale });
|
|
223
|
+
i18n.changeLanguage({ locale });
|
|
224
|
+
|
|
225
|
+
if (!(await appApi.checkEnvironment())) {
|
|
226
|
+
// eslint-disable-next-line no-process-exit
|
|
227
|
+
process.exit(1);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
generator.logger.debug(`start run @modern-js/mwa-generator`);
|
|
231
|
+
generator.logger.debug(`context=${JSON.stringify(context)}`);
|
|
232
|
+
generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
|
|
233
|
+
|
|
234
|
+
let projectPath = '';
|
|
235
|
+
let isElectron = false;
|
|
236
|
+
try {
|
|
237
|
+
({ projectPath, isElectron } = await handleTemplateFile(
|
|
238
|
+
context,
|
|
239
|
+
generator,
|
|
240
|
+
appApi,
|
|
241
|
+
));
|
|
242
|
+
} catch (e) {
|
|
243
|
+
generator.logger.error(e);
|
|
244
|
+
// eslint-disable-next-line no-process-exit
|
|
245
|
+
process.exit(1);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (context.handleForged) {
|
|
249
|
+
await context.handleForged(
|
|
250
|
+
Solution.MWA,
|
|
251
|
+
context,
|
|
252
|
+
context.config.hasPlugin,
|
|
253
|
+
projectPath,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
await appApi.runGitAndInstall(context.config.gitCommitMessage);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
generator.logger.error(e);
|
|
261
|
+
// eslint-disable-next-line no-process-exit
|
|
262
|
+
process.exit(1);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const { packageManager } = context.config;
|
|
266
|
+
|
|
267
|
+
if (successInfo) {
|
|
268
|
+
appApi.showSuccessInfo(successInfo);
|
|
269
|
+
} else if (isElectron) {
|
|
270
|
+
appApi.showSuccessInfo(
|
|
271
|
+
`${i18n.t(localeKeys.success, {
|
|
272
|
+
packageManager: getPackageManagerText(packageManager),
|
|
273
|
+
})}
|
|
274
|
+
${i18n.t(localeKeys.electron.success, {
|
|
275
|
+
packageManager: getPackageManagerText(packageManager),
|
|
276
|
+
})}`,
|
|
277
|
+
);
|
|
278
|
+
} else {
|
|
279
|
+
appApi.showSuccessInfo(
|
|
280
|
+
i18n.t(localeKeys.success, {
|
|
281
|
+
packageManager: getPackageManagerText(packageManager),
|
|
282
|
+
}),
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
generator.logger.debug(`forge @modern-js/mwa-generator succeed `);
|
|
287
|
+
};
|