Skip to content

Commit ae11623

Browse files
authored
Merge pull request #41 from Unity-Lab-AI/develop
Refactor: Switch copy-assets.js to blacklist approach
2 parents 565134d + 85faaf0 commit ae11623

File tree

1 file changed

+128
-80
lines changed

1 file changed

+128
-80
lines changed

copy-assets.js

Lines changed: 128 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,156 @@
22

33
/**
44
* Copy Additional Assets to Dist
5-
* Copies files that Vite doesn't process but are needed for the site
5+
* Uses BLACKLIST approach - copies everything EXCEPT excluded items
6+
* Vite handles HTML files, this copies all other assets
67
*/
78

89
const fs = require('fs');
910
const path = require('path');
1011

1112
const DIST_DIR = 'dist';
1213

13-
// Files and directories to copy
14-
const ASSETS_TO_COPY = [
15-
{ src: 'vendor', dest: 'vendor', type: 'dir' },
16-
{ src: 'fonts', dest: 'fonts', type: 'dir' },
17-
{ src: 'PolliLibJS', dest: 'PolliLibJS', type: 'dir' },
18-
{ src: 'favicon.ico', dest: 'favicon.ico', type: 'file' },
19-
{ src: 'robots.txt', dest: 'robots.txt', type: 'file' },
20-
{ src: 'sitemap.xml', dest: 'sitemap.xml', type: 'file' },
21-
{ src: 'BingSiteAuth.xml', dest: 'BingSiteAuth.xml', type: 'file' },
22-
{ src: '_headers', dest: '_headers', type: 'file' },
23-
{ src: 'script.js', dest: 'script.js', type: 'file' },
24-
{ src: 'visitor-tracking.js', dest: 'visitor-tracking.js', type: 'file' },
25-
{ src: 'about/about.js', dest: 'about/about.js', type: 'file' },
26-
{ src: 'ai/demo/age-verification.js', dest: 'ai/demo/age-verification.js', type: 'file' },
27-
{ src: 'ai/demo/js/main.js', dest: 'ai/demo/js/main.js', type: 'file' },
28-
{ src: 'apps/age-verification.js', dest: 'apps/age-verification.js', type: 'file' },
29-
// Apps subdirectories (apps/index.html is handled by Vite)
30-
{ src: 'apps/helperInterfaceDemo', dest: 'apps/helperInterfaceDemo', type: 'dir' },
31-
{ src: 'apps/oldSiteProject', dest: 'apps/oldSiteProject', type: 'dir' },
32-
{ src: 'apps/personaDemo', dest: 'apps/personaDemo', type: 'dir' },
33-
{ src: 'apps/screensaverDemo', dest: 'apps/screensaverDemo', type: 'dir' },
34-
{ src: 'apps/slideshowDemo', dest: 'apps/slideshowDemo', type: 'dir' },
35-
{ src: 'apps/talkingWithUnity', dest: 'apps/talkingWithUnity', type: 'dir' },
36-
{ src: 'apps/textDemo', dest: 'apps/textDemo', type: 'dir' },
37-
{ src: 'apps/unityDemo', dest: 'apps/unityDemo', type: 'dir' },
38-
{ src: 'apps/shared-nav.html', dest: 'apps/shared-nav.html', type: 'file' },
39-
{ src: 'apps/shared-nav.js', dest: 'apps/shared-nav.js', type: 'file' },
40-
{ src: 'apps/shared-theme.css', dest: 'apps/shared-theme.css', type: 'file' },
41-
{ src: 'apps/update-apps.sh', dest: 'apps/update-apps.sh', type: 'file' },
14+
// BLACKLIST: Files and directories to EXCLUDE from copying
15+
const EXCLUDE = [
16+
// Build/Dev folders
17+
'node_modules',
18+
'dist',
19+
'.git',
20+
'.github',
21+
'.vscode',
22+
23+
// Config files (not needed in production)
24+
'vite.config.js',
25+
'package.json',
26+
'package-lock.json',
27+
'copy-assets.js',
28+
'cache-bust.js',
29+
'generate-sitemap.js',
30+
'.gitignore',
31+
'.gitattributes',
32+
'.eslintrc.js',
33+
'.prettierrc',
34+
'tsconfig.json',
35+
'jsconfig.json',
36+
37+
// Documentation (not needed in production)
38+
'CLAUDE.md',
39+
'README.md',
40+
'Docs',
41+
42+
// Archived/legacy content (not needed in production)
43+
'Archived',
44+
'playwright-report',
45+
46+
// Python library (not needed for web)
47+
'PolliLibPy',
48+
49+
// Minified versions (originals are fine, Vite handles optimization)
50+
'styles.min.css',
51+
'script.min.js',
52+
53+
// Test files
54+
'tests',
55+
'test',
56+
'*.test.js',
57+
'*.spec.js',
58+
59+
// Temp/cache files
60+
'.DS_Store',
61+
'Thumbs.db',
62+
'*.log',
63+
'*.tmp',
64+
];
65+
66+
// File extensions to always exclude
67+
const EXCLUDE_EXTENSIONS = [
68+
'.md', // Markdown docs (except in specific folders we want)
69+
'.log',
70+
'.tmp',
4271
];
4372

4473
/**
45-
* Copy directory recursively
74+
* Check if a path should be excluded
4675
*/
47-
function copyDir(src, dest) {
48-
if (!fs.existsSync(src)) {
49-
console.log(` ⚠️ Skipping ${src} (not found)`);
50-
return false;
76+
function shouldExclude(itemPath, itemName) {
77+
// Check exact matches in exclude list
78+
if (EXCLUDE.includes(itemName)) {
79+
return true;
5180
}
5281

53-
fs.mkdirSync(dest, { recursive: true });
54-
55-
const entries = fs.readdirSync(src, { withFileTypes: true });
82+
// Check if it's a hidden file/folder (starts with .)
83+
if (itemName.startsWith('.') && itemName !== '.htaccess' && itemName !== '_headers') {
84+
return true;
85+
}
5686

57-
for (const entry of entries) {
58-
const srcPath = path.join(src, entry.name);
59-
const destPath = path.join(dest, entry.name);
87+
// Check excluded extensions
88+
const ext = path.extname(itemName).toLowerCase();
89+
if (EXCLUDE_EXTENSIONS.includes(ext)) {
90+
return true;
91+
}
6092

61-
if (entry.isDirectory()) {
62-
copyDir(srcPath, destPath);
63-
} else {
64-
fs.copyFileSync(srcPath, destPath);
93+
// Check glob patterns in exclude list
94+
for (const pattern of EXCLUDE) {
95+
if (pattern.startsWith('*') && itemName.endsWith(pattern.slice(1))) {
96+
return true;
6597
}
6698
}
6799

68-
return true;
100+
return false;
101+
}
102+
103+
/**
104+
* Check if file already exists in dist (Vite already processed it)
105+
*/
106+
function alreadyInDist(relativePath) {
107+
const distPath = path.join(DIST_DIR, relativePath);
108+
return fs.existsSync(distPath);
69109
}
70110

71111
/**
72-
* Copy file
112+
* Copy directory recursively with exclusions
73113
*/
74-
function copyFile(src, dest) {
114+
function copyDirRecursive(src, dest, relativePath = '') {
115+
let copiedCount = 0;
116+
75117
if (!fs.existsSync(src)) {
76-
console.log(` ⚠️ Skipping ${src} (not found)`);
77-
return false;
118+
return copiedCount;
119+
}
120+
121+
const entries = fs.readdirSync(src, { withFileTypes: true });
122+
123+
for (const entry of entries) {
124+
const srcPath = path.join(src, entry.name);
125+
const destPath = path.join(dest, entry.name);
126+
const relPath = path.join(relativePath, entry.name);
127+
128+
// Check exclusions
129+
if (shouldExclude(srcPath, entry.name)) {
130+
continue;
131+
}
132+
133+
if (entry.isDirectory()) {
134+
// Create directory and recurse
135+
fs.mkdirSync(destPath, { recursive: true });
136+
copiedCount += copyDirRecursive(srcPath, destPath, relPath);
137+
} else {
138+
// Skip if already exists in dist (Vite handled it)
139+
if (!alreadyInDist(relPath)) {
140+
fs.mkdirSync(dest, { recursive: true });
141+
fs.copyFileSync(srcPath, destPath);
142+
copiedCount++;
143+
}
144+
}
78145
}
79146

80-
const destDir = path.dirname(dest);
81-
fs.mkdirSync(destDir, { recursive: true });
82-
fs.copyFileSync(src, dest);
83-
return true;
147+
return copiedCount;
84148
}
85149

86150
/**
87151
* Main execution
88152
*/
89153
function main() {
90-
console.log('📋 Copying additional assets to dist...');
154+
console.log('📋 Copying assets to dist (blacklist mode)...');
91155
console.log('');
92156

93157
// Check if dist exists
@@ -97,35 +161,19 @@ function main() {
97161
process.exit(1);
98162
}
99163

100-
let copiedCount = 0;
101-
let skippedCount = 0;
102-
103-
// Copy each asset
104-
for (const asset of ASSETS_TO_COPY) {
105-
const srcPath = asset.src;
106-
const destPath = path.join(DIST_DIR, asset.dest);
107-
108-
console.log(` 📁 ${asset.src}${asset.dest}`);
109-
110-
let success;
111-
if (asset.type === 'dir') {
112-
success = copyDir(srcPath, destPath);
113-
} else {
114-
success = copyFile(srcPath, destPath);
115-
}
116-
117-
if (success) {
118-
copiedCount++;
119-
console.log(` ✅ Copied`);
120-
} else {
121-
skippedCount++;
122-
}
164+
console.log(' 🚫 Excluded patterns:');
165+
EXCLUDE.slice(0, 10).forEach(item => console.log(` - ${item}`));
166+
if (EXCLUDE.length > 10) {
167+
console.log(` ... and ${EXCLUDE.length - 10} more`);
123168
}
169+
console.log('');
170+
171+
// Copy everything from root, respecting exclusions
172+
const copiedCount = copyDirRecursive('.', DIST_DIR);
124173

125174
console.log('');
126175
console.log(`✅ Asset copying complete!`);
127-
console.log(` Copied: ${copiedCount}`);
128-
console.log(` Skipped: ${skippedCount}`);
176+
console.log(` Files copied: ${copiedCount}`);
129177
}
130178

131179
// Run

0 commit comments

Comments
 (0)