@modern-js/entry-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 +1775 -1560
  2. package/package.json +7 -7
  3. package/src/index.ts +136 -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": [
@@ -27,12 +27,12 @@
27
27
  "@types/node": "^14",
28
28
  "jest": "^27",
29
29
  "typescript": "^4",
30
- "@modern-js/generator-utils": "3.0.3",
31
- "@modern-js/plugin-i18n": "2.1.0",
32
- "@modern-js/utils": "2.1.0",
33
- "@modern-js/generator-common": "3.0.3",
34
- "@scripts/build": "2.1.0",
35
- "@scripts/jest-config": "2.1.0"
30
+ "@modern-js/generator-utils": "3.0.5",
31
+ "@modern-js/generator-common": "3.0.5",
32
+ "@modern-js/utils": "2.3.0",
33
+ "@modern-js/plugin-i18n": "2.3.0",
34
+ "@scripts/build": "2.3.0",
35
+ "@scripts/jest-config": "2.3.0"
36
36
  },
37
37
  "sideEffects": false,
38
38
  "publishConfig": {
package/src/index.ts ADDED
@@ -0,0 +1,136 @@
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 {
7
+ i18n as commonI18n,
8
+ getEntrySchema,
9
+ } from '@modern-js/generator-common';
10
+ import { isEmptySource, isSingleEntry } from './utils';
11
+ import { i18n, localeKeys } from './locale';
12
+
13
+ const handleInput = async (
14
+ context: GeneratorContext,
15
+ generator: GeneratorCore,
16
+ appApi: AppAPI,
17
+ ) => {
18
+ const appDir = context.materials.default.basePath;
19
+
20
+ const { entriesDir } = context.config;
21
+
22
+ await fs.ensureDir(path.join(appDir, entriesDir));
23
+
24
+ const analysisInfo = {
25
+ isEmptySrc: isEmptySource(appDir, entriesDir),
26
+ isSingleEntry: isSingleEntry(appDir, entriesDir),
27
+ isTsProject: isTsProject(appDir),
28
+ };
29
+
30
+ generator.logger.debug('analysisInfo:', analysisInfo);
31
+
32
+ const config = { ...context.config, ...analysisInfo };
33
+ const ans = await appApi.getInputBySchemaFunc(getEntrySchema, config);
34
+
35
+ return ans;
36
+ };
37
+
38
+ const refactorSingleEntry = async (
39
+ context: GeneratorContext,
40
+ generator: GeneratorCore,
41
+ ) => {
42
+ const pkgObj = await getPackageObj(context);
43
+ const pkgName = pkgObj.name;
44
+
45
+ const { entriesDir } = context.config;
46
+
47
+ const oldFilePath = path.join(context.materials.default.basePath, entriesDir);
48
+ const oldFiles = fs
49
+ .readdirSync(oldFilePath)
50
+ .filter(filePath => {
51
+ if (fs.statSync(path.join(oldFilePath, filePath)).isDirectory()) {
52
+ const files = fs.readdirSync(path.join(oldFilePath, filePath));
53
+ return files.length;
54
+ }
55
+ return (
56
+ filePath !== '.eslintrc.json' &&
57
+ filePath !== '.eslintrc.js' &&
58
+ filePath !== 'modern-app-env.d.ts'
59
+ );
60
+ })
61
+ .map(file =>
62
+ path.join(context.materials.default.basePath, entriesDir, file),
63
+ );
64
+
65
+ // create new dir in entriesDir and move code to that dir
66
+ fs.mkdirpSync(
67
+ path.join(context.materials.default.basePath, entriesDir, pkgName),
68
+ );
69
+ oldFiles.forEach(file => {
70
+ generator.logger.debug(
71
+ `rename ${file} to ${file.replace(
72
+ entriesDir,
73
+ path.join(entriesDir, pkgName),
74
+ )}`,
75
+ );
76
+ fs.renameSync(
77
+ file,
78
+ file.replace(entriesDir, path.join(entriesDir, pkgName)),
79
+ );
80
+ });
81
+ };
82
+
83
+ export const handleTemplateFile = async (
84
+ context: GeneratorContext,
85
+ generator: GeneratorCore,
86
+ appApi: AppAPI,
87
+ ) => {
88
+ const ans = await handleInput(context, generator, appApi);
89
+
90
+ if (ans.isSingleEntry) {
91
+ generator.logger.debug(
92
+ 'current is single entry, refactoring to multi entry',
93
+ );
94
+ await refactorSingleEntry(context, generator);
95
+ }
96
+
97
+ const entryName = (ans.name as string) || '';
98
+ const fileExtra = ans.isTsProject ? 'tsx' : 'jsx';
99
+ const targetPath = path.join(context.config.entriesDir, entryName);
100
+
101
+ await appApi.forgeTemplate(`templates/**/*`, undefined, resourceKey =>
102
+ resourceKey
103
+ .replace('templates', targetPath)
104
+ .replace('.handlebars', `.${fileExtra}`),
105
+ );
106
+ };
107
+
108
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
109
+ const appApi = new AppAPI(context, generator);
110
+
111
+ const { locale } = context.config;
112
+ commonI18n.changeLanguage({ locale });
113
+ appApi.i18n.changeLanguage({ locale });
114
+ i18n.changeLanguage({ locale });
115
+
116
+ if (!(await appApi.checkEnvironment())) {
117
+ // eslint-disable-next-line no-process-exit
118
+ process.exit(1);
119
+ }
120
+
121
+ generator.logger.debug(`start run @modern-js/entry-generator`);
122
+ generator.logger.debug(`context=${JSON.stringify(context)}`);
123
+ generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
124
+
125
+ merge(context.config, { entriesDir: context.config.entriesDir || 'src' });
126
+
127
+ await handleTemplateFile(context, generator, appApi);
128
+
129
+ if (!context.config.isEmptySrc) {
130
+ appApi.showSuccessInfo(
131
+ i18n.t(localeKeys.success, { name: context.config.name }),
132
+ );
133
+ }
134
+
135
+ generator.logger.debug(`forge @modern-js/entry-generator succeed `);
136
+ };