@modern-js/entry-generator 2.4.16 → 2.4.18
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 +0 -4
- package/package.json +10 -10
- package/src/index.ts +217 -0
package/dist/index.js
CHANGED
|
@@ -85066,7 +85066,6 @@ var require_downloadPackage = __commonJSMin((exports) => {
|
|
|
85066
85066
|
if (response.status !== 200) {
|
|
85067
85067
|
throw new Error(`download tar package get bad status code: ${response.status}`);
|
|
85068
85068
|
}
|
|
85069
|
-
const contentLength = Number(response.headers["content-length"]);
|
|
85070
85069
|
const randomId = Math.floor(Math.random() * 1e4);
|
|
85071
85070
|
const tempTgzFilePath = `${_os.default.tmpdir()}/temp-${randomId}.tgz`;
|
|
85072
85071
|
const dest = _utils.fs.createWriteStream(tempTgzFilePath);
|
|
@@ -85079,9 +85078,6 @@ var require_downloadPackage = __commonJSMin((exports) => {
|
|
|
85079
85078
|
resolve();
|
|
85080
85079
|
});
|
|
85081
85080
|
});
|
|
85082
|
-
if ((await _utils.fs.stat(tempTgzFilePath)).size !== contentLength) {
|
|
85083
|
-
throw new Error("download tar package get bad content length");
|
|
85084
|
-
}
|
|
85085
85081
|
await new Promise((resolve, reject) => {
|
|
85086
85082
|
_utils.fs.createReadStream(tempTgzFilePath).pipe(_tar.default.x({
|
|
85087
85083
|
strip: 1,
|
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.18",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"main": "./dist/index.js",
|
|
17
17
|
"files": [
|
|
@@ -20,19 +20,19 @@
|
|
|
20
20
|
],
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@babel/runtime": "^7.18.0",
|
|
23
|
-
"@modern-js/codesmith": "1.6.
|
|
24
|
-
"@modern-js/codesmith-api-app": "1.6.
|
|
25
|
-
"@modern-js/codesmith-api-handlebars": "1.6.
|
|
23
|
+
"@modern-js/codesmith": "1.6.4",
|
|
24
|
+
"@modern-js/codesmith-api-app": "1.6.4",
|
|
25
|
+
"@modern-js/codesmith-api-handlebars": "1.6.4",
|
|
26
26
|
"@types/jest": "^27",
|
|
27
27
|
"@types/node": "^14",
|
|
28
28
|
"jest": "^27",
|
|
29
29
|
"typescript": "^4",
|
|
30
|
-
"@modern-js/generator-
|
|
31
|
-
"@modern-js/generator-
|
|
32
|
-
"@modern-js/plugin-i18n": "1.22.
|
|
33
|
-
"@modern-js/utils": "1.22.
|
|
34
|
-
"@scripts/build": "1.22.
|
|
35
|
-
"@scripts/jest-config": "1.22.
|
|
30
|
+
"@modern-js/generator-utils": "2.4.18",
|
|
31
|
+
"@modern-js/generator-common": "2.4.18",
|
|
32
|
+
"@modern-js/plugin-i18n": "1.22.7",
|
|
33
|
+
"@modern-js/utils": "1.22.7",
|
|
34
|
+
"@scripts/build": "1.22.7",
|
|
35
|
+
"@scripts/jest-config": "1.22.7"
|
|
36
36
|
},
|
|
37
37
|
"sideEffects": false,
|
|
38
38
|
"modernConfig": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { merge } from '@modern-js/utils/lodash';
|
|
3
|
+
import { fs, getPackageObj, isTsProject } from '@modern-js/generator-utils';
|
|
4
|
+
import { GeneratorContext, GeneratorCore } from '@modern-js/codesmith';
|
|
5
|
+
import { AppAPI } from '@modern-js/codesmith-api-app';
|
|
6
|
+
import { renderString } from '@modern-js/codesmith-api-handlebars';
|
|
7
|
+
import {
|
|
8
|
+
i18n as commonI18n,
|
|
9
|
+
EntrySchema,
|
|
10
|
+
ClientRoute,
|
|
11
|
+
} from '@modern-js/generator-common';
|
|
12
|
+
import { isEmptySource, isSingleEntry } from './utils';
|
|
13
|
+
import { i18n, localeKeys } from './locale';
|
|
14
|
+
|
|
15
|
+
const handleInput = async (
|
|
16
|
+
context: GeneratorContext,
|
|
17
|
+
generator: GeneratorCore,
|
|
18
|
+
appApi: AppAPI,
|
|
19
|
+
) => {
|
|
20
|
+
const appDir = context.materials.default.basePath;
|
|
21
|
+
|
|
22
|
+
const { entriesDir } = context.config;
|
|
23
|
+
|
|
24
|
+
await fs.ensureDir(path.join(appDir, entriesDir));
|
|
25
|
+
|
|
26
|
+
const analysisInfo = {
|
|
27
|
+
isEmptySrc: isEmptySource(appDir, entriesDir),
|
|
28
|
+
isSingleEntry: isSingleEntry(appDir, entriesDir),
|
|
29
|
+
isTsProject: isTsProject(appDir),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
generator.logger.debug('analysisInfo:', analysisInfo);
|
|
33
|
+
|
|
34
|
+
const ans = await appApi.getInputBySchema(EntrySchema, {
|
|
35
|
+
...context.config,
|
|
36
|
+
...analysisInfo,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (ans.needModifyMWAConfig === 'no') {
|
|
40
|
+
ans.clientRoute = ClientRoute.SelfControlRoute;
|
|
41
|
+
}
|
|
42
|
+
return ans;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const refactorSingleEntry = async (
|
|
46
|
+
context: GeneratorContext,
|
|
47
|
+
generator: GeneratorCore,
|
|
48
|
+
) => {
|
|
49
|
+
const pkgObj = await getPackageObj(context);
|
|
50
|
+
const pkgName = pkgObj.name;
|
|
51
|
+
|
|
52
|
+
const { entriesDir } = context.config;
|
|
53
|
+
|
|
54
|
+
const oldFilePath = path.join(context.materials.default.basePath, entriesDir);
|
|
55
|
+
const oldFiles = fs
|
|
56
|
+
.readdirSync(oldFilePath)
|
|
57
|
+
.filter(filePath => {
|
|
58
|
+
if (fs.statSync(path.join(oldFilePath, filePath)).isDirectory()) {
|
|
59
|
+
const files = fs.readdirSync(path.join(oldFilePath, filePath));
|
|
60
|
+
return files.length;
|
|
61
|
+
}
|
|
62
|
+
return (
|
|
63
|
+
filePath !== '.eslintrc.json' &&
|
|
64
|
+
filePath !== '.eslintrc.js' &&
|
|
65
|
+
filePath !== 'modern-app-env.d.ts'
|
|
66
|
+
);
|
|
67
|
+
})
|
|
68
|
+
.map(file =>
|
|
69
|
+
path.join(context.materials.default.basePath, entriesDir, file),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// create new dir in entriesDir and move code to that dir
|
|
73
|
+
fs.mkdirpSync(
|
|
74
|
+
path.join(context.materials.default.basePath, entriesDir, pkgName),
|
|
75
|
+
);
|
|
76
|
+
oldFiles.forEach(file => {
|
|
77
|
+
generator.logger.debug(
|
|
78
|
+
`rename ${file} to ${file.replace(
|
|
79
|
+
entriesDir,
|
|
80
|
+
path.join(entriesDir, pkgName),
|
|
81
|
+
)}`,
|
|
82
|
+
);
|
|
83
|
+
fs.renameSync(
|
|
84
|
+
file,
|
|
85
|
+
file.replace(entriesDir, path.join(entriesDir, pkgName)),
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const getTplInfo = (clientRoute: ClientRoute, isTs: boolean) => {
|
|
91
|
+
const fileExtra = isTs ? 'tsx' : 'jsx';
|
|
92
|
+
if (clientRoute === ClientRoute.ConventionalRoute) {
|
|
93
|
+
return {
|
|
94
|
+
name: 'pages-router',
|
|
95
|
+
space: ' ',
|
|
96
|
+
fileExtra,
|
|
97
|
+
entry: `Index.${fileExtra}`,
|
|
98
|
+
css: 'index.css',
|
|
99
|
+
};
|
|
100
|
+
} else if (clientRoute === ClientRoute.SelfControlRoute) {
|
|
101
|
+
return {
|
|
102
|
+
name: 'router',
|
|
103
|
+
space: ' ',
|
|
104
|
+
fileExtra,
|
|
105
|
+
entry: `App.${fileExtra}`,
|
|
106
|
+
css: 'App.css',
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
name: 'base',
|
|
111
|
+
space: ' ',
|
|
112
|
+
fileExtra,
|
|
113
|
+
entry: `App.${fileExtra}`,
|
|
114
|
+
css: 'App.css',
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const getTargetFolder = (
|
|
119
|
+
clientRoute: ClientRoute,
|
|
120
|
+
entriesDir: string,
|
|
121
|
+
entryName: string,
|
|
122
|
+
) => {
|
|
123
|
+
let targetPath = path.join(entriesDir, entryName);
|
|
124
|
+
if (clientRoute === ClientRoute.ConventionalRoute) {
|
|
125
|
+
targetPath = path.join(targetPath, 'pages');
|
|
126
|
+
}
|
|
127
|
+
return targetPath;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const handleTemplateFile = async (
|
|
131
|
+
context: GeneratorContext,
|
|
132
|
+
generator: GeneratorCore,
|
|
133
|
+
appApi: AppAPI,
|
|
134
|
+
) => {
|
|
135
|
+
const ans = await handleInput(context, generator, appApi);
|
|
136
|
+
|
|
137
|
+
if (ans.isSingleEntry) {
|
|
138
|
+
generator.logger.debug(
|
|
139
|
+
'current is single entry, refactoring to multi entry',
|
|
140
|
+
);
|
|
141
|
+
await refactorSingleEntry(context, generator);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const entryName = (ans.name as string) || '';
|
|
145
|
+
const { name, space, fileExtra, entry, css } = getTplInfo(
|
|
146
|
+
ans.clientRoute as ClientRoute,
|
|
147
|
+
ans.isTsProject as boolean,
|
|
148
|
+
);
|
|
149
|
+
const targetFolder = getTargetFolder(
|
|
150
|
+
ans.clientRoute as ClientRoute,
|
|
151
|
+
context.config.entriesDir,
|
|
152
|
+
entryName,
|
|
153
|
+
);
|
|
154
|
+
const sourceFolder = `templates/${name}`;
|
|
155
|
+
|
|
156
|
+
const mainTpl = await context.current?.material
|
|
157
|
+
.get('templates/main.handlebars')
|
|
158
|
+
.value();
|
|
159
|
+
const main = renderString((mainTpl?.content as string | undefined) || '', {
|
|
160
|
+
space,
|
|
161
|
+
entry,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
await appApi.forgeTemplate(
|
|
165
|
+
`${sourceFolder}/*`,
|
|
166
|
+
undefined,
|
|
167
|
+
resourceKey =>
|
|
168
|
+
resourceKey
|
|
169
|
+
.replace(sourceFolder, targetFolder)
|
|
170
|
+
.replace('.handlebars', `.${fileExtra}`),
|
|
171
|
+
{
|
|
172
|
+
main,
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
await appApi.forgeTemplate(
|
|
177
|
+
`templates/main.css`,
|
|
178
|
+
undefined,
|
|
179
|
+
resourceKey =>
|
|
180
|
+
resourceKey
|
|
181
|
+
.replace('templates/main.css', `${targetFolder}/${css}`)
|
|
182
|
+
.replace('.handlebars', ''),
|
|
183
|
+
{
|
|
184
|
+
main,
|
|
185
|
+
},
|
|
186
|
+
);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
|
190
|
+
const appApi = new AppAPI(context, generator);
|
|
191
|
+
|
|
192
|
+
const { locale } = context.config;
|
|
193
|
+
commonI18n.changeLanguage({ locale });
|
|
194
|
+
appApi.i18n.changeLanguage({ locale });
|
|
195
|
+
i18n.changeLanguage({ locale });
|
|
196
|
+
|
|
197
|
+
if (!(await appApi.checkEnvironment())) {
|
|
198
|
+
// eslint-disable-next-line no-process-exit
|
|
199
|
+
process.exit(1);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
generator.logger.debug(`start run @modern-js/entry-generator`);
|
|
203
|
+
generator.logger.debug(`context=${JSON.stringify(context)}`);
|
|
204
|
+
generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
|
|
205
|
+
|
|
206
|
+
merge(context.config, { entriesDir: context.config.entriesDir || 'src' });
|
|
207
|
+
|
|
208
|
+
await handleTemplateFile(context, generator, appApi);
|
|
209
|
+
|
|
210
|
+
if (!context.config.isEmptySrc) {
|
|
211
|
+
appApi.showSuccessInfo(
|
|
212
|
+
i18n.t(localeKeys.success, { name: context.config.name }),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
generator.logger.debug(`forge @modern-js/entry-generator succeed `);
|
|
217
|
+
};
|