hiepdh-playable-toolkit 2.0.4 → 2.2.4

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/package.json CHANGED
@@ -1,25 +1,33 @@
1
- {
2
- "name": "hiepdh-playable-toolkit",
3
- "version": "2.0.4",
4
- "main": "setup.js",
5
- "dependencies": {
6
- "fs-extra": "^11.1.0",
7
- "adm-zip": "^0.5.10"
8
- },
9
- "scripts": {
10
- "postinstall": "node setup.js"
11
- },
12
- "bin": {
13
- "hiepdh-setup": "setup.js"
14
- },
15
- "files": [
16
- "2x",
17
- "3x",
18
- "setup.js",
19
- "README.md"
20
- ],
21
- "keywords": [],
22
- "author": "HiepDH",
23
- "license": "ISC",
24
- "description": "ahihi!"
25
- }
1
+ {
2
+ "name": "hiepdh-playable-toolkit",
3
+ "version": "2.2.4",
4
+ "main": "setup.js",
5
+ "dependencies": {
6
+ "fs-extra": "^11.1.0",
7
+ "adm-zip": "^0.5.10"
8
+ },
9
+ "scripts": {
10
+ "postinstall": "node setup.js",
11
+ "clean": "node -e \"fs.rmSync('./build-templates/web-mobile/application.js', {force:true})\"",
12
+ "fix-unity": "node patch-template-unityReject.js",
13
+ "build-unity": "node build-inline.js unity",
14
+ "build-all": "node build-inline.js all"
15
+ },
16
+ "bin": {
17
+ "hiepdh-setup": "setup.js"
18
+ },
19
+ "files": [
20
+ "2x",
21
+ "3x",
22
+ "setup.js",
23
+ "README.md"
24
+ ],
25
+ "keywords": [],
26
+ "author": "HiepDH",
27
+ "license": "ISC",
28
+ "description": "ahihi!",
29
+ "devDependencies": {
30
+ "adm-zip": "^0.5.10",
31
+ "fs-extra": "^11.1.0"
32
+ }
33
+ }
package/setup.js CHANGED
@@ -1,6 +1,6 @@
1
- const fs = require('fs-extra');
2
- const path = require('path');
3
- const { execSync } = require('child_process');
1
+ const fs = require("fs-extra");
2
+ const path = require("path");
3
+ const { execSync } = require("child_process");
4
4
 
5
5
  // Xác định thư mục gốc của dự án người dùng
6
6
  const projectRoot = process.env.INIT_CWD || process.cwd();
@@ -23,9 +23,10 @@ const cocosVer = pkg.creator ? pkg.creator.version : "unknown";
23
23
  if (cocosVer.startsWith("3")) {
24
24
  console.log(` Phát hiện Cocos Creator 3.x (Version: ${cocosVer})`);
25
25
  setupForV3(pkg);
26
- } else if (cocosVer.startsWith("1")) { // Sửa từ "1" thành "2"
26
+ } else if (cocosVer.startsWith("1")) {
27
+ // Sửa từ "1" thành "2"
27
28
  console.log(` Phát hiện Cocos Creator 2.x (Version: ${cocosVer})`);
28
- setupForV2(pkg);
29
+ setupForV2(pkg);
29
30
  } else {
30
31
  console.warn(" Không xác định được phiên bản. Mặc định cấu hình cho 3.x");
31
32
  setupForV3(pkg);
@@ -37,10 +38,11 @@ function setupForV3(pkg) {
37
38
 
38
39
  // 1. Cập nhật Scripts vào package.json
39
40
  updateScripts(pkg, {
40
- "clean": "node -e \"fs.rmSync('./build-templates/web-mobile/application.js', {force:true})\"",
41
+ clean:
42
+ "node -e \"fs.rmSync('./build-templates/web-mobile/application.js', {force:true})\"",
41
43
  "fix-unity": "node patch-template-unityReject.js",
42
44
  "build-unity": "node build-inline.js unity",
43
- "build-all": "node build-inline.js all"
45
+ "build-all": "node build-inline.js all",
44
46
  });
45
47
 
46
48
  // 2. Cài đặt Extension (super-html)
@@ -51,27 +53,48 @@ function setupForV3(pkg) {
51
53
  console.log(" Đã cài đặt Extension super-html.");
52
54
  // Tự động npm install cho extension
53
55
  try {
54
- console.log(" Đang cài đặt dependencies cho Extension...");
55
- execSync('npm install', { cwd: extTarget, stdio: 'ignore' });
56
- } catch(e) { console.warn(" Vui lòng chạy 'npm install' trong folder extension thủ công."); }
56
+ console.log(" Đang cài đặt dependencies cho Extension...");
57
+ execSync("npm install", { cwd: extTarget, stdio: "ignore" });
58
+ } catch (e) {
59
+ console.warn(
60
+ " Vui lòng chạy 'npm install' trong folder extension thủ công."
61
+ );
62
+ }
57
63
  }
58
64
 
59
65
  // 3. Cài đặt Templates (index.ejs)
60
- const templateSource = path.join(source3x, "build-templates", "web-mobile", "index.ejs");
61
- const templateTargetDir = path.join(projectRoot, "build-templates", "web-mobile");
66
+ const templateSource = path.join(
67
+ source3x,
68
+ "build-templates",
69
+ "web-mobile",
70
+ "index.ejs"
71
+ );
72
+ const templateTargetDir = path.join(
73
+ projectRoot,
74
+ "build-templates",
75
+ "web-mobile"
76
+ );
62
77
  if (fs.existsSync(templateSource)) {
63
- if (!fs.existsSync(templateTargetDir)) fs.mkdirSync(templateTargetDir, { recursive: true });
78
+ if (!fs.existsSync(templateTargetDir))
79
+ fs.mkdirSync(templateTargetDir, { recursive: true });
64
80
  fs.copyFileSync(templateSource, path.join(templateTargetDir, "index.ejs"));
65
81
  console.log(" Đã cấu hình build-templates/index.ejs.");
66
82
  }
67
83
 
68
84
  // 4. Copy các Script xử lý (build-inline.js, patch...) ra gốc dự án
69
- const files = ["build-inline.js", "patch-template-unityReject.js", "injectCustomScript.js"];
70
- files.forEach(file => {
85
+ const files = [
86
+ "build-inline.js",
87
+ "patch-template-unityReject.js",
88
+ "injectCustomScript.js",
89
+ ];
90
+ files.forEach((file) => {
71
91
  const src = path.join(source3x, file);
72
92
  if (fs.existsSync(src)) fs.copyFileSync(src, path.join(projectRoot, file));
73
93
  });
74
94
 
95
+ // 5. init các package cho Ver
96
+ reviveProject(projectRoot, toolkitRoot, "3x");
97
+
75
98
  console.log("\n--- Toolkit 3.x Ready! Let's Cook! ---");
76
99
  }
77
100
 
@@ -89,11 +112,77 @@ function updateScripts(pkg, newScripts) {
89
112
 
90
113
  function copyFolderRecursive(source, target) {
91
114
  if (!fs.existsSync(target)) fs.mkdirSync(target, { recursive: true });
92
- fs.readdirSync(source).forEach(file => {
115
+ fs.readdirSync(source).forEach((file) => {
93
116
  const srcFile = path.join(source, file);
94
117
  const tgtFile = path.join(target, file);
95
118
  if (file === "node_modules") return; // Bỏ qua node_modules
96
- if (fs.lstatSync(srcFile).isDirectory()) copyFolderRecursive(srcFile, tgtFile);
119
+ if (fs.lstatSync(srcFile).isDirectory())
120
+ copyFolderRecursive(srcFile, tgtFile);
97
121
  else fs.copyFileSync(srcFile, tgtFile);
98
122
  });
99
- }
123
+ }
124
+
125
+ function reviveProject(projectRoot, toolkitRoot, version = "3x") {
126
+ console.log(` Đang khôi phục môi trường cho Cocos ${version}...`);
127
+
128
+ const targetPkgPath = path.join(projectRoot, "package.json");
129
+ const targetLockPath = path.join(projectRoot, "package-lock.json");
130
+ const templateDir = path.join(toolkitRoot, version, "templates");
131
+
132
+ // 1. Ưu tiên đưa file package-lock.json mẫu vào trước
133
+ if (!fs.existsSync(targetLockPath)) {
134
+ const sourceLock = path.join(templateDir, "package-lock.json");
135
+ if (fs.existsSync(sourceLock)) {
136
+ fs.copySync(sourceLock, targetLockPath);
137
+ console.log(" Đã khôi phục file lock từ Toolkit.");
138
+ } else {
139
+ console.error(" Lỗi: Toolkit không có file package-lock mẫu!");
140
+ return;
141
+ }
142
+ }
143
+
144
+ // 2. Tái tạo hoặc cập nhật package.json
145
+ let targetPkg = fs.existsSync(targetPkgPath)
146
+ ? fs.readJsonSync(targetPkgPath)
147
+ : {};
148
+ const lockData = fs.readJsonSync(targetLockPath);
149
+
150
+ // Trích xuất "linh hồn" của package.json từ file lock (định dạng npm 7+)
151
+ const rootInfo = lockData.packages && lockData.packages[""];
152
+
153
+ if (rootInfo) {
154
+ console.log(" Đang trích xuất dependencies từ file lock...");
155
+ targetPkg.name = targetPkg.name || rootInfo.name || "reconstructed-project";
156
+ targetPkg.version = targetPkg.version || rootInfo.version || "1.0.0";
157
+ targetPkg.dependencies = {
158
+ ...rootInfo.dependencies,
159
+ ...targetPkg.dependencies,
160
+ };
161
+ targetPkg.devDependencies = {
162
+ ...rootInfo.devDependencies,
163
+ ...targetPkg.devDependencies,
164
+ };
165
+ }
166
+
167
+ // 3. Bơm thêm các thư viện bắt buộc của Toolkit
168
+ targetPkg.devDependencies = {
169
+ ...targetPkg.devDependencies,
170
+ "adm-zip": "^0.5.10",
171
+ "fs-extra": "^11.1.0",
172
+ };
173
+
174
+ // 4. Lưu lại file package.json hoàn chỉnh
175
+ fs.writeJsonSync(targetPkgPath, targetPkg, { spaces: 2 });
176
+ console.log(" File package.json đã được tái tạo thành công.");
177
+
178
+ // 5. Kích hoạt cài đặt
179
+ try {
180
+ console.log(" Đang nạp lại toàn bộ node_modules (npm install)...");
181
+ // Sử dụng --package-lock-only nếu bạn chỉ muốn cập nhật cấu trúc,
182
+ // nhưng ở đây ta cần thư mục node_modules nên dùng install thuần.
183
+ execSync("npm install", { cwd: projectRoot, stdio: "inherit" });
184
+ console.log(" Hoàn tất! node_modules đã hồi sinh.");
185
+ } catch (e) {
186
+ console.error(" Lỗi khi cài đặt: " + e.message);
187
+ }
188
+ }