preinstall.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const fs = require("fs");
  2. const path = require("path");
  3. const { spawnSync } = require("child_process");
  4. const PATH = {
  5. packageJSON: path.join(__dirname, "../package.json")
  6. };
  7. function checkCreatorTypesVersion(version) {
  8. try {
  9. // 根据平台选择合适的npm命令
  10. const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
  11. // 检查npm命令是否可用
  12. const npmCheck = spawnSync(npmCmd, ["--version"], {
  13. stdio: 'pipe',
  14. shell: process.platform === "win32"
  15. });
  16. if (npmCheck.error || npmCheck.status !== 0) {
  17. console.warn("Warning: npm command not available, skipping version check");
  18. return true; // 如果npm不可用,跳过检查
  19. }
  20. // 获取版本列表
  21. const result = spawnSync(npmCmd, ["view", "@cocos/creator-types", "versions"], {
  22. stdio: 'pipe',
  23. shell: process.platform === "win32"
  24. });
  25. if (result.error || result.status !== 0) {
  26. console.warn("Warning: Failed to fetch @cocos/creator-types versions, skipping check");
  27. return true; // 如果获取失败,跳过检查
  28. }
  29. let output = result.stdout.toString().trim();
  30. // 尝试解析JSON
  31. try {
  32. const versions = JSON.parse(output);
  33. if (Array.isArray(versions)) {
  34. return versions.includes(version);
  35. } else if (typeof versions === 'string') {
  36. return versions.includes(version);
  37. }
  38. } catch (parseError) {
  39. // 如果JSON解析失败,尝试作为字符串处理
  40. return output.includes(version);
  41. }
  42. return false;
  43. } catch (error) {
  44. console.warn("Warning: Version check failed:", error.message);
  45. return true; // 出错时跳过检查
  46. }
  47. }
  48. function getCreatorTypesVersion() {
  49. try {
  50. // 检查package.json文件是否存在
  51. if (!fs.existsSync(PATH.packageJSON)) {
  52. console.warn("Warning: package.json not found");
  53. return null;
  54. }
  55. const packageContent = fs.readFileSync(PATH.packageJSON, "utf8");
  56. const packageJson = JSON.parse(packageContent);
  57. // 检查devDependencies是否存在
  58. if (!packageJson.devDependencies || !packageJson.devDependencies["@cocos/creator-types"]) {
  59. console.warn("Warning: @cocos/creator-types not found in devDependencies");
  60. return null;
  61. }
  62. const versionString = packageJson.devDependencies["@cocos/creator-types"];
  63. return versionString.replace(/^[^\d]+/, "");
  64. } catch (error) {
  65. console.warn("Warning: Failed to read package.json:", error.message);
  66. return null;
  67. }
  68. }
  69. function main() {
  70. try {
  71. const creatorTypesVersion = getCreatorTypesVersion();
  72. if (!creatorTypesVersion) {
  73. console.log("Skipping @cocos/creator-types version check");
  74. return;
  75. }
  76. if (!checkCreatorTypesVersion(creatorTypesVersion)) {
  77. console.log("\x1b[33mWarning:\x1b[0m");
  78. console.log(" @en");
  79. console.log(" Version check of @cocos/creator-types failed.");
  80. console.log(` The definition of ${creatorTypesVersion} has not been released yet. Please export the definition to the ./node_modules directory by selecting "Developer -> Export Interface Definition" in the menu of the Creator editor.`);
  81. console.log(" The definition of the corresponding version will be released on npm after the editor is officially released.");
  82. console.log(" @zh");
  83. console.log(" @cocos/creator-types 版本检查失败。");
  84. console.log(` ${creatorTypesVersion} 定义还未发布,请先通过 Creator 编辑器菜单 "开发者 -> 导出接口定义",导出定义到 ./node_modules 目录。`);
  85. console.log(" 对应版本的定义会在编辑器正式发布后同步发布到 npm 上。");
  86. }
  87. } catch (error) {
  88. console.error("Preinstall script error:", error.message);
  89. // 不要抛出错误,让安装继续进行
  90. process.exit(0);
  91. }
  92. }
  93. // 执行主函数
  94. main();