@modern-js/repo-generator 3.0.3 → 3.0.5

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.
Files changed (3) hide show
  1. package/dist/index.js +1905 -1686
  2. package/package.json +11 -11
  3. package/src/index.ts +156 -0
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "3.0.3",
14
+ "version": "3.0.5",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "main": "./dist/index.js",
17
17
  "files": [
@@ -26,16 +26,16 @@
26
26
  "@types/node": "^14",
27
27
  "jest": "^27",
28
28
  "typescript": "^4",
29
- "@modern-js/generator-common": "3.0.3",
30
- "@modern-js/generator-utils": "3.0.3",
31
- "@modern-js/module-generator": "3.0.3",
32
- "@modern-js/mwa-generator": "3.0.3",
33
- "@modern-js/monorepo-generator": "3.0.3",
34
- "@modern-js/utils": "2.1.0",
35
- "@modern-js/generator-plugin": "3.0.3",
36
- "@modern-js/base-generator": "3.0.3",
37
- "@scripts/jest-config": "2.1.0",
38
- "@scripts/build": "2.1.0"
29
+ "@modern-js/generator-utils": "3.0.5",
30
+ "@modern-js/module-generator": "3.0.5",
31
+ "@modern-js/monorepo-generator": "3.0.5",
32
+ "@modern-js/generator-plugin": "3.0.5",
33
+ "@modern-js/generator-common": "3.0.5",
34
+ "@modern-js/base-generator": "3.0.5",
35
+ "@modern-js/mwa-generator": "3.0.5",
36
+ "@modern-js/utils": "2.3.0",
37
+ "@scripts/jest-config": "2.3.0",
38
+ "@scripts/build": "2.3.0"
39
39
  },
40
40
  "sideEffects": false,
41
41
  "publishConfig": {
package/src/index.ts ADDED
@@ -0,0 +1,156 @@
1
+ import path from 'path';
2
+ import { merge } from '@modern-js/utils/lodash';
3
+ import { GeneratorContext, GeneratorCore } from '@modern-js/codesmith';
4
+ import { AppAPI } from '@modern-js/codesmith-api-app';
5
+ import {
6
+ i18n,
7
+ getSolutionSchema,
8
+ SolutionGenerator,
9
+ Solution,
10
+ SolutionDefaultConfig,
11
+ BaseGenerator,
12
+ getMonorepoNewActionSchema,
13
+ SubSolution,
14
+ SubSolutionGenerator,
15
+ MonorepoNewActionConfig,
16
+ getSolutionNameFromSubSolution,
17
+ getScenesSchema,
18
+ } from '@modern-js/generator-common';
19
+ import { GeneratorPlugin } from '@modern-js/generator-plugin';
20
+
21
+ const getGeneratorPath = (generator: string, distTag: string) => {
22
+ if (process.env.CODESMITH_ENV === 'development') {
23
+ return path.dirname(require.resolve(generator));
24
+ } else if (distTag) {
25
+ return `${generator}@${distTag}`;
26
+ }
27
+ return generator;
28
+ };
29
+
30
+ const mergeDefaultConfig = (context: GeneratorContext) => {
31
+ const { defaultSolution } = context.config;
32
+
33
+ if (defaultSolution) {
34
+ merge(
35
+ context.config,
36
+ { solution: defaultSolution },
37
+ SolutionDefaultConfig[defaultSolution as Solution],
38
+ );
39
+ }
40
+ };
41
+
42
+ const getNeedRunPlugin = (
43
+ context: GeneratorContext,
44
+ generatorPlugin?: GeneratorPlugin,
45
+ ): boolean => {
46
+ if (!generatorPlugin) {
47
+ return false;
48
+ }
49
+ const { extendPlugin, customPlugin } = generatorPlugin;
50
+ const { isMonorepo, solution, scenes } = context.config;
51
+ const pluginSolution = isMonorepo
52
+ ? getSolutionNameFromSubSolution(solution)
53
+ : solution;
54
+ if (!scenes || scenes === pluginSolution) {
55
+ return (
56
+ extendPlugin?.[pluginSolution] && extendPlugin[pluginSolution].length > 0
57
+ );
58
+ }
59
+ return Boolean(
60
+ customPlugin[pluginSolution]?.find(plugin => plugin.key === scenes),
61
+ );
62
+ };
63
+
64
+ const handleTemplateFile = async (
65
+ context: GeneratorContext,
66
+ generator: GeneratorCore,
67
+ appApi: AppAPI,
68
+ generatorPlugin?: GeneratorPlugin,
69
+ ) => {
70
+ const { isMonorepo } = context.config;
71
+
72
+ const { solution } = await appApi.getInputBySchemaFunc(
73
+ isMonorepo ? getMonorepoNewActionSchema : getSolutionSchema,
74
+ {
75
+ ...context.config,
76
+ customPlugin: generatorPlugin?.customPlugin,
77
+ },
78
+ );
79
+
80
+ await appApi.getInputBySchemaFunc(getScenesSchema, context.config);
81
+
82
+ const solutionGenerator =
83
+ // eslint-disable-next-line no-nested-ternary
84
+ solution === 'custom'
85
+ ? BaseGenerator
86
+ : isMonorepo
87
+ ? SubSolutionGenerator[solution as SubSolution]
88
+ : SolutionGenerator[solution as Solution];
89
+
90
+ if (!solution || !solutionGenerator) {
91
+ generator.logger.error('solution is not valid ');
92
+ }
93
+
94
+ await appApi.runSubGenerator(
95
+ getGeneratorPath(solutionGenerator, context.config.distTag),
96
+ undefined,
97
+ {
98
+ ...(isMonorepo
99
+ ? MonorepoNewActionConfig[solution as SubSolution] || {}
100
+ : {}),
101
+ ...context.config,
102
+ hasPlugin: getNeedRunPlugin(context, generatorPlugin),
103
+ generatorPlugin,
104
+ },
105
+ );
106
+ };
107
+
108
+ const handlePlugin = async (
109
+ context: GeneratorContext,
110
+ generator: GeneratorCore,
111
+ ) => {
112
+ const { plugins, registry, locale } = context.config;
113
+ const generatorPlugin = new GeneratorPlugin(
114
+ generator.logger,
115
+ generator.event,
116
+ locale,
117
+ );
118
+ await generatorPlugin.setupPlugin(plugins, registry);
119
+ return generatorPlugin;
120
+ };
121
+
122
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
123
+ process.setMaxListeners(20);
124
+
125
+ const appApi = new AppAPI(context, generator);
126
+
127
+ const { locale } = context.config;
128
+ i18n.changeLanguage({ locale });
129
+ appApi.i18n.changeLanguage({ locale });
130
+
131
+ if (!(await appApi.checkEnvironment())) {
132
+ // eslint-disable-next-line no-process-exit
133
+ process.exit(1);
134
+ }
135
+
136
+ generator.logger.debug(`start run @modern-js/repo-generator`);
137
+ generator.logger.debug(`context=${JSON.stringify(context)}`);
138
+ generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
139
+
140
+ mergeDefaultConfig(context);
141
+
142
+ let generatorPlugin;
143
+ if (context.config.plugins?.length > 0) {
144
+ generatorPlugin = await handlePlugin(context, generator);
145
+ }
146
+
147
+ try {
148
+ await handleTemplateFile(context, generator, appApi, generatorPlugin);
149
+ } catch (e) {
150
+ generator.logger.error(e);
151
+ // eslint-disable-next-line no-process-exit
152
+ process.exit(1);
153
+ }
154
+
155
+ generator.logger.debug(`forge @modern-js/repo-generator succeed `);
156
+ };