drgame-cc 1.0.0 → 1.0.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.
@@ -0,0 +1,152 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * npm install 后自动执行的脚本
5
+ * 自动将 build-templates 拷贝到 Cocos Creator 项目根目录
6
+ */
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ // 颜色输出
12
+ const colors = {
13
+ reset: '\x1b[0m',
14
+ green: '\x1b[32m',
15
+ yellow: '\x1b[33m',
16
+ red: '\x1b[31m',
17
+ cyan: '\x1b[36m'
18
+ };
19
+
20
+ function log(message, color = 'reset') {
21
+ console.log(`${colors[color]}${message}${colors.reset}`);
22
+ }
23
+
24
+ /**
25
+ * 递归拷贝目录
26
+ */
27
+ function copyDir(src, dest) {
28
+ if (!fs.existsSync(src)) {
29
+ return false;
30
+ }
31
+
32
+ if (!fs.existsSync(dest)) {
33
+ fs.mkdirSync(dest, { recursive: true });
34
+ }
35
+
36
+ const entries = fs.readdirSync(src, { withFileTypes: true });
37
+
38
+ for (const entry of entries) {
39
+ const srcPath = path.join(src, entry.name);
40
+ const destPath = path.join(dest, entry.name);
41
+
42
+ if (entry.isDirectory()) {
43
+ copyDir(srcPath, destPath);
44
+ } else {
45
+ fs.copyFileSync(srcPath, destPath);
46
+ }
47
+ }
48
+
49
+ return true;
50
+ }
51
+
52
+ /**
53
+ * 查找项目根目录(向上查找包含 assets 目录的目录)
54
+ */
55
+ function findProjectRoot(startDir) {
56
+ let currentDir = startDir;
57
+ log(`开始从 ${startDir} 查找项目根目录...`, 'yellow');
58
+
59
+ while (currentDir !== path.dirname(currentDir)) {
60
+ log(` 检查: ${currentDir}`, 'reset');
61
+
62
+ // 检查是否存在 assets 目录(Cocos Creator 项目的标志)
63
+ const hasAssets = fs.existsSync(path.join(currentDir, 'assets'));
64
+ const hasAssetsUpper = fs.existsSync(path.join(currentDir, 'Assets'));
65
+
66
+ if (hasAssets || hasAssetsUpper) {
67
+ log(` ✓ 找到 assets 目录!`, 'green');
68
+ return currentDir;
69
+ }
70
+ currentDir = path.dirname(currentDir);
71
+ }
72
+
73
+ log(` ✗ 未找到项目根目录`, 'red');
74
+ return null;
75
+ }
76
+
77
+ /**
78
+ * 主函数
79
+ */
80
+ function main() {
81
+ log('========================================', 'cyan');
82
+ log('🚀 drgame-cc 安装后自动配置', 'cyan');
83
+ log('========================================', 'cyan');
84
+
85
+ // 获取当前工作目录
86
+ const cwd = process.cwd();
87
+ log(`当前目录: ${cwd}`, 'yellow');
88
+ log(`脚本目录: ${__dirname}`, 'yellow');
89
+ log(`包目录: ${path.resolve(__dirname, '..')}`, 'yellow');
90
+
91
+ // 查找项目根目录
92
+ const projectRoot = findProjectRoot(cwd);
93
+
94
+ if (!projectRoot) {
95
+ log('⚠️ 未找到 Cocos Creator 项目根目录(未找到 assets 目录)', 'yellow');
96
+ log('跳过自动拷贝 build-templates', 'yellow');
97
+ return;
98
+ }
99
+
100
+ log(`找到项目根目录: ${projectRoot}`, 'green');
101
+
102
+ // 获取包内的 build-templates 路径
103
+ // __dirname 是 scripts 目录,向上两级到包根目录
104
+ const packageDir = path.resolve(__dirname, '..');
105
+ const sourceDir = path.join(packageDir, 'build-templates');
106
+ const targetDir = path.join(projectRoot, 'build-templates');
107
+
108
+ log(`\n📁 源目录: ${sourceDir}`, 'yellow');
109
+ log(`📁 目标目录: ${targetDir}`, 'yellow');
110
+
111
+ // 检查源目录是否存在
112
+ if (!fs.existsSync(sourceDir)) {
113
+ log('\n⚠️ 包内没有 build-templates 目录,跳过拷贝', 'yellow');
114
+ return;
115
+ }
116
+
117
+ // 检查目标目录是否已存在
118
+ if (fs.existsSync(targetDir)) {
119
+ log('\n⚠️ 项目根目录已存在 build-templates,跳过拷贝(避免覆盖)', 'yellow');
120
+ return;
121
+ }
122
+
123
+ // 执行拷贝
124
+ log('\n📋 开始拷贝 build-templates...', 'cyan');
125
+ const success = copyDir(sourceDir, targetDir);
126
+
127
+ if (success) {
128
+ log('\n✅ build-templates 拷贝成功!', 'green');
129
+ log(`📂 位置: ${targetDir}`, 'green');
130
+
131
+ // 显示拷贝的内容
132
+ const items = fs.readdirSync(targetDir);
133
+ log(`\n📄 包含 ${items.length} 个文件/目录:`, 'cyan');
134
+ items.forEach(item => {
135
+ log(` - ${item}`, 'reset');
136
+ });
137
+ } else {
138
+ log('\n❌ 拷贝失败', 'red');
139
+ }
140
+
141
+ log('\n========================================', 'cyan');
142
+ log('🎉 安装完成!', 'green');
143
+ log('========================================', 'cyan');
144
+ }
145
+
146
+ // 执行主函数
147
+ try {
148
+ main();
149
+ } catch (error) {
150
+ console.error('❌ 安装后脚本执行失败:', error);
151
+ // 不退出进程,避免影响 npm install
152
+ }