@modern-js/generator-utils 3.6.3 → 3.6.4-alpha.0

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/cjs/index.js CHANGED
@@ -29,75 +29,58 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
29
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
30
  var src_exports = {};
31
31
  __export(src_exports, {
32
- canUseNpm: () => import_utils2.canUseNpm,
33
- canUsePnpm: () => import_utils2.canUsePnpm,
34
- canUseYarn: () => import_utils2.canUseYarn,
35
- chalk: () => import_utils2.chalk,
36
- execa: () => import_utils2.execa,
37
- fs: () => import_utils2.fs,
32
+ canUseNpm: () => import_npm.canUseNpm,
33
+ canUsePnpm: () => import_npm.canUsePnpm,
34
+ canUseYarn: () => import_npm.canUseYarn,
35
+ chalk: () => import_chalk.chalk,
36
+ execa: () => import_execa.execa,
37
+ fs: () => import_fs_extra2.fs,
38
38
  getAllPackages: () => getAllPackages,
39
39
  getMWAProjectPath: () => getMWAProjectPath,
40
40
  getModernConfigFile: () => getModernConfigFile,
41
41
  getModernPluginVersion: () => getModernPluginVersion,
42
42
  getModernVersion: () => getModernVersion,
43
43
  getModuleProjectPath: () => getModuleProjectPath,
44
- getPackageManager: () => import_utils2.getPackageManager,
45
44
  getPackageManagerText: () => getPackageManagerText,
46
45
  getPackageObj: () => getPackageObj,
47
46
  getPackageVersion: () => getPackageVersion,
48
47
  i18n: () => import_locale2.i18n,
49
- isReact18: () => import_utils2.isReact18,
50
48
  isTsProject: () => isTsProject,
51
- ora: () => import_utils2.ora,
52
- readTsConfigByFile: () => import_utils2.readTsConfigByFile,
53
- semver: () => import_utils2.semver,
49
+ ora: () => import_ora2.ora,
50
+ semver: () => import_semver.semver,
54
51
  validatePackageName: () => validatePackageName,
55
52
  validatePackagePath: () => validatePackagePath
56
53
  });
57
54
  module.exports = __toCommonJS(src_exports);
58
55
  var import_path = __toESM(require("path"));
56
+ var import_codesmith = require("@modern-js/codesmith");
57
+ var import_fs_extra = require("@modern-js/codesmith-utils/fs-extra");
58
+ var import_ora = require("@modern-js/codesmith-utils/ora");
59
59
  var import_generator_common = require("@modern-js/generator-common");
60
- var import_utils = require("@modern-js/utils");
61
60
  var import_locale = require("./locale");
62
61
  var import_fsExist = require("./utils/fsExist");
62
+ var import_monorepo = require("./utils/monorepo");
63
63
  var import_package = require("./utils/package");
64
- var import_stripAnsi = require("./utils/stripAnsi");
65
64
  __reExport(src_exports, require("./utils"), module.exports);
66
- var import_utils2 = require("@modern-js/utils");
65
+ var import_npm = require("@modern-js/codesmith-utils/npm");
66
+ var import_fs_extra2 = require("@modern-js/codesmith-utils/fs-extra");
67
+ var import_ora2 = require("@modern-js/codesmith-utils/ora");
68
+ var import_semver = require("@modern-js/codesmith-utils/semver");
69
+ var import_execa = require("@modern-js/codesmith-utils/execa");
70
+ var import_chalk = require("@modern-js/codesmith-utils/chalk");
67
71
  var import_locale2 = require("./locale");
68
- async function getPackageVersion(packageName, registry) {
69
- const spinner = (0, import_utils.ora)({
72
+ async function getPackageVersion(packageName, registryUrl) {
73
+ const spinner = (0, import_ora.ora)({
70
74
  text: "Load Generator...",
71
75
  spinner: "runner"
72
76
  }).start();
73
- if (await (0, import_utils.canUsePnpm)()) {
74
- const args = [
75
- "info",
76
- packageName,
77
- "version"
78
- ];
79
- if (registry) {
80
- args.push(`--registry=${registry}`);
81
- }
82
- const result = await (0, import_utils.execa)("pnpm", args);
83
- spinner.stop();
84
- return (0, import_stripAnsi.stripAnsi)(result.stdout);
85
- }
86
- if (await (0, import_utils.canUseNpm)()) {
87
- const args = [
88
- "view",
89
- packageName,
90
- "version"
91
- ];
92
- if (registry) {
93
- args.push(`--registry=${registry}`);
94
- }
95
- const result = await (0, import_utils.execa)("npm", args);
96
- spinner.stop();
97
- return (0, import_stripAnsi.stripAnsi)(result.stdout);
98
- }
77
+ const { name, version: pkgVersion } = (0, import_codesmith.getPackageInfo)(packageName);
78
+ const version = await (0, import_codesmith.getNpmVersion)(name, {
79
+ version: pkgVersion,
80
+ registryUrl
81
+ });
99
82
  spinner.stop();
100
- throw new Error("not found npm, please install npm before");
83
+ return version;
101
84
  }
102
85
  async function getModernVersion(solution, registry, distTag = "latest") {
103
86
  const dep = import_generator_common.SolutionToolsMap[solution];
@@ -127,8 +110,8 @@ async function getModernPluginVersion(solution, packageName, options = {
127
110
  ]
128
111
  }), "../..", "package.json");
129
112
  }
130
- if (import_utils.fs.existsSync(pkgPath)) {
131
- const pkgInfo = import_utils.fs.readJSONSync(pkgPath);
113
+ if (import_fs_extra.fs.existsSync(pkgPath)) {
114
+ const pkgInfo = import_fs_extra.fs.readJSONSync(pkgPath);
132
115
  const modernVersion = pkgInfo.version;
133
116
  if (typeof modernVersion !== "string") {
134
117
  return getLatetPluginVersion();
@@ -145,14 +128,14 @@ function getPackageManagerText(packageManager) {
145
128
  return packageManager === "yarn" ? "yarn" : `${packageManager} run`;
146
129
  }
147
130
  function isTsProject(appDir) {
148
- return import_utils.fs.existsSync(import_path.default.join(appDir, "tsconfig.json"));
131
+ return import_fs_extra.fs.existsSync(import_path.default.join(appDir, "tsconfig.json"));
149
132
  }
150
133
  async function getPackageObj(context) {
151
134
  const pkgStr = (await context.materials.default.get(`package.json`).value()).content;
152
135
  return JSON.parse(pkgStr);
153
136
  }
154
137
  function getAllPackages(appDir) {
155
- const packages = (0, import_utils.getMonorepoPackages)(appDir);
138
+ const packages = (0, import_monorepo.getMonorepoPackages)(appDir);
156
139
  return packages.map((pkg) => pkg.name);
157
140
  }
158
141
  function validatePackageName(value, packages, options) {
@@ -173,7 +156,7 @@ function validatePackagePath(value, projectDir, options) {
173
156
  const { isMwa } = options || {};
174
157
  const dir = isMwa ? "apps" : "packages";
175
158
  const packageDir = import_path.default.resolve(projectDir || "", dir, value);
176
- if (import_utils.fs.existsSync(packageDir)) {
159
+ if (import_fs_extra.fs.existsSync(packageDir)) {
177
160
  return {
178
161
  success: false,
179
162
  error: import_locale.i18n.t(import_locale.localeKeys.packagePath.exit, {
@@ -225,15 +208,12 @@ async function getModernConfigFile(appDir) {
225
208
  getModernPluginVersion,
226
209
  getModernVersion,
227
210
  getModuleProjectPath,
228
- getPackageManager,
229
211
  getPackageManagerText,
230
212
  getPackageObj,
231
213
  getPackageVersion,
232
214
  i18n,
233
- isReact18,
234
215
  isTsProject,
235
216
  ora,
236
- readTsConfigByFile,
237
217
  semver,
238
218
  validatePackageName,
239
219
  validatePackagePath,
@@ -21,10 +21,10 @@ __export(fsExist_exports, {
21
21
  fileExist: () => fileExist
22
22
  });
23
23
  module.exports = __toCommonJS(fsExist_exports);
24
- var import_utils = require("@modern-js/utils");
24
+ var import_fs_extra = require("@modern-js/codesmith-utils/fs-extra");
25
25
  async function fileExist(filePath) {
26
26
  try {
27
- const stat = await import_utils.fs.stat(filePath);
27
+ const stat = await import_fs_extra.fs.stat(filePath);
28
28
  if (stat.isFile()) {
29
29
  return true;
30
30
  }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var get_exports = {};
30
+ __export(get_exports, {
31
+ readTsConfig: () => readTsConfig,
32
+ readTsConfigByFile: () => readTsConfigByFile
33
+ });
34
+ module.exports = __toCommonJS(get_exports);
35
+ var import_path = __toESM(require("path"));
36
+ var import_fs_extra = require("@modern-js/codesmith-utils/fs-extra");
37
+ var import_json5 = __toESM(require("json5"));
38
+ const readTsConfig = (root) => {
39
+ return readTsConfigByFile(import_path.default.resolve(root, "./tsconfig.json"));
40
+ };
41
+ const readTsConfigByFile = (filename) => {
42
+ const content = import_fs_extra.fs.readFileSync(import_path.default.resolve(filename), "utf-8");
43
+ return import_json5.default.parse(content);
44
+ };
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ readTsConfig,
48
+ readTsConfigByFile
49
+ });
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
6
  var __copyProps = (to, from, except, desc) => {
11
7
  if (from && typeof from === "object" || typeof from === "function") {
12
8
  for (let key of __getOwnPropNames(from))
@@ -18,16 +14,17 @@ var __copyProps = (to, from, except, desc) => {
18
14
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
15
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
16
  var utils_exports = {};
21
- __export(utils_exports, {
22
- getGeneratorPath: () => import_getGeneratorPath.getGeneratorPath
23
- });
24
17
  module.exports = __toCommonJS(utils_exports);
25
18
  __reExport(utils_exports, require("./stripAnsi"), module.exports);
26
19
  __reExport(utils_exports, require("./package"), module.exports);
27
- var import_getGeneratorPath = require("./getGeneratorPath");
20
+ __reExport(utils_exports, require("./getGeneratorPath"), module.exports);
21
+ __reExport(utils_exports, require("./get"), module.exports);
22
+ __reExport(utils_exports, require("./is"), module.exports);
28
23
  // Annotate the CommonJS export names for ESM import in node:
29
24
  0 && (module.exports = {
30
- getGeneratorPath,
31
25
  ...require("./stripAnsi"),
32
- ...require("./package")
26
+ ...require("./package"),
27
+ ...require("./getGeneratorPath"),
28
+ ...require("./get"),
29
+ ...require("./is")
33
30
  });
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var is_exports = {};
30
+ __export(is_exports, {
31
+ isReact18: () => isReact18
32
+ });
33
+ module.exports = __toCommonJS(is_exports);
34
+ var import_path = __toESM(require("path"));
35
+ var import_fs_extra = require("@modern-js/codesmith-utils/fs-extra");
36
+ var import_semver = require("@modern-js/codesmith-utils/semver");
37
+ const isReact18 = (cwd = process.cwd()) => {
38
+ const pkgPath = import_path.default.join(cwd, "package.json");
39
+ if (!import_fs_extra.fs.existsSync(pkgPath)) {
40
+ return false;
41
+ }
42
+ const pkgInfo = JSON.parse(import_fs_extra.fs.readFileSync(pkgPath, "utf8"));
43
+ const deps = {
44
+ ...pkgInfo.devDependencies,
45
+ ...pkgInfo.dependencies
46
+ };
47
+ if (typeof deps.react !== "string") {
48
+ return false;
49
+ }
50
+ return import_semver.semver.satisfies(import_semver.semver.minVersion(deps.react), ">=18.0.0");
51
+ };
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ isReact18
55
+ });
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var monorepo_exports = {};
30
+ __export(monorepo_exports, {
31
+ findMonorepoRoot: () => findMonorepoRoot,
32
+ getMonorepoPackages: () => getMonorepoPackages,
33
+ isLerna: () => isLerna,
34
+ isMonorepo: () => isMonorepo,
35
+ isPnpmWorkspaces: () => isPnpmWorkspaces,
36
+ isYarnWorkspaces: () => isYarnWorkspaces
37
+ });
38
+ module.exports = __toCommonJS(monorepo_exports);
39
+ var import_fs = __toESM(require("fs"));
40
+ var import_path = __toESM(require("path"));
41
+ var import_glob = __toESM(require("glob"));
42
+ var import_js_yaml = __toESM(require("js-yaml"));
43
+ const PACKAGE_MAX_DEPTH = 5;
44
+ const WORKSPACE_FILES = {
45
+ YARN: "package.json",
46
+ PNPM: "pnpm-workspace.yaml",
47
+ LERNA: "lerna.json"
48
+ };
49
+ const isLerna = (root) => import_fs.default.existsSync(import_path.default.join(root, WORKSPACE_FILES.LERNA));
50
+ const isYarnWorkspaces = (root) => {
51
+ var _json_workspaces;
52
+ const pkg = import_path.default.join(root, WORKSPACE_FILES.YARN);
53
+ if (!import_fs.default.existsSync(pkg)) {
54
+ return false;
55
+ }
56
+ const json = JSON.parse(import_fs.default.readFileSync(pkg, "utf8"));
57
+ return Boolean((_json_workspaces = json.workspaces) === null || _json_workspaces === void 0 ? void 0 : _json_workspaces.packages);
58
+ };
59
+ const isPnpmWorkspaces = (root) => import_fs.default.existsSync(import_path.default.join(root, WORKSPACE_FILES.PNPM));
60
+ const isMonorepo = (root) => isLerna(root) || isYarnWorkspaces(root) || isPnpmWorkspaces(root);
61
+ const findMonorepoRoot = (appDirectory, maxDepth = PACKAGE_MAX_DEPTH) => {
62
+ let inMonorepo = false;
63
+ let monorepoRoot = appDirectory;
64
+ for (let depth = 0; depth < maxDepth; depth++) {
65
+ if (isMonorepo(appDirectory)) {
66
+ inMonorepo = true;
67
+ break;
68
+ }
69
+ monorepoRoot = import_path.default.dirname(appDirectory);
70
+ }
71
+ return inMonorepo ? monorepoRoot : void 0;
72
+ };
73
+ const getMonorepoPackages = (root) => {
74
+ let packages = [];
75
+ if (isYarnWorkspaces(root)) {
76
+ const json = JSON.parse(import_fs.default.readFileSync(import_path.default.join(root, "package.json"), "utf8"));
77
+ ({ packages } = json.workspaces);
78
+ } else if (isLerna(root)) {
79
+ const json = JSON.parse(import_fs.default.readFileSync(import_path.default.resolve(root, "lerna.json"), "utf8"));
80
+ ({ packages } = json);
81
+ } else {
82
+ ({ packages } = import_js_yaml.default.load(import_fs.default.readFileSync(import_path.default.join(root, WORKSPACE_FILES.PNPM), "utf8")));
83
+ }
84
+ if (packages) {
85
+ return packages.map((name) => (
86
+ // The trailing / ensures only dirs are picked up
87
+ import_glob.default.sync(import_path.default.join(root, `${name}/`), {
88
+ ignore: [
89
+ "**/node_modules/**"
90
+ ]
91
+ })
92
+ )).reduce((acc, val) => acc.concat(val), []).filter((filepath) => import_fs.default.existsSync(import_path.default.resolve(filepath, "package.json"))).map((filepath) => ({
93
+ path: filepath,
94
+ name: JSON.parse(import_fs.default.readFileSync(import_path.default.resolve(filepath, "package.json"), "utf8")).name
95
+ }));
96
+ }
97
+ return [];
98
+ };
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ findMonorepoRoot,
102
+ getMonorepoPackages,
103
+ isLerna,
104
+ isMonorepo,
105
+ isPnpmWorkspaces,
106
+ isYarnWorkspaces
107
+ });
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,18 +17,33 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var package_exports = {};
20
30
  __export(package_exports, {
21
31
  getAvailableVersion: () => getAvailableVersion,
32
+ getPackageManager: () => getPackageManager,
22
33
  isPackageDeprecated: () => isPackageDeprecated,
23
34
  isPackageExist: () => isPackageExist,
24
35
  semverDecrease: () => semverDecrease
25
36
  });
26
37
  module.exports = __toCommonJS(package_exports);
27
- var import_utils = require("@modern-js/utils");
38
+ var import_os = __toESM(require("os"));
39
+ var import_path = __toESM(require("path"));
40
+ var import_execa = require("@modern-js/codesmith-utils/execa");
41
+ var import_fs_extra = require("@modern-js/codesmith-utils/fs-extra");
42
+ var import_npm = require("@modern-js/codesmith-utils/npm");
43
+ var import_semver = require("@modern-js/codesmith-utils/semver");
44
+ var import_stripAnsi = require("./stripAnsi");
28
45
  async function isPackageExist(packageName, registry) {
29
- if (await (0, import_utils.canUseNpm)()) {
46
+ if (await (0, import_npm.canUseNpm)()) {
30
47
  try {
31
48
  const args = [
32
49
  "view",
@@ -36,8 +53,8 @@ async function isPackageExist(packageName, registry) {
36
53
  if (registry) {
37
54
  args.push(`--registry=${registry}`);
38
55
  }
39
- const result = await (0, import_utils.execa)("npm", args);
40
- return (0, import_utils.stripAnsi)(result.stdout);
56
+ const result = await (0, import_execa.execa)("npm", args);
57
+ return (0, import_stripAnsi.stripAnsi)(result.stdout);
41
58
  } catch (e) {
42
59
  return false;
43
60
  }
@@ -45,7 +62,7 @@ async function isPackageExist(packageName, registry) {
45
62
  throw new Error("not found npm, please install npm before");
46
63
  }
47
64
  async function isPackageDeprecated(packageName, registry) {
48
- if (await (0, import_utils.canUseNpm)()) {
65
+ if (await (0, import_npm.canUseNpm)()) {
49
66
  const args = [
50
67
  "view",
51
68
  packageName,
@@ -54,13 +71,13 @@ async function isPackageDeprecated(packageName, registry) {
54
71
  if (registry) {
55
72
  args.push(`--registry=${registry}`);
56
73
  }
57
- const result = await (0, import_utils.execa)("npm", args);
58
- return (0, import_utils.stripAnsi)(result.stdout);
74
+ const result = await (0, import_execa.execa)("npm", args);
75
+ return (0, import_stripAnsi.stripAnsi)(result.stdout);
59
76
  }
60
77
  throw new Error("not found npm, please install npm before");
61
78
  }
62
79
  function semverDecrease(version) {
63
- const versionObj = import_utils.semver.parse(version, {
80
+ const versionObj = import_semver.semver.parse(version, {
64
81
  loose: true
65
82
  });
66
83
  if (!versionObj) {
@@ -70,8 +87,8 @@ function semverDecrease(version) {
70
87
  versionObj.prerelease = [];
71
88
  versionObj.patch--;
72
89
  const result = versionObj.format();
73
- if (!import_utils.semver.valid(result)) {
74
- import_utils.logger.debug(`Version ${result} is not valid semver`);
90
+ if (!import_semver.semver.valid(result)) {
91
+ console.error(`Version ${result} is not valid semver`);
75
92
  return version;
76
93
  }
77
94
  return result;
@@ -81,7 +98,7 @@ async function getAvailableVersion(packageName, currentVersion, registry) {
81
98
  let version = currentVersion;
82
99
  while (times) {
83
100
  if (!await isPackageExist(`${packageName}@${version}`, registry) || await isPackageDeprecated(`${packageName}@${version}`, registry)) {
84
- version = import_utils.semver.inc(version, "patch");
101
+ version = import_semver.semver.inc(version, "patch");
85
102
  times--;
86
103
  continue;
87
104
  }
@@ -98,9 +115,35 @@ async function getAvailableVersion(packageName, currentVersion, registry) {
98
115
  }
99
116
  return currentVersion;
100
117
  }
118
+ const MAX_TIMES = 5;
119
+ async function getPackageManager(cwd = process.cwd()) {
120
+ let appDirectory = cwd;
121
+ let times = 0;
122
+ while (import_os.default.homedir() !== appDirectory && times < MAX_TIMES) {
123
+ times++;
124
+ if (import_fs_extra.fs.existsSync(import_path.default.resolve(appDirectory, "pnpm-lock.yaml"))) {
125
+ return "pnpm";
126
+ }
127
+ if (import_fs_extra.fs.existsSync(import_path.default.resolve(appDirectory, "yarn.lock"))) {
128
+ return "yarn";
129
+ }
130
+ if (import_fs_extra.fs.existsSync(import_path.default.resolve(appDirectory, "package-lock.json"))) {
131
+ return "npm";
132
+ }
133
+ appDirectory = import_path.default.join(appDirectory, "..");
134
+ }
135
+ if (await (0, import_npm.canUsePnpm)()) {
136
+ return "pnpm";
137
+ }
138
+ if (await (0, import_npm.canUseYarn)()) {
139
+ return "yarn";
140
+ }
141
+ return "npm";
142
+ }
101
143
  // Annotate the CommonJS export names for ESM import in node:
102
144
  0 && (module.exports = {
103
145
  getAvailableVersion,
146
+ getPackageManager,
104
147
  isPackageDeprecated,
105
148
  isPackageExist,
106
149
  semverDecrease
package/dist/esm/index.js CHANGED
@@ -1,46 +1,32 @@
1
1
  import path from "path";
2
+ import { getNpmVersion, getPackageInfo } from "@modern-js/codesmith";
3
+ import { fs } from "@modern-js/codesmith-utils/fs-extra";
4
+ import { ora } from "@modern-js/codesmith-utils/ora";
2
5
  import { Solution, SolutionToolsMap } from "@modern-js/generator-common";
3
- import { fs, canUseNpm, canUsePnpm, execa, getMonorepoPackages, ora } from "@modern-js/utils";
4
6
  import { i18n, localeKeys } from "./locale";
5
7
  import { fileExist } from "./utils/fsExist";
8
+ import { getMonorepoPackages } from "./utils/monorepo";
6
9
  import { getAvailableVersion, isPackageExist } from "./utils/package";
7
- import { stripAnsi } from "./utils/stripAnsi";
8
10
  export * from "./utils";
9
- import { ora as ora2, fs as fs2, semver, execa as execa2, chalk, readTsConfigByFile, getPackageManager, canUseNpm as canUseNpm2, canUsePnpm as canUsePnpm2, canUseYarn, isReact18 } from "@modern-js/utils";
11
+ import { canUseNpm, canUsePnpm, canUseYarn } from "@modern-js/codesmith-utils/npm";
12
+ import { fs as fs2 } from "@modern-js/codesmith-utils/fs-extra";
13
+ import { ora as ora2 } from "@modern-js/codesmith-utils/ora";
14
+ import { semver } from "@modern-js/codesmith-utils/semver";
15
+ import { execa } from "@modern-js/codesmith-utils/execa";
16
+ import { chalk } from "@modern-js/codesmith-utils/chalk";
10
17
  import { i18n as i18n2 } from "./locale";
11
- async function getPackageVersion(packageName, registry) {
18
+ async function getPackageVersion(packageName, registryUrl) {
12
19
  const spinner = ora({
13
20
  text: "Load Generator...",
14
21
  spinner: "runner"
15
22
  }).start();
16
- if (await canUsePnpm()) {
17
- const args = [
18
- "info",
19
- packageName,
20
- "version"
21
- ];
22
- if (registry) {
23
- args.push(`--registry=${registry}`);
24
- }
25
- const result = await execa("pnpm", args);
26
- spinner.stop();
27
- return stripAnsi(result.stdout);
28
- }
29
- if (await canUseNpm()) {
30
- const args = [
31
- "view",
32
- packageName,
33
- "version"
34
- ];
35
- if (registry) {
36
- args.push(`--registry=${registry}`);
37
- }
38
- const result = await execa("npm", args);
39
- spinner.stop();
40
- return stripAnsi(result.stdout);
41
- }
23
+ const { name, version: pkgVersion } = getPackageInfo(packageName);
24
+ const version = await getNpmVersion(name, {
25
+ version: pkgVersion,
26
+ registryUrl
27
+ });
42
28
  spinner.stop();
43
- throw new Error("not found npm, please install npm before");
29
+ return version;
44
30
  }
45
31
  async function getModernVersion(solution, registry, distTag = "latest") {
46
32
  const dep = SolutionToolsMap[solution];
@@ -155,11 +141,11 @@ async function getModernConfigFile(appDir) {
155
141
  return isTsProject(appDir) ? "modern.config.ts" : "modern.config.js";
156
142
  }
157
143
  export {
158
- canUseNpm2 as canUseNpm,
159
- canUsePnpm2 as canUsePnpm,
144
+ canUseNpm,
145
+ canUsePnpm,
160
146
  canUseYarn,
161
147
  chalk,
162
- execa2 as execa,
148
+ execa,
163
149
  fs2 as fs,
164
150
  getAllPackages,
165
151
  getMWAProjectPath,
@@ -167,15 +153,12 @@ export {
167
153
  getModernPluginVersion,
168
154
  getModernVersion,
169
155
  getModuleProjectPath,
170
- getPackageManager,
171
156
  getPackageManagerText,
172
157
  getPackageObj,
173
158
  getPackageVersion,
174
159
  i18n2 as i18n,
175
- isReact18,
176
160
  isTsProject,
177
161
  ora2 as ora,
178
- readTsConfigByFile,
179
162
  semver,
180
163
  validatePackageName,
181
164
  validatePackagePath
@@ -1,4 +1,4 @@
1
- import { fs } from "@modern-js/utils";
1
+ import { fs } from "@modern-js/codesmith-utils/fs-extra";
2
2
  async function fileExist(filePath) {
3
3
  try {
4
4
  const stat = await fs.stat(filePath);
@@ -0,0 +1,14 @@
1
+ import path from "path";
2
+ import { fs } from "@modern-js/codesmith-utils/fs-extra";
3
+ import json5 from "json5";
4
+ const readTsConfig = (root) => {
5
+ return readTsConfigByFile(path.resolve(root, "./tsconfig.json"));
6
+ };
7
+ const readTsConfigByFile = (filename) => {
8
+ const content = fs.readFileSync(path.resolve(filename), "utf-8");
9
+ return json5.parse(content);
10
+ };
11
+ export {
12
+ readTsConfig,
13
+ readTsConfigByFile
14
+ };
@@ -1,6 +1,5 @@
1
1
  export * from "./stripAnsi";
2
2
  export * from "./package";
3
- import { getGeneratorPath } from "./getGeneratorPath";
4
- export {
5
- getGeneratorPath
6
- };
3
+ export * from "./getGeneratorPath";
4
+ export * from "./get";
5
+ export * from "./is";
@@ -0,0 +1,21 @@
1
+ import path from "path";
2
+ import { fs } from "@modern-js/codesmith-utils/fs-extra";
3
+ import { semver } from "@modern-js/codesmith-utils/semver";
4
+ const isReact18 = (cwd = process.cwd()) => {
5
+ const pkgPath = path.join(cwd, "package.json");
6
+ if (!fs.existsSync(pkgPath)) {
7
+ return false;
8
+ }
9
+ const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
10
+ const deps = {
11
+ ...pkgInfo.devDependencies,
12
+ ...pkgInfo.dependencies
13
+ };
14
+ if (typeof deps.react !== "string") {
15
+ return false;
16
+ }
17
+ return semver.satisfies(semver.minVersion(deps.react), ">=18.0.0");
18
+ };
19
+ export {
20
+ isReact18
21
+ };
@@ -0,0 +1,68 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import glob from "glob";
4
+ import yaml from "js-yaml";
5
+ const PACKAGE_MAX_DEPTH = 5;
6
+ const WORKSPACE_FILES = {
7
+ YARN: "package.json",
8
+ PNPM: "pnpm-workspace.yaml",
9
+ LERNA: "lerna.json"
10
+ };
11
+ const isLerna = (root) => fs.existsSync(path.join(root, WORKSPACE_FILES.LERNA));
12
+ const isYarnWorkspaces = (root) => {
13
+ var _json_workspaces;
14
+ const pkg = path.join(root, WORKSPACE_FILES.YARN);
15
+ if (!fs.existsSync(pkg)) {
16
+ return false;
17
+ }
18
+ const json = JSON.parse(fs.readFileSync(pkg, "utf8"));
19
+ return Boolean((_json_workspaces = json.workspaces) === null || _json_workspaces === void 0 ? void 0 : _json_workspaces.packages);
20
+ };
21
+ const isPnpmWorkspaces = (root) => fs.existsSync(path.join(root, WORKSPACE_FILES.PNPM));
22
+ const isMonorepo = (root) => isLerna(root) || isYarnWorkspaces(root) || isPnpmWorkspaces(root);
23
+ const findMonorepoRoot = (appDirectory, maxDepth = PACKAGE_MAX_DEPTH) => {
24
+ let inMonorepo = false;
25
+ let monorepoRoot = appDirectory;
26
+ for (let depth = 0; depth < maxDepth; depth++) {
27
+ if (isMonorepo(appDirectory)) {
28
+ inMonorepo = true;
29
+ break;
30
+ }
31
+ monorepoRoot = path.dirname(appDirectory);
32
+ }
33
+ return inMonorepo ? monorepoRoot : void 0;
34
+ };
35
+ const getMonorepoPackages = (root) => {
36
+ let packages = [];
37
+ if (isYarnWorkspaces(root)) {
38
+ const json = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
39
+ ({ packages } = json.workspaces);
40
+ } else if (isLerna(root)) {
41
+ const json = JSON.parse(fs.readFileSync(path.resolve(root, "lerna.json"), "utf8"));
42
+ ({ packages } = json);
43
+ } else {
44
+ ({ packages } = yaml.load(fs.readFileSync(path.join(root, WORKSPACE_FILES.PNPM), "utf8")));
45
+ }
46
+ if (packages) {
47
+ return packages.map((name) => (
48
+ // The trailing / ensures only dirs are picked up
49
+ glob.sync(path.join(root, `${name}/`), {
50
+ ignore: [
51
+ "**/node_modules/**"
52
+ ]
53
+ })
54
+ )).reduce((acc, val) => acc.concat(val), []).filter((filepath) => fs.existsSync(path.resolve(filepath, "package.json"))).map((filepath) => ({
55
+ path: filepath,
56
+ name: JSON.parse(fs.readFileSync(path.resolve(filepath, "package.json"), "utf8")).name
57
+ }));
58
+ }
59
+ return [];
60
+ };
61
+ export {
62
+ findMonorepoRoot,
63
+ getMonorepoPackages,
64
+ isLerna,
65
+ isMonorepo,
66
+ isPnpmWorkspaces,
67
+ isYarnWorkspaces
68
+ };
@@ -1,4 +1,10 @@
1
- import { canUseNpm, execa, logger, semver, stripAnsi } from "@modern-js/utils";
1
+ import os from "os";
2
+ import path from "path";
3
+ import { execa } from "@modern-js/codesmith-utils/execa";
4
+ import { fs } from "@modern-js/codesmith-utils/fs-extra";
5
+ import { canUseNpm, canUsePnpm, canUseYarn } from "@modern-js/codesmith-utils/npm";
6
+ import { semver } from "@modern-js/codesmith-utils/semver";
7
+ import { stripAnsi } from "./stripAnsi";
2
8
  async function isPackageExist(packageName, registry) {
3
9
  if (await canUseNpm()) {
4
10
  try {
@@ -45,7 +51,7 @@ function semverDecrease(version) {
45
51
  versionObj.patch--;
46
52
  const result = versionObj.format();
47
53
  if (!semver.valid(result)) {
48
- logger.debug(`Version ${result} is not valid semver`);
54
+ console.error(`Version ${result} is not valid semver`);
49
55
  return version;
50
56
  }
51
57
  return result;
@@ -72,8 +78,34 @@ async function getAvailableVersion(packageName, currentVersion, registry) {
72
78
  }
73
79
  return currentVersion;
74
80
  }
81
+ const MAX_TIMES = 5;
82
+ async function getPackageManager(cwd = process.cwd()) {
83
+ let appDirectory = cwd;
84
+ let times = 0;
85
+ while (os.homedir() !== appDirectory && times < MAX_TIMES) {
86
+ times++;
87
+ if (fs.existsSync(path.resolve(appDirectory, "pnpm-lock.yaml"))) {
88
+ return "pnpm";
89
+ }
90
+ if (fs.existsSync(path.resolve(appDirectory, "yarn.lock"))) {
91
+ return "yarn";
92
+ }
93
+ if (fs.existsSync(path.resolve(appDirectory, "package-lock.json"))) {
94
+ return "npm";
95
+ }
96
+ appDirectory = path.join(appDirectory, "..");
97
+ }
98
+ if (await canUsePnpm()) {
99
+ return "pnpm";
100
+ }
101
+ if (await canUseYarn()) {
102
+ return "yarn";
103
+ }
104
+ return "npm";
105
+ }
75
106
  export {
76
107
  getAvailableVersion,
108
+ getPackageManager,
77
109
  isPackageDeprecated,
78
110
  isPackageExist,
79
111
  semverDecrease
@@ -1,9 +1,14 @@
1
1
  import type { GeneratorContext } from '@modern-js/codesmith';
2
2
  import { Solution } from '@modern-js/generator-common';
3
3
  export * from './utils';
4
- export { ora, fs, semver, execa, chalk, readTsConfigByFile, getPackageManager, canUseNpm, canUsePnpm, canUseYarn, isReact18, } from '@modern-js/utils';
4
+ export { canUseNpm, canUsePnpm, canUseYarn, } from '@modern-js/codesmith-utils/npm';
5
+ export { fs } from '@modern-js/codesmith-utils/fs-extra';
6
+ export { ora } from '@modern-js/codesmith-utils/ora';
7
+ export { semver } from '@modern-js/codesmith-utils/semver';
8
+ export { execa } from '@modern-js/codesmith-utils/execa';
9
+ export { chalk } from '@modern-js/codesmith-utils/chalk';
5
10
  export { i18n } from './locale';
6
- export declare function getPackageVersion(packageName: string, registry?: string): Promise<string>;
11
+ export declare function getPackageVersion(packageName: string, registryUrl?: string): Promise<string>;
7
12
  export declare function getModernVersion(solution: Solution, registry?: string, distTag?: string): Promise<string>;
8
13
  export declare function getModernPluginVersion(solution: Solution, packageName: string, options?: {
9
14
  cwd?: string;
@@ -0,0 +1,2 @@
1
+ export declare const readTsConfig: (root: string) => any;
2
+ export declare const readTsConfigByFile: (filename: string) => any;
@@ -1,3 +1,5 @@
1
1
  export * from './stripAnsi';
2
2
  export * from './package';
3
- export { getGeneratorPath } from './getGeneratorPath';
3
+ export * from './getGeneratorPath';
4
+ export * from './get';
5
+ export * from './is';
@@ -0,0 +1 @@
1
+ export declare const isReact18: (cwd?: string) => boolean;
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ import path from 'path';
3
+ export declare const isLerna: (root: string) => boolean;
4
+ export declare const isYarnWorkspaces: (root: string) => boolean;
5
+ export declare const isPnpmWorkspaces: (root: string) => boolean;
6
+ export declare const isMonorepo: (root: string) => boolean;
7
+ export declare const findMonorepoRoot: (appDirectory: string, maxDepth?: number) => string | undefined;
8
+ export declare const getMonorepoPackages: (root: string) => {
9
+ name: string;
10
+ path: string;
11
+ }[];
@@ -7,3 +7,4 @@ export declare function semverDecrease(version: string): string;
7
7
  * 限制只在当前小版本号范围内寻找
8
8
  */
9
9
  export declare function getAvailableVersion(packageName: string, currentVersion: string, registry?: string): Promise<string>;
10
+ export declare function getPackageManager(cwd?: string): Promise<"pnpm" | "yarn" | "npm">;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.6.3",
18
+ "version": "3.6.4-alpha.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -30,14 +30,19 @@
30
30
  }
31
31
  },
32
32
  "dependencies": {
33
+ "@modern-js/codesmith-utils": "2.6.0",
34
+ "glob": "7.2.0",
35
+ "js-yaml": "4.1.0",
36
+ "json5": "2.2.3",
33
37
  "@swc/helpers": "0.5.13",
34
38
  "@modern-js/generator-common": "3.6.3",
35
- "@modern-js/plugin-i18n": "2.60.3",
36
- "@modern-js/utils": "2.60.3"
39
+ "@modern-js/plugin-i18n": "2.60.3"
37
40
  },
38
41
  "devDependencies": {
39
- "@modern-js/codesmith": "2.5.2",
42
+ "@modern-js/codesmith": "2.6.0",
40
43
  "@types/jest": "^29",
44
+ "@types/glob": "7.2.0",
45
+ "@types/js-yaml": "4.0.9",
41
46
  "@types/node": "^14",
42
47
  "jest": "^29",
43
48
  "typescript": "^5",
@@ -47,8 +52,7 @@
47
52
  "sideEffects": false,
48
53
  "publishConfig": {
49
54
  "registry": "https://registry.npmjs.org/",
50
- "access": "public",
51
- "provenance": true
55
+ "access": "public"
52
56
  },
53
57
  "scripts": {
54
58
  "new": "modern-lib new",