| 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 { |
| |
| await fs.ensureDir(distDir); |
|
|
| |
| 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)) { |
| |
| 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}`); |
| } |
| } |
|
|
| |
| 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(); |
|
|