@modern-js/monorepo-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.
Files changed (3) hide show
  1. package/dist/index.js +31 -19
  2. package/package.json +9 -9
  3. package/src/index.ts +194 -0
package/dist/index.js CHANGED
@@ -35785,7 +35785,7 @@ var require_nodeEnv = __commonJS({
35785
35785
  });
35786
35786
  }
35787
35787
  exports.canUseNpm = canUseNpm2;
35788
- function canUseYarn3() {
35788
+ function canUseYarn2() {
35789
35789
  return __async(this, null, function* () {
35790
35790
  try {
35791
35791
  yield (0, compiled_1.execa)("yarn", ["--version"], { env: process.env });
@@ -35795,7 +35795,7 @@ var require_nodeEnv = __commonJS({
35795
35795
  }
35796
35796
  });
35797
35797
  }
35798
- exports.canUseYarn = canUseYarn3;
35798
+ exports.canUseYarn = canUseYarn2;
35799
35799
  function canUsePnpm3() {
35800
35800
  return __async(this, null, function* () {
35801
35801
  try {
@@ -37196,14 +37196,14 @@ var require_packageManager = __commonJS({
37196
37196
  value: true
37197
37197
  });
37198
37198
  exports.canUsePnpm = canUsePnpm3;
37199
- exports.canUseYarn = canUseYarn3;
37199
+ exports.canUseYarn = canUseYarn2;
37200
37200
  exports.runInstall = runInstall;
37201
37201
  var _path = _interopRequireDefault(require("path"));
37202
37202
  var _utils = require_dist();
37203
37203
  function _interopRequireDefault(obj) {
37204
37204
  return obj && obj.__esModule ? obj : { default: obj };
37205
37205
  }
37206
- function canUseYarn3() {
37206
+ function canUseYarn2() {
37207
37207
  return __async(this, null, function* () {
37208
37208
  try {
37209
37209
  yield (0, _utils.execa)("yarn", ["--version"], {
@@ -37250,7 +37250,7 @@ var require_packageManager = __commonJS({
37250
37250
  params.push(`--registry=${registryUrl}`);
37251
37251
  }
37252
37252
  yield (0, _utils.execa)("pnpm", params, options);
37253
- } else if (yield canUseYarn3()) {
37253
+ } else if (yield canUseYarn2()) {
37254
37254
  const params = ["install", "--production", "--silent", "--ignore-scripts"];
37255
37255
  if (registryUrl) {
37256
37256
  params.push(`--registry=${registryUrl}`);
@@ -53325,7 +53325,7 @@ var require_env = __commonJS({
53325
53325
  exports.canUseNpm = canUseNpm2;
53326
53326
  exports.canUseNvm = canUseNvm;
53327
53327
  exports.canUsePnpm = canUsePnpm3;
53328
- exports.canUseYarn = canUseYarn3;
53328
+ exports.canUseYarn = canUseYarn2;
53329
53329
  var _utils = require_dist();
53330
53330
  function canUseNvm() {
53331
53331
  return __async(this, null, function* () {
@@ -53352,7 +53352,7 @@ var require_env = __commonJS({
53352
53352
  }
53353
53353
  });
53354
53354
  }
53355
- function canUseYarn3() {
53355
+ function canUseYarn2() {
53356
53356
  return __async(this, null, function* () {
53357
53357
  try {
53358
53358
  yield (0, _utils.execa)("yarn", ["--version"], {
@@ -134820,6 +134820,26 @@ var require_compiled2 = __commonJS({
134820
134820
  }
134821
134821
  });
134822
134822
 
134823
+ // ../../../toolkit/utils/dist/commands.js
134824
+ var require_commands = __commonJS({
134825
+ "../../../toolkit/utils/dist/commands.js"(exports) {
134826
+ "use strict";
134827
+ Object.defineProperty(exports, "__esModule", { value: true });
134828
+ exports.isDevCommand = exports.getCommand = void 0;
134829
+ var getCommand = () => {
134830
+ const args = process.argv.slice(2);
134831
+ const command = args[0];
134832
+ return command;
134833
+ };
134834
+ exports.getCommand = getCommand;
134835
+ var isDevCommand = () => {
134836
+ const command = (0, exports.getCommand)();
134837
+ return command === "dev" || command === "start";
134838
+ };
134839
+ exports.isDevCommand = isDevCommand;
134840
+ }
134841
+ });
134842
+
134823
134843
  // ../../../toolkit/utils/dist/format.js
134824
134844
  var require_format2 = __commonJS({
134825
134845
  "../../../toolkit/utils/dist/format.js"(exports) {
@@ -135394,7 +135414,7 @@ var require_constants6 = __commonJS({
135394
135414
  exports.SERVER_DIR = "server";
135395
135415
  exports.SHARED_DIR = "shared";
135396
135416
  exports.CONFIG_CACHE_DIR = "./node_modules/.cache/node-bundle-require";
135397
- exports.CONFIG_FILE_EXTENSIONS = [".js", ".ts", ".ejs", ".mjs"];
135417
+ exports.CONFIG_FILE_EXTENSIONS = [".js", ".ts", ".mjs"];
135398
135418
  exports.OUTPUT_CONFIG_FILE = "modern.config.json";
135399
135419
  exports.DEFAULT_SERVER_CONFIG = "modern.server-runtime.config";
135400
135420
  exports.ROUTE_MINIFEST_FILE = "routes-manifest.json";
@@ -135885,7 +135905,7 @@ var require_nodeEnv2 = __commonJS({
135885
135905
  });
135886
135906
  }
135887
135907
  exports.canUseNpm = canUseNpm2;
135888
- function canUseYarn3() {
135908
+ function canUseYarn2() {
135889
135909
  return __async(this, null, function* () {
135890
135910
  try {
135891
135911
  yield (0, compiled_1.execa)("yarn", ["--version"], { env: process.env });
@@ -135895,7 +135915,7 @@ var require_nodeEnv2 = __commonJS({
135895
135915
  }
135896
135916
  });
135897
135917
  }
135898
- exports.canUseYarn = canUseYarn3;
135918
+ exports.canUseYarn = canUseYarn2;
135899
135919
  function canUsePnpm3() {
135900
135920
  return __async(this, null, function* () {
135901
135921
  try {
@@ -136974,6 +136994,7 @@ var require_dist3 = __commonJS({
136974
136994
  };
136975
136995
  Object.defineProperty(exports, "__esModule", { value: true });
136976
136996
  __exportStar(require_compiled2(), exports);
136997
+ __exportStar(require_commands(), exports);
136977
136998
  __exportStar(require_format2(), exports);
136978
136999
  __exportStar(require_FileSizeReporter2(), exports);
136979
137000
  __exportStar(require_printBuildError2(), exports);
@@ -137403,15 +137424,6 @@ function getPackageVersion(packageName, registry2) {
137403
137424
  spinner.stop();
137404
137425
  return stripAnsi2(result.stdout);
137405
137426
  }
137406
- if (yield (0, import_utils2.canUseYarn)()) {
137407
- const args = ["info", packageName, "version", "--silent"];
137408
- if (registry2) {
137409
- args.push(`--registry=${registry2}`);
137410
- }
137411
- const result = yield (0, import_utils2.execa)("yarn", args);
137412
- spinner.stop();
137413
- return stripAnsi2(result.stdout);
137414
- }
137415
137427
  if (yield (0, import_utils2.canUseNpm)()) {
137416
137428
  const args = ["view", packageName, "version"];
137417
137429
  if (registry2) {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "3.0.4",
14
+ "version": "3.0.5",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "main": "./dist/index.js",
17
17
  "files": [
@@ -27,14 +27,14 @@
27
27
  "@types/node": "^14",
28
28
  "jest": "^27",
29
29
  "typescript": "^4",
30
- "@modern-js/generator-utils": "3.0.4",
31
- "@modern-js/generator-common": "3.0.4",
32
- "@modern-js/packages-generator": "3.0.4",
33
- "@modern-js/plugin-i18n": "2.2.0",
34
- "@modern-js/changeset-generator": "3.0.4",
35
- "@modern-js/base-generator": "3.0.4",
36
- "@scripts/build": "2.2.0",
37
- "@scripts/jest-config": "2.2.0"
30
+ "@modern-js/generator-common": "3.0.5",
31
+ "@modern-js/changeset-generator": "3.0.5",
32
+ "@modern-js/plugin-i18n": "2.3.0",
33
+ "@modern-js/packages-generator": "3.0.5",
34
+ "@modern-js/generator-utils": "3.0.5",
35
+ "@modern-js/base-generator": "3.0.5",
36
+ "@scripts/jest-config": "2.3.0",
37
+ "@scripts/build": "2.3.0"
38
38
  },
39
39
  "sideEffects": false,
40
40
  "publishConfig": {
package/src/index.ts ADDED
@@ -0,0 +1,194 @@
1
+ import path from 'path';
2
+ import {
3
+ canUsePnpm,
4
+ canUseYarn,
5
+ GeneratorContext,
6
+ GeneratorCore,
7
+ } from '@modern-js/codesmith';
8
+ import { AppAPI } from '@modern-js/codesmith-api-app';
9
+ import { JsonAPI } from '@modern-js/codesmith-api-json';
10
+ import {
11
+ i18n as commonI18n,
12
+ BaseGenerator,
13
+ Solution,
14
+ getMonorepoSchema,
15
+ PackageManager,
16
+ ChangesetGenerator,
17
+ PackagesGenerator,
18
+ } from '@modern-js/generator-common';
19
+ import {
20
+ getPackageManagerText,
21
+ getModernVersion,
22
+ } from '@modern-js/generator-utils';
23
+ import { i18n, localeKeys } from './locale';
24
+
25
+ const getGeneratorPath = (generator: string, distTag: string) => {
26
+ if (process.env.CODESMITH_ENV === 'development') {
27
+ return path.dirname(require.resolve(generator));
28
+ } else if (distTag) {
29
+ return `${generator}@${distTag}`;
30
+ }
31
+ return generator;
32
+ };
33
+
34
+ export const handleTemplateFile = async (
35
+ context: GeneratorContext,
36
+ generator: GeneratorCore,
37
+ appApi: AppAPI,
38
+ ) => {
39
+ const { hasPlugin, generatorPlugin, ...extra } = context.config;
40
+
41
+ let ans: Record<string, unknown> = {};
42
+
43
+ if (hasPlugin) {
44
+ await generatorPlugin.installPlugins(Solution.Monorepo, extra);
45
+ const schema = generatorPlugin.getInputSchema();
46
+ const inputValue = generatorPlugin.getInputValue();
47
+ context.config.gitCommitMessage =
48
+ generatorPlugin.getGitMessage() || context.config.gitCommitMessage;
49
+ ans = await appApi.getInputBySchema(
50
+ schema,
51
+ 'formily',
52
+ {
53
+ ...context.config,
54
+ ...inputValue,
55
+ isMonorepo: true,
56
+ },
57
+ {},
58
+ {},
59
+ );
60
+ } else {
61
+ ans = await appApi.getInputBySchemaFunc(getMonorepoSchema, {
62
+ ...context.config,
63
+ isMonorepo: true,
64
+ });
65
+ }
66
+
67
+ const modernVersion = await getModernVersion(
68
+ Solution.Monorepo,
69
+ context.config.registry,
70
+ context.config.distTag,
71
+ );
72
+
73
+ generator.logger.debug(`ans=`, ans);
74
+
75
+ const { packageManager } = ans;
76
+ await appApi.runSubGenerator(
77
+ getGeneratorPath(BaseGenerator, context.config.distTag),
78
+ undefined,
79
+ { ...context.config, hasPlugin: false },
80
+ );
81
+
82
+ await appApi.forgeTemplate(
83
+ 'templates/base-template/**/*',
84
+ undefined,
85
+ (resourceKey: string) =>
86
+ resourceKey
87
+ .replace('templates/base-template/', '')
88
+ .replace('.handlebars', ''),
89
+ { packageManager, modernVersion },
90
+ );
91
+
92
+ if (packageManager === PackageManager.Pnpm) {
93
+ await appApi.forgeTemplate(
94
+ 'templates/pnpm-template/**/*',
95
+ undefined,
96
+ (resourceKey: string) =>
97
+ resourceKey
98
+ .replace('templates/pnpm-template/', '')
99
+ .replace('.handlebars', ''),
100
+ );
101
+ }
102
+
103
+ if (packageManager === PackageManager.Yarn) {
104
+ await appApi.forgeTemplate(
105
+ 'templates/yarn-template/**/*',
106
+ undefined,
107
+ (resourceKey: string) =>
108
+ resourceKey
109
+ .replace('templates/yarn-template/', '')
110
+ .replace('.handlebars', ''),
111
+ );
112
+
113
+ const jsonAPI = new JsonAPI(generator);
114
+
115
+ await jsonAPI.update(
116
+ context.materials.default.get(
117
+ path.join(generator.outputPath, 'package.json'),
118
+ ),
119
+ {
120
+ query: {},
121
+ update: {
122
+ $set: {
123
+ 'scripts.prepare': 'lerna run prepare',
124
+ },
125
+ },
126
+ },
127
+ );
128
+ }
129
+
130
+ await appApi.runSubGenerator(
131
+ getGeneratorPath(ChangesetGenerator, context.config.distTag),
132
+ );
133
+
134
+ const { packagesInfo } = context.config;
135
+ if (packagesInfo && Object.keys(packagesInfo).length > 0) {
136
+ await appApi.runSubGenerator(
137
+ getGeneratorPath(PackagesGenerator, context.config.distTag),
138
+ undefined,
139
+ context.config,
140
+ );
141
+ }
142
+ };
143
+
144
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
145
+ const appApi = new AppAPI(context, generator);
146
+
147
+ const { locale, successInfo } = context.config;
148
+ commonI18n.changeLanguage({ locale });
149
+ i18n.changeLanguage({ locale });
150
+ appApi.i18n.changeLanguage({ locale });
151
+
152
+ // monorepo 场景下必须使用 pnpm 或者 yarn
153
+ if (!(await canUsePnpm()) && !(await canUseYarn())) {
154
+ generator.logger.warn(i18n.t(localeKeys.environment.yarn_pnpm));
155
+ // eslint-disable-next-line no-process-exit
156
+ process.exit(1);
157
+ }
158
+
159
+ generator.logger.debug(`start run @modern-js/monorepo-generator`);
160
+ generator.logger.debug(`context=${JSON.stringify(context)}`);
161
+ generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
162
+
163
+ try {
164
+ await handleTemplateFile(context, generator, appApi);
165
+ } catch (e) {
166
+ generator.logger.error(e);
167
+ // eslint-disable-next-line no-process-exit
168
+ process.exit(1);
169
+ }
170
+
171
+ if (context.handleForged) {
172
+ await context.handleForged(
173
+ Solution.Monorepo,
174
+ context,
175
+ context.config.hasPlugin,
176
+ );
177
+ }
178
+
179
+ try {
180
+ await appApi.runGitAndInstall(context.config.gitCommitMessage);
181
+ } catch (e) {
182
+ // eslint-disable-next-line no-process-exit
183
+ process.exit(1);
184
+ }
185
+
186
+ appApi.showSuccessInfo(
187
+ successInfo ||
188
+ i18n.t(localeKeys.success, {
189
+ packageManager: getPackageManagerText(context.config.packageManager),
190
+ }),
191
+ );
192
+
193
+ generator.logger.debug(`forge @modern-js/monorepo-generator succeed `);
194
+ };