@modern-js/entry-generator 3.0.4 → 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.
- package/dist/index.js +31 -10
- package/package.json +7 -7
- package/src/index.ts +136 -0
package/dist/index.js
CHANGED
|
@@ -34714,6 +34714,26 @@ var require_compiled = __commonJS({
|
|
|
34714
34714
|
}
|
|
34715
34715
|
});
|
|
34716
34716
|
|
|
34717
|
+
// ../../../toolkit/utils/dist/commands.js
|
|
34718
|
+
var require_commands = __commonJS({
|
|
34719
|
+
"../../../toolkit/utils/dist/commands.js"(exports) {
|
|
34720
|
+
"use strict";
|
|
34721
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34722
|
+
exports.isDevCommand = exports.getCommand = void 0;
|
|
34723
|
+
var getCommand = () => {
|
|
34724
|
+
const args = process.argv.slice(2);
|
|
34725
|
+
const command = args[0];
|
|
34726
|
+
return command;
|
|
34727
|
+
};
|
|
34728
|
+
exports.getCommand = getCommand;
|
|
34729
|
+
var isDevCommand = () => {
|
|
34730
|
+
const command = (0, exports.getCommand)();
|
|
34731
|
+
return command === "dev" || command === "start";
|
|
34732
|
+
};
|
|
34733
|
+
exports.isDevCommand = isDevCommand;
|
|
34734
|
+
}
|
|
34735
|
+
});
|
|
34736
|
+
|
|
34717
34737
|
// ../../../toolkit/utils/dist/format.js
|
|
34718
34738
|
var require_format = __commonJS({
|
|
34719
34739
|
"../../../toolkit/utils/dist/format.js"(exports) {
|
|
@@ -35288,7 +35308,7 @@ var require_constants = __commonJS({
|
|
|
35288
35308
|
exports.SERVER_DIR = "server";
|
|
35289
35309
|
exports.SHARED_DIR = "shared";
|
|
35290
35310
|
exports.CONFIG_CACHE_DIR = "./node_modules/.cache/node-bundle-require";
|
|
35291
|
-
exports.CONFIG_FILE_EXTENSIONS = [".js", ".ts", ".
|
|
35311
|
+
exports.CONFIG_FILE_EXTENSIONS = [".js", ".ts", ".mjs"];
|
|
35292
35312
|
exports.OUTPUT_CONFIG_FILE = "modern.config.json";
|
|
35293
35313
|
exports.DEFAULT_SERVER_CONFIG = "modern.server-runtime.config";
|
|
35294
35314
|
exports.ROUTE_MINIFEST_FILE = "routes-manifest.json";
|
|
@@ -35779,7 +35799,7 @@ var require_nodeEnv = __commonJS({
|
|
|
35779
35799
|
});
|
|
35780
35800
|
}
|
|
35781
35801
|
exports.canUseNpm = canUseNpm3;
|
|
35782
|
-
function
|
|
35802
|
+
function canUseYarn2() {
|
|
35783
35803
|
return __async(this, null, function* () {
|
|
35784
35804
|
try {
|
|
35785
35805
|
yield (0, compiled_1.execa)("yarn", ["--version"], { env: process.env });
|
|
@@ -35789,7 +35809,7 @@ var require_nodeEnv = __commonJS({
|
|
|
35789
35809
|
}
|
|
35790
35810
|
});
|
|
35791
35811
|
}
|
|
35792
|
-
exports.canUseYarn =
|
|
35812
|
+
exports.canUseYarn = canUseYarn2;
|
|
35793
35813
|
function canUsePnpm3() {
|
|
35794
35814
|
return __async(this, null, function* () {
|
|
35795
35815
|
try {
|
|
@@ -36868,6 +36888,7 @@ var require_dist = __commonJS({
|
|
|
36868
36888
|
};
|
|
36869
36889
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36870
36890
|
__exportStar(require_compiled(), exports);
|
|
36891
|
+
__exportStar(require_commands(), exports);
|
|
36871
36892
|
__exportStar(require_format(), exports);
|
|
36872
36893
|
__exportStar(require_FileSizeReporter(), exports);
|
|
36873
36894
|
__exportStar(require_printBuildError(), exports);
|
|
@@ -66974,7 +66995,7 @@ var require_nodeEnv2 = __commonJS({
|
|
|
66974
66995
|
});
|
|
66975
66996
|
}
|
|
66976
66997
|
exports.canUseNpm = canUseNpm3;
|
|
66977
|
-
function
|
|
66998
|
+
function canUseYarn2() {
|
|
66978
66999
|
return __async(this, null, function* () {
|
|
66979
67000
|
try {
|
|
66980
67001
|
yield (0, compiled_1.execa)("yarn", ["--version"], { env: process.env });
|
|
@@ -66984,7 +67005,7 @@ var require_nodeEnv2 = __commonJS({
|
|
|
66984
67005
|
}
|
|
66985
67006
|
});
|
|
66986
67007
|
}
|
|
66987
|
-
exports.canUseYarn =
|
|
67008
|
+
exports.canUseYarn = canUseYarn2;
|
|
66988
67009
|
function canUsePnpm3() {
|
|
66989
67010
|
return __async(this, null, function* () {
|
|
66990
67011
|
try {
|
|
@@ -75622,7 +75643,7 @@ var require_env = __commonJS({
|
|
|
75622
75643
|
exports.canUseNpm = canUseNpm3;
|
|
75623
75644
|
exports.canUseNvm = canUseNvm;
|
|
75624
75645
|
exports.canUsePnpm = canUsePnpm3;
|
|
75625
|
-
exports.canUseYarn =
|
|
75646
|
+
exports.canUseYarn = canUseYarn2;
|
|
75626
75647
|
var _utils = require_dist2();
|
|
75627
75648
|
function canUseNvm() {
|
|
75628
75649
|
return __async(this, null, function* () {
|
|
@@ -75649,7 +75670,7 @@ var require_env = __commonJS({
|
|
|
75649
75670
|
}
|
|
75650
75671
|
});
|
|
75651
75672
|
}
|
|
75652
|
-
function
|
|
75673
|
+
function canUseYarn2() {
|
|
75653
75674
|
return __async(this, null, function* () {
|
|
75654
75675
|
try {
|
|
75655
75676
|
yield (0, _utils.execa)("yarn", ["--version"], {
|
|
@@ -76685,14 +76706,14 @@ var require_packageManager = __commonJS({
|
|
|
76685
76706
|
value: true
|
|
76686
76707
|
});
|
|
76687
76708
|
exports.canUsePnpm = canUsePnpm3;
|
|
76688
|
-
exports.canUseYarn =
|
|
76709
|
+
exports.canUseYarn = canUseYarn2;
|
|
76689
76710
|
exports.runInstall = runInstall;
|
|
76690
76711
|
var _path = _interopRequireDefault(require("path"));
|
|
76691
76712
|
var _utils = require_dist2();
|
|
76692
76713
|
function _interopRequireDefault(obj) {
|
|
76693
76714
|
return obj && obj.__esModule ? obj : { default: obj };
|
|
76694
76715
|
}
|
|
76695
|
-
function
|
|
76716
|
+
function canUseYarn2() {
|
|
76696
76717
|
return __async(this, null, function* () {
|
|
76697
76718
|
try {
|
|
76698
76719
|
yield (0, _utils.execa)("yarn", ["--version"], {
|
|
@@ -76739,7 +76760,7 @@ var require_packageManager = __commonJS({
|
|
|
76739
76760
|
params.push(`--registry=${registryUrl}`);
|
|
76740
76761
|
}
|
|
76741
76762
|
yield (0, _utils.execa)("pnpm", params, options);
|
|
76742
|
-
} else if (yield
|
|
76763
|
+
} else if (yield canUseYarn2()) {
|
|
76743
76764
|
const params = ["install", "--production", "--silent", "--ignore-scripts"];
|
|
76744
76765
|
if (registryUrl) {
|
|
76745
76766
|
params.push(`--registry=${registryUrl}`);
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "3.0.
|
|
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.
|
|
31
|
-
"@modern-js/
|
|
32
|
-
"@modern-js/
|
|
33
|
-
"@modern-js/
|
|
34
|
-
"@scripts/build": "2.
|
|
35
|
-
"@scripts/jest-config": "2.
|
|
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
|
+
};
|