nsgm-cli 2.1.45 → 2.1.46

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/lib/generate.js CHANGED
@@ -25,12 +25,49 @@ const normalizeDirectory = (dictionary) => {
25
25
  const installNpmPackages = (targetDir) => {
26
26
  try {
27
27
  const prefix = targetDir ? `cd ${targetDir} && ` : "";
28
- console.log("Installing all dependencies from package.json...");
29
- const installResult = shelljs_1.default.exec(`${prefix}pnpm install ${PNPM_INSTALL_FLAGS}`);
28
+ // 检测 packageManager 字段
29
+ let packageManager = "pnpm"; // 默认使用 pnpm
30
+ const packageJsonPath = targetDir ? `${targetDir}/package.json` : "package.json";
31
+ try {
32
+ const packageJsonContent = shelljs_1.default.cat(packageJsonPath);
33
+ if (packageJsonContent.code === 0) {
34
+ const packageJson = JSON.parse(packageJsonContent);
35
+ if (packageJson.packageManager) {
36
+ if (packageJson.packageManager.startsWith("npm")) {
37
+ packageManager = "npm";
38
+ }
39
+ else if (packageJson.packageManager.startsWith("pnpm")) {
40
+ packageManager = "pnpm";
41
+ }
42
+ else if (packageJson.packageManager.startsWith("yarn")) {
43
+ packageManager = "yarn";
44
+ }
45
+ }
46
+ }
47
+ }
48
+ catch {
49
+ // 如果无法读取或解析 package.json,使用默认包管理器
50
+ console.warn("无法检测 packageManager 字段,使用默认包管理器");
51
+ }
52
+ console.log(`Installing all dependencies from package.json using ${packageManager}...`);
53
+ let installCommand;
54
+ if (packageManager === "npm") {
55
+ installCommand = `${prefix}npm install`;
56
+ }
57
+ else if (packageManager === "pnpm") {
58
+ installCommand = `${prefix}pnpm install ${PNPM_INSTALL_FLAGS}`;
59
+ }
60
+ else if (packageManager === "yarn") {
61
+ installCommand = `${prefix}yarn install`;
62
+ }
63
+ else {
64
+ installCommand = `${prefix}npm install`;
65
+ }
66
+ const installResult = shelljs_1.default.exec(installCommand);
30
67
  return installResult.code === 0;
31
68
  }
32
69
  catch (error) {
33
- console.error("Failed to install pnpm packages:", error);
70
+ console.error("Failed to install packages:", error);
34
71
  return false;
35
72
  }
36
73
  };