File size: 3,062 Bytes
57e8a04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const JavaScriptObfuscator = require('javascript-obfuscator');
const fs = require('fs-extra');
const path = require('path');

const srcDir = path.resolve(__dirname, '..');
const distDir = path.resolve(__dirname, '../dist');

async function build() {
    console.log('πŸš€ Starting "Hugging Face Shield" Build...');
    console.log(`   Source: ${srcDir}`);
    console.log(`   Destination: ${distDir}`);

    try {
        // 1. Ensure dist folder exists
        await fs.ensureDir(distDir);

        // 2. Define what to copy
        const toCopy = ['server', 'public', 'package.json', 'Dockerfile', '.dockerignore', '.env.example'];

        for (const item of toCopy) {
            const srcPath = path.join(srcDir, item);
            const destPath = path.join(distDir, item);
            
            if (fs.existsSync(srcPath)) {
                // Remove destination if it exists (but handle folder locks)
                try {
                    if (fs.existsSync(destPath)) {
                        await fs.remove(destPath);
                    }
                } catch (e) {
                    console.warn(`   ⚠️ Warning: Could not remove old ${item}, overwriting...`);
                }
                
                await fs.copy(srcPath, destPath);
                console.log(`βœ… Copied: ${item}`);
            } else {
                console.warn(`❌ Missing source: ${item}`);
            }
        }

        // 3. Obfuscate all JS files in dist
        console.log('πŸ”’ Scrambling code...');
        await obfuscateDirectory(path.join(distDir, 'server'));
        await obfuscateDirectory(path.join(distDir, 'public'));

        console.log('\n✨ BUILD COMPLETE! ✨');
        console.log('πŸ“ Your scrambled code is in the "dist" folder.');
        console.log('πŸ‘‰ Upload ONLY the contents of the "dist" folder to Hugging Face.');
    } catch (err) {
        console.error('❌ Build failed:', err);
    }
}

async function obfuscateDirectory(dir) {
    if (!fs.existsSync(dir)) return;
    const files = await fs.readdir(dir);

    for (const file of files) {
        const fullPath = path.join(dir, file);
        const stat = await fs.stat(fullPath);

        if (stat.isDirectory()) {
            await obfuscateDirectory(fullPath);
        } else if (file.endsWith('.js') && !file.includes('.min.js')) {
            const code = await fs.readFile(fullPath, 'utf8');
            const result = JavaScriptObfuscator.obfuscate(code, {
                compact: true,
                controlFlowFlattening: true,
                controlFlowFlatteningThreshold: 1,
                numbersToExpressions: true,
                simplify: true,
                stringArrayThreshold: 1,
                stringArrayRotate: true,
                stringArrayShuffle: true,
                stringArray: true,
                identifierNamesGenerator: 'hexadecimal'
            });

            await fs.writeFile(fullPath, result.getObfuscatedCode());
            console.log(`   - Scrambled: ${file}`);
        }
    }
}

build();