@modern-js/server-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 +6 -6
  3. package/src/index.ts +131 -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", ".ejs", ".mjs"];
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 canUseYarn3() {
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 = canUseYarn3;
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 canUseYarn3() {
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 = canUseYarn3;
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 = canUseYarn3;
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 canUseYarn3() {
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 = canUseYarn3;
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 canUseYarn3() {
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 canUseYarn3()) {
76763
+ } else if (yield canUseYarn2()) {
76743
76764
  const params = ["install", "--production", "--silent", "--ignore-scripts"];
76744
76765
  if (registryUrl) {
76745
76766
  params.push(`--registry=${registryUrl}`);
@@ -137434,15 +137455,6 @@ function getPackageVersion(packageName, registry2) {
137434
137455
  spinner.stop();
137435
137456
  return stripAnsi(result.stdout);
137436
137457
  }
137437
- if (yield (0, import_utils3.canUseYarn)()) {
137438
- const args = ["info", packageName, "version", "--silent"];
137439
- if (registry2) {
137440
- args.push(`--registry=${registry2}`);
137441
- }
137442
- const result = yield (0, import_utils3.execa)("yarn", args);
137443
- spinner.stop();
137444
- return stripAnsi(result.stdout);
137445
- }
137446
137458
  if (yield (0, import_utils3.canUseNpm)()) {
137447
137459
  const args = ["view", packageName, "version"];
137448
137460
  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
  "types": "./src/index.ts",
17
17
  "main": "./dist/index.js",
@@ -28,11 +28,11 @@
28
28
  "@types/node": "^14",
29
29
  "jest": "^27",
30
30
  "typescript": "^4",
31
- "@modern-js/generator-common": "3.0.4",
32
- "@modern-js/generator-utils": "3.0.4",
33
- "@modern-js/dependence-generator": "3.0.4",
34
- "@scripts/build": "2.2.0",
35
- "@scripts/jest-config": "2.2.0"
31
+ "@modern-js/generator-utils": "3.0.5",
32
+ "@modern-js/generator-common": "3.0.5",
33
+ "@scripts/jest-config": "2.3.0",
34
+ "@scripts/build": "2.3.0",
35
+ "@modern-js/dependence-generator": "3.0.5"
36
36
  },
37
37
  "sideEffects": false,
38
38
  "publishConfig": {
package/src/index.ts ADDED
@@ -0,0 +1,131 @@
1
+ import path from 'path';
2
+ import {
3
+ fs,
4
+ getModernPluginVersion,
5
+ isTsProject,
6
+ readTsConfigByFile,
7
+ } from '@modern-js/generator-utils';
8
+ import { GeneratorContext, GeneratorCore } from '@modern-js/codesmith';
9
+ import { AppAPI } from '@modern-js/codesmith-api-app';
10
+ import { JsonAPI } from '@modern-js/codesmith-api-json';
11
+ import {
12
+ DependenceGenerator,
13
+ i18n,
14
+ Language,
15
+ Solution,
16
+ } from '@modern-js/generator-common';
17
+
18
+ const getGeneratorPath = (generator: string, distTag: string) => {
19
+ if (process.env.CODESMITH_ENV === 'development') {
20
+ return path.dirname(require.resolve(generator));
21
+ } else if (distTag) {
22
+ return `${generator}@${distTag}`;
23
+ }
24
+ return generator;
25
+ };
26
+
27
+ function isEmptyServerDir(serverDir: string) {
28
+ const files = fs.readdirSync(serverDir);
29
+ if (files.length === 0) {
30
+ return true;
31
+ }
32
+ return files.every(file => {
33
+ if (fs.statSync(path.join(serverDir, file)).isDirectory()) {
34
+ const childFiles = fs.readdirSync(path.join(serverDir, file));
35
+ return childFiles.length === 0;
36
+ }
37
+ return false;
38
+ });
39
+ }
40
+
41
+ const handleTemplateFile = async (
42
+ context: GeneratorContext,
43
+ generator: GeneratorCore,
44
+ appApi: AppAPI,
45
+ ) => {
46
+ const jsonAPI = new JsonAPI(generator);
47
+
48
+ const appDir = context.materials.default.basePath;
49
+ const serverDir = path.join(appDir, 'server');
50
+
51
+ if (fs.existsSync(serverDir) && !isEmptyServerDir(serverDir)) {
52
+ const files = fs.readdirSync('server');
53
+ if (files.length > 0) {
54
+ generator.logger.warn("'server' is already exist");
55
+ // eslint-disable-next-line no-process-exit
56
+ process.exit(1);
57
+ }
58
+ }
59
+
60
+ const language = isTsProject(appDir) ? Language.TS : Language.JS;
61
+ const serverPlugin = '@modern-js/plugin-server';
62
+ await appApi.runSubGenerator(
63
+ getGeneratorPath(DependenceGenerator, context.config.distTag),
64
+ undefined,
65
+ {
66
+ ...context.config,
67
+ devDependencies: {
68
+ ...(context.config.devDependencies || {}),
69
+ [serverPlugin]: await getModernPluginVersion(
70
+ Solution.MWA,
71
+ serverPlugin,
72
+ {
73
+ registry: context.config.registry,
74
+ distTag: context.config.distTag,
75
+ cwd: context.materials.default.basePath,
76
+ },
77
+ ),
78
+ 'ts-node': '~10.8.1',
79
+ 'tsconfig-paths': '~3.14.1',
80
+ },
81
+ },
82
+ );
83
+
84
+ await appApi.forgeTemplate(
85
+ `templates/base-template/${language}/**/*`,
86
+ undefined,
87
+ resourceKey =>
88
+ resourceKey
89
+ .replace(`templates/base-template/${language}/`, '')
90
+ .replace('.handlebars', ''),
91
+ );
92
+
93
+ if (language === Language.TS) {
94
+ const tsconfigJSON = readTsConfigByFile(path.join(appDir, 'tsconfig.json'));
95
+
96
+ if (!(tsconfigJSON.include || []).includes('server')) {
97
+ await jsonAPI.update(
98
+ context.materials.default.get(path.join(appDir, 'tsconfig.json')),
99
+ {
100
+ query: {},
101
+ update: {
102
+ $set: {
103
+ include: [...(tsconfigJSON.include || []), 'server'],
104
+ },
105
+ },
106
+ },
107
+ );
108
+ }
109
+ }
110
+ };
111
+
112
+ export default async (context: GeneratorContext, generator: GeneratorCore) => {
113
+ const appApi = new AppAPI(context, generator);
114
+
115
+ const { locale } = context.config;
116
+ i18n.changeLanguage({ locale });
117
+ appApi.i18n.changeLanguage({ locale });
118
+
119
+ if (!(await appApi.checkEnvironment())) {
120
+ // eslint-disable-next-line no-process-exit
121
+ process.exit(1);
122
+ }
123
+
124
+ generator.logger.debug(`start run @modern-js/server-generator`);
125
+ generator.logger.debug(`context=${JSON.stringify(context)}`);
126
+ generator.logger.debug(`context.data=${JSON.stringify(context.data)}`);
127
+
128
+ await handleTemplateFile(context, generator, appApi);
129
+
130
+ generator.logger.debug(`forge @modern-js/server-generator succeed `);
131
+ };