drgame-cc 1.0.67 → 1.0.70

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/README.md CHANGED
@@ -1,172 +1,172 @@
1
- # drgame-cc
2
-
3
- 道然游戏 Cocos Creator 2.4.15 适配包(npm 包),为宿主项目提供组件脚本、资源与构建模板。
4
-
5
- - 包名:`drgame-cc`
6
- - 适用引擎:Cocos Creator 2.4.15
7
- - 主要组件:`BtnExtra`(继承 `cc.Component`)、`ExitWin`、`Toast` 等
8
-
9
- ---
10
-
11
- ## 一、安装
12
-
13
- 在宿主 Cocos Creator 项目的根目录执行:
14
-
15
- ```bash
16
- npm install drgame-cc
17
- ```
18
-
19
- 安装完成后会自动触发 `postinstall`,把包内资源拷贝到宿主项目对应目录(见下文)。
20
-
21
- > 也可在任意位置手动运行安装:`npm install drgame-cc --prefix /path/to/CocosProject`
22
-
23
- ---
24
-
25
- ## 二、postinstall(安装后自动 / 手动拷贝)
26
-
27
- `scripts/postinstall.js` 负责把包内的资源同步到宿主 Cocos 项目。
28
-
29
- ### 拷贝映射
30
-
31
- | 包内目录 | 宿主项目目录 | 说明 |
32
- | --- | --- | --- |
33
- | `build-templates` | `<CocosRoot>/build-templates` | 构建模板 |
34
- | `drres` | `<CocosRoot>/assets/resources/drres` | 运行时资源 |
35
- | `drscript` | `<CocosRoot>/assets/drscript` | 组件脚本(`@ccclass` 组件,编辑器在此识别) |
36
-
37
- ### 指定 Cocos 项目路径
38
-
39
- 默认目标目录为 `node_modules` 的父目录。可通过命令行参数显式指定 Cocos 项目根目录
40
-
41
- ```bash
42
- # 位置参数
43
- node scripts/postinstall.js D:/MyCocosProject
44
-
45
- # 选项形式
46
- node scripts/postinstall.js --cocos-path D:/MyCocosProject
47
- node scripts/postinstall.js --cocos-path=D:/MyCocosProject
48
- node scripts/postinstall.js -p D:/MyCocosProject
49
- ```
50
-
51
- - 优先级:命令行参数 > 默认目录
52
- - 相对路径会基于当前工作目录解析
53
- - 目录不存在时打印 `[SKIP]` 跳过,不影响其它项
54
-
55
- > 注意:要让组件(如 `BtnExtra`)被编辑器识别,`.ts` 源文件必须位于宿主 `assets/` 内且配套 `.meta`。请确保发包前 `drres`/`drscript` 已生成并纳入 `package.json` 的 `files`。
56
-
57
- ---
58
-
59
- ## 三、sync-back(反向同步,开发调试用)
60
-
61
- 在宿主项目里改动了脚本或资源后,用 `scripts/sync-back.js` 把修改反向拷回 npm 包原始目录,便于提交/发包。
62
-
63
- 拷贝映射(与 postinstall 完全对称,方向相反):
64
-
65
- | 宿主项目目录 | 包内目录 |
66
- | --- | --- |
67
- | `<CocosRoot>/build-templates` | `build-templates` |
68
- | `<CocosRoot>/assets/resources/drres` | `drres` |
69
- | `<CocosRoot>/assets/drscript` | `drscript` |
70
-
71
- ### 用法
72
-
73
- ```bash
74
- node scripts/sync-back.js D:/MyCocosProject
75
- node scripts/sync-back.js --cocos-path D:/MyCocosProject
76
- node scripts/sync-back.js -p D:/MyCocosProject
77
- ```
78
-
79
- > 该脚本会**直接覆盖**包内同名文件(含 `.meta` 中宿主生成的 uuid)。建议在 git 版本控制下运行,便于回滚。
80
-
81
- 建议加入 `package.json` 的 `scripts` 作为快捷入口:
82
-
83
- ```json
84
- "scripts": {
85
- "postinstall": "node scripts/postinstall.js && echo '完成'",
86
- "sync-back": "node scripts/sync-back.js"
87
- }
88
- ```
89
-
90
- ---
91
-
92
- ## 四、发布流程
93
-
94
-
95
- ### 1. 安全登录(二选一)
96
-
97
- 方式 A:交互式登录(推荐,不落盘明文密码)
98
-
99
- ```bash
100
- npm login
101
- # 依次输入 Username: banshenghuiyi / Password / Email
102
- ```
103
-
104
- 方式 B:使用 authToken(写入用户级 `~/.npmrc`,不要提交到项目)
105
-
106
- ```bash
107
- npm config set //registry.npmjs.org/:_authToken=<你的TOKEN>
108
- ```
109
-
110
- > 也可在项目根新建 `.npmrc`,但务必加入 `.gitignore`,切勿提交:
111
- > ```
112
- > //registry.npmjs.org/:_authToken=${NPM_TOKEN}
113
- > ```
114
- > 并通过环境变量 `NPM_TOKEN` 注入。
115
-
116
- ### 2. 确认 registry
117
-
118
- 发布到公共 npm:
119
-
120
- ```bash
121
- npm config get registry # 应为 https://registry.npmjs.org/
122
- ```
123
-
124
- 如需发布到私有源,先设置:
125
-
126
- ```bash
127
- npm config set registry https://your-private-registry/
128
- ```
129
-
130
- ### 3. 更新版本号
131
-
132
- 遵循语义化版本(SemVer):
133
-
134
- ```bash
135
- npm version patch # 1.0.14 -> 1.0.15 修复
136
- npm version minor # 1.0.14 -> 1.1.0 小特性
137
- npm version major # 1.0.14 -> 2.0.0 破坏性变更
138
- ```
139
-
140
- ### 4. 发布
141
-
142
- ```bash
143
- npm publish
144
- ```
145
-
146
- > 发布前请确认:
147
- > - `package.json` 的 `files` 已包含 `drres`、`drscript`、`build-templates`、`tvui` 等需要分发的目录;
148
- > - 已执行构建,生成最新的 `drres`/`drscript` 产物;
149
- > - 没有把 `.npmrc`、本地凭据、密钥提交进去。
150
-
151
- ### 5. 在宿主项目更新
152
-
153
- ```bash
154
- npm update drgame-cc
155
- # 或指定版本
156
- npm install drgame-cc@1.0.15
157
- ```
158
-
159
- 安装后会自动触发 `postinstall`,把新资源同步到宿主项目。
160
-
161
- ---
162
-
163
- ## 五、常见问题
164
-
165
- **Q:编辑器识别不到 `BtnExtra` 等组件?**
166
- A:组件 `.ts` 源文件必须位于宿主 `assets/` 内且带 `.meta`。请确认发包时 `drscript`(含组件脚本与 `.meta`)已生成并纳入 `files`,安装后确实出现在 `<CocosRoot>/assets/drscript` 下。
167
-
168
- **Q:postinstall 提示源目录不存在?**
169
- A:`drres`/`drscript` 为空或不存在,通常是未执行构建生成产物。先在包内生成这两个目录再发包。
170
-
171
- **Q:反向同步后 uuid 变了?**
172
- A:`sync-back` 会把宿主项目的 `.meta`(含其 uuid)覆盖回包内。这是预期行为,便于保持一致;如不需要,可只同步 `.ts`/资源而忽略 `.meta`。
1
+ # drgame-cc
2
+
3
+ 道然游戏 Cocos Creator 2.4.15 适配包(npm 包),为宿主项目提供组件脚本、资源与构建模板。
4
+
5
+ - 包名:`drgame-cc`
6
+ - 适用引擎:Cocos Creator 2.4.15
7
+ - 主要组件:`BtnExtra`(继承 `cc.Component`)、`ExitWin`、`Toast` 等
8
+
9
+ ---
10
+
11
+ ## 一、安装
12
+
13
+ 在宿主 Cocos Creator 项目的根目录执行:
14
+
15
+ ```bash
16
+ npm install drgame-cc
17
+ ```
18
+
19
+ 安装完成后会自动触发 `postinstall`,把包内资源拷贝到宿主项目对应目录(见下文)。
20
+
21
+ > 也可在任意位置手动运行安装:`npm install drgame-cc --prefix /path/to/CocosProject`
22
+
23
+ ---
24
+
25
+ ## 二、postinstall(安装后自动 / 手动拷贝)
26
+
27
+ `scripts/postinstall.js` 负责把包内的资源同步到宿主 Cocos 项目。
28
+
29
+ ### 拷贝映射
30
+
31
+ | 包内目录 | 宿主项目目录 | 说明 |
32
+ | --- | --- | --- |
33
+ | `build-templates` | `<CocosRoot>/build-templates` | 构建模板 |
34
+ | `drres` | `<CocosRoot>/assets/resources/drres` | 运行时资源 |
35
+ | `drscript` | `<CocosRoot>/assets/drscript` | 组件脚本(`@ccclass` 组件,编辑器在此识别) |
36
+
37
+ ### 指定 Cocos 项目路径
38
+
39
+ 默认目标目录为 `node_modules` 的父目录。可通过命令行参数显式指定 Cocos 项目根目录
40
+
41
+ ```bash
42
+ # 位置参数
43
+ node scripts/postinstall.js D:/MyCocosProject
44
+
45
+ # 选项形式
46
+ node scripts/postinstall.js --cocos-path D:/MyCocosProject
47
+ node scripts/postinstall.js --cocos-path=D:/MyCocosProject
48
+ node scripts/postinstall.js -p D:/MyCocosProject
49
+ ```
50
+
51
+ - 优先级:命令行参数 > 默认目录
52
+ - 相对路径会基于当前工作目录解析
53
+ - 目录不存在时打印 `[SKIP]` 跳过,不影响其它项
54
+
55
+ > 注意:要让组件(如 `BtnExtra`)被编辑器识别,`.ts` 源文件必须位于宿主 `assets/` 内且配套 `.meta`。请确保发包前 `drres`/`drscript` 已生成并纳入 `package.json` 的 `files`。
56
+
57
+ ---
58
+
59
+ ## 三、sync-back(反向同步,开发调试用)
60
+
61
+ 在宿主项目里改动了脚本或资源后,用 `scripts/sync-back.js` 把修改反向拷回 npm 包原始目录,便于提交/发包。
62
+
63
+ 拷贝映射(与 postinstall 完全对称,方向相反):
64
+
65
+ | 宿主项目目录 | 包内目录 |
66
+ | --- | --- |
67
+ | `<CocosRoot>/build-templates` | `build-templates` |
68
+ | `<CocosRoot>/assets/resources/drres` | `drres` |
69
+ | `<CocosRoot>/assets/drscript` | `drscript` |
70
+
71
+ ### 用法
72
+
73
+ ```bash
74
+ node scripts/sync-back.js D:/MyCocosProject
75
+ node scripts/sync-back.js --cocos-path D:/MyCocosProject
76
+ node scripts/sync-back.js -p D:/MyCocosProject
77
+ ```
78
+
79
+ > 该脚本会**直接覆盖**包内同名文件(含 `.meta` 中宿主生成的 uuid)。建议在 git 版本控制下运行,便于回滚。
80
+
81
+ 建议加入 `package.json` 的 `scripts` 作为快捷入口:
82
+
83
+ ```json
84
+ "scripts": {
85
+ "postinstall": "node scripts/postinstall.js && echo '完成'",
86
+ "sync-back": "node scripts/sync-back.js"
87
+ }
88
+ ```
89
+
90
+ ---
91
+
92
+ ## 四、发布流程
93
+
94
+
95
+ ### 1. 安全登录(二选一)
96
+
97
+ 方式 A:交互式登录(推荐,不落盘明文密码)
98
+
99
+ ```bash
100
+ npm login
101
+ # 依次输入 Username: banshenghuiyi / Password / Email
102
+ ```
103
+
104
+ 方式 B:使用 authToken(写入用户级 `~/.npmrc`,不要提交到项目)
105
+
106
+ ```bash
107
+ npm config set //registry.npmjs.org/:_authToken=<你的TOKEN>
108
+ ```
109
+
110
+ > 也可在项目根新建 `.npmrc`,但务必加入 `.gitignore`,切勿提交:
111
+ > ```
112
+ > //registry.npmjs.org/:_authToken=${NPM_TOKEN}
113
+ > ```
114
+ > 并通过环境变量 `NPM_TOKEN` 注入。
115
+
116
+ ### 2. 确认 registry
117
+
118
+ 发布到公共 npm:
119
+
120
+ ```bash
121
+ npm config get registry # 应为 https://registry.npmjs.org/
122
+ ```
123
+
124
+ 如需发布到私有源,先设置:
125
+
126
+ ```bash
127
+ npm config set registry https://your-private-registry/
128
+ ```
129
+
130
+ ### 3. 更新版本号
131
+
132
+ 遵循语义化版本(SemVer):
133
+
134
+ ```bash
135
+ npm version patch # 1.0.14 -> 1.0.15 修复
136
+ npm version minor # 1.0.14 -> 1.1.0 小特性
137
+ npm version major # 1.0.14 -> 2.0.0 破坏性变更
138
+ ```
139
+
140
+ ### 4. 发布
141
+
142
+ ```bash
143
+ npm publish
144
+ ```
145
+
146
+ > 发布前请确认:
147
+ > - `package.json` 的 `files` 已包含 `drres`、`drscript`、`build-templates`、`tvui` 等需要分发的目录;
148
+ > - 已执行构建,生成最新的 `drres`/`drscript` 产物;
149
+ > - 没有把 `.npmrc`、本地凭据、密钥提交进去。
150
+
151
+ ### 5. 在宿主项目更新
152
+
153
+ ```bash
154
+ npm update drgame-cc
155
+ # 或指定版本
156
+ npm install drgame-cc@1.0.15
157
+ ```
158
+
159
+ 安装后会自动触发 `postinstall`,把新资源同步到宿主项目。
160
+
161
+ ---
162
+
163
+ ## 五、常见问题
164
+
165
+ **Q:编辑器识别不到 `BtnExtra` 等组件?**
166
+ A:组件 `.ts` 源文件必须位于宿主 `assets/` 内且带 `.meta`。请确认发包时 `drscript`(含组件脚本与 `.meta`)已生成并纳入 `files`,安装后确实出现在 `<CocosRoot>/assets/drscript` 下。
167
+
168
+ **Q:postinstall 提示源目录不存在?**
169
+ A:`drres`/`drscript` 为空或不存在,通常是未执行构建生成产物。先在包内生成这两个目录再发包。
170
+
171
+ **Q:反向同步后 uuid 变了?**
172
+ A:`sync-back` 会把宿主项目的 `.meta`(含其 uuid)覆盖回包内。这是预期行为,便于保持一致;如不需要,可只同步 `.ts`/资源而忽略 `.meta`。
@@ -56,7 +56,7 @@
56
56
  <!-- <img id="age-rating" src="16+.png" alt="16+"> -->
57
57
  </div>
58
58
  <script src="src/settings.js" charset="utf-8"></script>
59
- <script src="vconsole.min.js"></script>
59
+ <!-- <script src="vconsole.min.js"></script> -->
60
60
  <script src="main.js" charset="utf-8"></script>
61
61
 
62
62
  <script type="text/javascript">
@@ -6,6 +6,9 @@ export class KeyAdapter {
6
6
  private static keyHandler: ((event: { key: KeyCode, action: KeyAction }) => void) | null = null;
7
7
  private static keyboardBound: boolean = false;
8
8
 
9
+ /** 兼容处理:保存原有 onNativeBridgeCallback,避免覆盖 */
10
+ private static originalCallback: ((action, data) => void) | null = null;
11
+
9
12
  /** 虚拟手柄模式:激活时忽略键盘/遥控器/AdSdk 输入 */
10
13
  private static _virtualGamepadActive: boolean = false;
11
14
 
@@ -61,7 +64,16 @@ export class KeyAdapter {
61
64
 
62
65
  cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, KeyAdapter.onKeyboardDown, KeyAdapter);
63
66
  cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, KeyAdapter.onKeyboardUp, KeyAdapter);
64
- window['onNativeBridgeCallback'] = this.onMessage.bind(this);
67
+
68
+ // 兼容处理:保存已有回调,注册包装回调,避免覆盖其他模块
69
+ this.originalCallback = window['onNativeBridgeCallback'] || null;
70
+ const self = KeyAdapter;
71
+ window['onNativeBridgeCallback'] = function (action, data) {
72
+ if (self.originalCallback) {
73
+ self.originalCallback(action, data);
74
+ }
75
+ self.onMessage(action, data);
76
+ };
65
77
  KeyAdapter.keyboardBound = true;
66
78
  }
67
79
 
@@ -76,6 +88,11 @@ export class KeyAdapter {
76
88
  cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, KeyAdapter.onKeyboardUp, KeyAdapter);
77
89
  KeyAdapter.keyboardBound = false;
78
90
  }
91
+ // 兼容处理:销毁时恢复原有回调
92
+ if (KeyAdapter.originalCallback) {
93
+ window['onNativeBridgeCallback'] = KeyAdapter.originalCallback;
94
+ KeyAdapter.originalCallback = null;
95
+ }
79
96
  }
80
97
 
81
98
  private static onMessage(action, data) {
@@ -26,6 +26,9 @@ import {
26
26
  } from './types/interfaces';
27
27
  const KEY_EVENT_NAME = '__key_event__';
28
28
 
29
+ /** 排行榜/玩家数据共用的默认服务地址(所有 URL 的唯一出处) */
30
+ export const DEFAULT_SERVER_URL = 'https://lewo.daoran.com';
31
+
29
32
  /** 已注册自定义适配器的平台集合 */
30
33
  type AdapterConstructor = new () => IAdAdapter;
31
34
 
@@ -139,14 +142,14 @@ class AdSdkImpl implements IAdSdk {
139
142
 
140
143
  // 自动初始化排行榜模块(serverUrl/project/platform 由 ad-sdk 统一处理)
141
144
  Leaderboard.init({
142
- serverUrl: config.serverUrl,
145
+ serverUrl: config.serverUrl || DEFAULT_SERVER_URL,
143
146
  project: config.project,
144
147
  platform: config.gamePlatform,
145
148
  });
146
149
 
147
150
  // 自动初始化用户游戏数据模块(serverUrl/project/platform 由 ad-sdk 统一处理)
148
151
  UserGameData.init({
149
- serverUrl: config.serverUrl,
152
+ serverUrl: config.serverUrl || DEFAULT_SERVER_URL,
150
153
  project: config.project,
151
154
  platform: config.gamePlatform,
152
155
  });
@@ -12,6 +12,7 @@ import {
12
12
  IRankResult,
13
13
  IAddPointParams,
14
14
  } from '../types/interfaces';
15
+ import { DEFAULT_SERVER_URL } from '../ad-sdk';
15
16
 
16
17
  // ────────────────────────────────────────
17
18
  // 内部类型
@@ -62,7 +63,7 @@ const Leaderboard: LeaderboardLike = {
62
63
  /** 配置 */
63
64
  _config: {
64
65
  /** 服务基础域名 */
65
- serverUrl: 'https://hyp-test.huyingpai.com',
66
+ serverUrl: '',
66
67
  /** 排行榜服务地址 */
67
68
  rank_url: '',
68
69
  /** 添加积分服务地址 */
@@ -82,7 +83,7 @@ const Leaderboard: LeaderboardLike = {
82
83
  init: function (config: Partial<ILeaderboardConfig>) {
83
84
  config = config || {};
84
85
 
85
- this._config.serverUrl = (config.serverUrl || 'https://hyp-test.huyingpai.com').replace(/\/+$/, '');
86
+ this._config.serverUrl = (config.serverUrl || DEFAULT_SERVER_URL).replace(/\/+$/, '');
86
87
  this._config.rank_url = this._config.serverUrl + '/API_ACHIEVEMENT/api/user/point/rank';
87
88
  this._config.add_url = this._config.serverUrl + '/API_ACHIEVEMENT/api/user/point/add';
88
89
  this._config.project = config.project || '';
@@ -361,7 +361,7 @@ export interface IAdSdkInitConfig {
361
361
  platform: AdPlatform;
362
362
  /** CP 标识 */
363
363
  cpId?: string;
364
- /** 服务基础域名(排行榜/玩家数据/上报共用域名,不传则用默认测试域名) */
364
+ /** 服务基础域名(排行榜/玩家数据/上报共用域名,不传则用 ad-sdk 内共享的默认地址 DEFAULT_SERVER_URL) */
365
365
  serverUrl?: string;
366
366
  /** 项目编码(产品编码,排行榜/玩家数据统一使用) */
367
367
  project?: string;
@@ -12,6 +12,7 @@ import {
12
12
  IAddUserGameDataParams,
13
13
  IGetUserGameDataParams,
14
14
  } from '../types/interfaces';
15
+ import { DEFAULT_SERVER_URL } from '../ad-sdk';
15
16
 
16
17
  // ────────────────────────────────────────
17
18
  // 内部类型
@@ -67,7 +68,7 @@ const UserGameData: UserGameDataLike = {
67
68
  /** 配置 */
68
69
  _config: {
69
70
  /** 服务基础域名 */
70
- serverUrl: 'https://hyp-test.huyingpai.com',
71
+ serverUrl: '',
71
72
  /** 增加用户游戏数据服务地址 */
72
73
  add_url: '',
73
74
  /** 获取用户游戏数据服务地址 */
@@ -87,7 +88,7 @@ const UserGameData: UserGameDataLike = {
87
88
  init: function (config: Partial<IUserGameDataConfig>) {
88
89
  config = config || {};
89
90
 
90
- this._config.serverUrl = (config.serverUrl || 'https://hyp-test.huyingpai.com').replace(/\/+$/, '');
91
+ this._config.serverUrl = (config.serverUrl || DEFAULT_SERVER_URL).replace(/\/+$/, '');
91
92
  this._config.add_url = this._config.serverUrl + '/API_USER/user/gamedata/add';
92
93
  this._config.get_url = this._config.serverUrl + '/API_USER/user/gamedata/get';
93
94
  this._config.project = config.project || '';
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "drgame-cc",
3
- "version": "1.0.67",
3
+ "version": "1.0.70",
4
4
  "description": "道然游戏适配包",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "bin": {
8
- "drgame-cc": "./bin/drgame-cc.js"
9
- },
10
7
  "scripts": {
11
8
  "postinstall": "node scripts/postinstall.js && echo '完成'"
12
9
  },
@@ -48,4 +45,4 @@
48
45
  "engines": {
49
46
  "node": ">=18.0.0"
50
47
  }
51
- }
48
+ }
@@ -1,99 +1,99 @@
1
- #!/usr/bin/env node
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const fse = require('fs-extra');
6
-
7
- // ============================================================
8
- // 1. 包自身根目录(始终指向 drgame-cc 包目录)
9
- // ============================================================
10
- const pkgRoot = path.resolve(__dirname, '..');
11
-
12
- // 宿主项目根目录(npm install 时 = node_modules 的父目录)
13
- const projectRoot = path.resolve(__dirname, '..', '..', '..');
14
-
15
- // ============================================================
16
- // 2. 解析命令行参数,支持以下形式指定 Cocos 项目根目录:
17
- // node scripts/postinstall.js <path>
18
- // node scripts/postinstall.js --cocos-path <path>
19
- // node scripts/postinstall.js --cocos-path=<path>
20
- // node scripts/postinstall.js -p <path>
21
- // 优先级:命令行参数 > 默认(node_modules 父目录)
22
- // ============================================================
23
- function resolveCocosPath(argv) {
24
- const args = argv.slice(2);
25
- for (let i = 0; i < args.length; i++) {
26
- const a = args[i];
27
- if (a.startsWith('--cocos-path=')) {
28
- return a.slice('--cocos-path='.length);
29
- }
30
- if (a === '--cocos-path' || a === '-p') {
31
- const next = args[i + 1];
32
- if (next && !next.startsWith('-')) {
33
- return next;
34
- }
35
- } else if (!a.startsWith('-') && a.length > 0) {
36
- // 第一个非选项位置参数
37
- return a;
38
- }
39
- }
40
- return null;
41
- }
42
-
43
- const argPath = resolveCocosPath(process.argv);
44
- let COCOS_ROOT = argPath || projectRoot;
45
- // 相对路径相对于当前工作目录解析
46
- if (!path.isAbsolute(COCOS_ROOT)) {
47
- COCOS_ROOT = path.resolve(process.cwd(), COCOS_ROOT);
48
- }
49
-
50
- console.log('✅ 开始拷贝资源');
51
- console.log(`📦 宿主项目: ${projectRoot}`);
52
- console.log(`📦 目标目录: ${COCOS_ROOT}`);
53
- console.log(`🎯 Cocos 路径来源: ${argPath ? '命令行参数' : '默认(node_modules 父目录)'}`);
54
-
55
- // ============================================================
56
- // 3. 拷贝资源
57
- // ============================================================
58
- try {
59
- // build-templates
60
- const buildTemplatesSrc = path.join(pkgRoot, 'build-templates');
61
- const buildTemplatesDest = path.join(COCOS_ROOT, 'build-templates');
62
-
63
- if (fs.existsSync(buildTemplatesSrc)) {
64
- fse.copySync(buildTemplatesSrc, buildTemplatesDest, { overwrite: true });
65
- console.log('✅ build-templates 拷贝完成');
66
- } else {
67
- console.log(`⚠️ 源目录不存在: ${buildTemplatesSrc}`);
68
- }
69
-
70
- // drres
71
- const drresSrc = path.join(pkgRoot, 'drres');
72
- const drresDest = path.join(COCOS_ROOT, 'assets', 'resources', 'drres');
73
-
74
- if (fs.existsSync(drresSrc)) {
75
- fse.ensureDirSync(drresDest);
76
- fse.copySync(drresSrc, drresDest, { overwrite: true });
77
- console.log('✅ drres 拷贝完成');
78
- } else {
79
- console.log(`⚠️ 源目录不存在: ${drresSrc}`);
80
- }
81
-
82
- // drscript
83
- const drscriptSrc = path.join(pkgRoot, 'drscript');
84
- const drscriptDest = path.join(COCOS_ROOT, 'assets', 'drscript');
85
-
86
- if (fs.existsSync(drscriptSrc)) {
87
- fse.ensureDirSync(drscriptDest);
88
- fse.copySync(drscriptSrc, drscriptDest, { overwrite: true });
89
- console.log('✅ drscript 拷贝完成');
90
- } else {
91
- console.log(`⚠️ 源目录不存在: ${drscriptSrc}`);
92
- }
93
-
94
- console.log('🎉 资源拷贝完成!');
95
- } catch (error) {
96
- console.error('❌ 资源拷贝失败:', error.message);
97
- console.error(error.stack);
98
- process.exit(1);
99
- }
1
+ #!/usr/bin/env node
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fse = require('fs-extra');
6
+
7
+ // ============================================================
8
+ // 1. 包自身根目录(始终指向 drgame-cc 包目录)
9
+ // ============================================================
10
+ const pkgRoot = path.resolve(__dirname, '..');
11
+
12
+ // 宿主项目根目录(npm install 时 = node_modules 的父目录)
13
+ const projectRoot = path.resolve(__dirname, '..', '..', '..');
14
+
15
+ // ============================================================
16
+ // 2. 解析命令行参数,支持以下形式指定 Cocos 项目根目录:
17
+ // node scripts/postinstall.js <path>
18
+ // node scripts/postinstall.js --cocos-path <path>
19
+ // node scripts/postinstall.js --cocos-path=<path>
20
+ // node scripts/postinstall.js -p <path>
21
+ // 优先级:命令行参数 > 默认(node_modules 父目录)
22
+ // ============================================================
23
+ function resolveCocosPath(argv) {
24
+ const args = argv.slice(2);
25
+ for (let i = 0; i < args.length; i++) {
26
+ const a = args[i];
27
+ if (a.startsWith('--cocos-path=')) {
28
+ return a.slice('--cocos-path='.length);
29
+ }
30
+ if (a === '--cocos-path' || a === '-p') {
31
+ const next = args[i + 1];
32
+ if (next && !next.startsWith('-')) {
33
+ return next;
34
+ }
35
+ } else if (!a.startsWith('-') && a.length > 0) {
36
+ // 第一个非选项位置参数
37
+ return a;
38
+ }
39
+ }
40
+ return null;
41
+ }
42
+
43
+ const argPath = resolveCocosPath(process.argv);
44
+ let COCOS_ROOT = argPath || projectRoot;
45
+ // 相对路径相对于当前工作目录解析
46
+ if (!path.isAbsolute(COCOS_ROOT)) {
47
+ COCOS_ROOT = path.resolve(process.cwd(), COCOS_ROOT);
48
+ }
49
+
50
+ console.log('✅ 开始拷贝资源');
51
+ console.log(`📦 宿主项目: ${projectRoot}`);
52
+ console.log(`📦 目标目录: ${COCOS_ROOT}`);
53
+ console.log(`🎯 Cocos 路径来源: ${argPath ? '命令行参数' : '默认(node_modules 父目录)'}`);
54
+
55
+ // ============================================================
56
+ // 3. 拷贝资源
57
+ // ============================================================
58
+ try {
59
+ // build-templates
60
+ const buildTemplatesSrc = path.join(pkgRoot, 'build-templates');
61
+ const buildTemplatesDest = path.join(COCOS_ROOT, 'build-templates');
62
+
63
+ if (fs.existsSync(buildTemplatesSrc)) {
64
+ fse.copySync(buildTemplatesSrc, buildTemplatesDest, { overwrite: true });
65
+ console.log('✅ build-templates 拷贝完成');
66
+ } else {
67
+ console.log(`⚠️ 源目录不存在: ${buildTemplatesSrc}`);
68
+ }
69
+
70
+ // drres
71
+ const drresSrc = path.join(pkgRoot, 'drres');
72
+ const drresDest = path.join(COCOS_ROOT, 'assets', 'resources', 'drres');
73
+
74
+ if (fs.existsSync(drresSrc)) {
75
+ fse.ensureDirSync(drresDest);
76
+ fse.copySync(drresSrc, drresDest, { overwrite: true });
77
+ console.log('✅ drres 拷贝完成');
78
+ } else {
79
+ console.log(`⚠️ 源目录不存在: ${drresSrc}`);
80
+ }
81
+
82
+ // drscript
83
+ const drscriptSrc = path.join(pkgRoot, 'drscript');
84
+ const drscriptDest = path.join(COCOS_ROOT, 'assets', 'drscript');
85
+
86
+ if (fs.existsSync(drscriptSrc)) {
87
+ fse.ensureDirSync(drscriptDest);
88
+ fse.copySync(drscriptSrc, drscriptDest, { overwrite: true });
89
+ console.log('✅ drscript 拷贝完成');
90
+ } else {
91
+ console.log(`⚠️ 源目录不存在: ${drscriptSrc}`);
92
+ }
93
+
94
+ console.log('🎉 资源拷贝完成!');
95
+ } catch (error) {
96
+ console.error('❌ 资源拷贝失败:', error.message);
97
+ console.error(error.stack);
98
+ process.exit(1);
99
+ }
@@ -1,85 +1,85 @@
1
- #!/usr/bin/env node
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const fse = require('fs-extra');
6
-
7
- // ============================================================
8
- // 1. 包自身根目录 / 宿主项目根目录
9
- // ============================================================
10
- const pkgRoot = path.resolve(__dirname, '..');
11
- const projectRoot = path.resolve(__dirname, '..', '..', '..');
12
-
13
- // ============================================================
14
- // 2. 解析命令行参数(与 postinstall.js 一致)
15
- // 支持: <path> | --cocos-path <path> | --cocos-path=<path> | -p <path>
16
- // ============================================================
17
- function resolveCocosPath(argv) {
18
- const args = argv.slice(2);
19
- for (let i = 0; i < args.length; i++) {
20
- const a = args[i];
21
- if (a.startsWith('--cocos-path=')) {
22
- return a.slice('--cocos-path='.length);
23
- }
24
- if (a === '--cocos-path' || a === '-p') {
25
- const next = args[i + 1];
26
- if (next && !next.startsWith('-')) {
27
- return next;
28
- }
29
- } else if (!a.startsWith('-') && a.length > 0) {
30
- return a;
31
- }
32
- }
33
- return null;
34
- }
35
-
36
- const argPath = resolveCocosPath(process.argv);
37
- let COCOS_ROOT = argPath || projectRoot;
38
- if (!path.isAbsolute(COCOS_ROOT)) {
39
- COCOS_ROOT = path.resolve(process.cwd(), COCOS_ROOT);
40
- }
41
-
42
- console.log('开始反向同步(Cocos 项目 -> npm 包)');
43
- console.log(`源目录(宿主): ${COCOS_ROOT}`);
44
- console.log(`目标目录(包): ${pkgRoot}`);
45
- console.log(`Cocos 路径来源: ${argPath ? '命令行参数' : '默认(node_modules 父目录)'}`);
46
-
47
- // ============================================================
48
- // 3. 反向拷贝:与 postinstall.js 完全对称
49
- // COCOS_ROOT/build-templates -> pkgRoot/build-templates
50
- // COCOS_ROOT/assets/resources/drres -> pkgRoot/drres
51
- // COCOS_ROOT/assets/drscript -> pkgRoot/drscript
52
- // ============================================================
53
- const jobs = [
54
- {
55
- name: 'build-templates',
56
- src: path.join(COCOS_ROOT, 'build-templates'),
57
- dest: path.join(pkgRoot, 'build-templates'),
58
- },
59
- {
60
- name: 'drres',
61
- src: path.join(COCOS_ROOT, 'assets', 'resources', 'drres'),
62
- dest: path.join(pkgRoot, 'drres'),
63
- },
64
- {
65
- name: 'drscript',
66
- src: path.join(COCOS_ROOT, 'assets', 'drscript'),
67
- dest: path.join(pkgRoot, 'drscript'),
68
- },
69
- ];
70
-
71
- try {
72
- for (const job of jobs) {
73
- if (fs.existsSync(job.src)) {
74
- fse.copySync(job.src, job.dest, { overwrite: true });
75
- console.log(`[OK] ${job.name} 反向同步完成`);
76
- } else {
77
- console.log(`[SKIP] 源目录不存在,跳过: ${job.src}`);
78
- }
79
- }
80
- console.log('反向同步完成!');
81
- } catch (error) {
82
- console.error('反向同步失败:', error.message);
83
- console.error(error.stack);
84
- process.exit(1);
85
- }
1
+ #!/usr/bin/env node
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fse = require('fs-extra');
6
+
7
+ // ============================================================
8
+ // 1. 包自身根目录 / 宿主项目根目录
9
+ // ============================================================
10
+ const pkgRoot = path.resolve(__dirname, '..');
11
+ const projectRoot = path.resolve(__dirname, '..', '..', '..');
12
+
13
+ // ============================================================
14
+ // 2. 解析命令行参数(与 postinstall.js 一致)
15
+ // 支持: <path> | --cocos-path <path> | --cocos-path=<path> | -p <path>
16
+ // ============================================================
17
+ function resolveCocosPath(argv) {
18
+ const args = argv.slice(2);
19
+ for (let i = 0; i < args.length; i++) {
20
+ const a = args[i];
21
+ if (a.startsWith('--cocos-path=')) {
22
+ return a.slice('--cocos-path='.length);
23
+ }
24
+ if (a === '--cocos-path' || a === '-p') {
25
+ const next = args[i + 1];
26
+ if (next && !next.startsWith('-')) {
27
+ return next;
28
+ }
29
+ } else if (!a.startsWith('-') && a.length > 0) {
30
+ return a;
31
+ }
32
+ }
33
+ return null;
34
+ }
35
+
36
+ const argPath = resolveCocosPath(process.argv);
37
+ let COCOS_ROOT = argPath || projectRoot;
38
+ if (!path.isAbsolute(COCOS_ROOT)) {
39
+ COCOS_ROOT = path.resolve(process.cwd(), COCOS_ROOT);
40
+ }
41
+
42
+ console.log('开始反向同步(Cocos 项目 -> npm 包)');
43
+ console.log(`源目录(宿主): ${COCOS_ROOT}`);
44
+ console.log(`目标目录(包): ${pkgRoot}`);
45
+ console.log(`Cocos 路径来源: ${argPath ? '命令行参数' : '默认(node_modules 父目录)'}`);
46
+
47
+ // ============================================================
48
+ // 3. 反向拷贝:与 postinstall.js 完全对称
49
+ // COCOS_ROOT/build-templates -> pkgRoot/build-templates
50
+ // COCOS_ROOT/assets/resources/drres -> pkgRoot/drres
51
+ // COCOS_ROOT/assets/drscript -> pkgRoot/drscript
52
+ // ============================================================
53
+ const jobs = [
54
+ {
55
+ name: 'build-templates',
56
+ src: path.join(COCOS_ROOT, 'build-templates'),
57
+ dest: path.join(pkgRoot, 'build-templates'),
58
+ },
59
+ {
60
+ name: 'drres',
61
+ src: path.join(COCOS_ROOT, 'assets', 'resources', 'drres'),
62
+ dest: path.join(pkgRoot, 'drres'),
63
+ },
64
+ {
65
+ name: 'drscript',
66
+ src: path.join(COCOS_ROOT, 'assets', 'drscript'),
67
+ dest: path.join(pkgRoot, 'drscript'),
68
+ },
69
+ ];
70
+
71
+ try {
72
+ for (const job of jobs) {
73
+ if (fs.existsSync(job.src)) {
74
+ fse.copySync(job.src, job.dest, { overwrite: true });
75
+ console.log(`[OK] ${job.name} 反向同步完成`);
76
+ } else {
77
+ console.log(`[SKIP] 源目录不存在,跳过: ${job.src}`);
78
+ }
79
+ }
80
+ console.log('反向同步完成!');
81
+ } catch (error) {
82
+ console.error('反向同步失败:', error.message);
83
+ console.error(error.stack);
84
+ process.exit(1);
85
+ }