File size: 1,766 Bytes
51bdde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const sourceDir = path.join(__dirname, '../unoptimized-js');
const outputDir = path.join(__dirname, '../dist/js');

if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true });
}

function processFile(filePath) {
  const relativePath = path.relative(sourceDir, filePath);
  const outputPath = path.join(outputDir, relativePath);
  const outputDirForFile = path.dirname(outputPath);

  // Create subdirectories in the output folder
  if (!fs.existsSync(outputDirForFile)) {
    fs.mkdirSync(outputDirForFile, { recursive: true });
  }

  try {
    // 1. Obfuscate the file
    console.log(`Obfuscating: ${filePath}`);
    const obfuscateCmd = `npx javascript-obfuscator "${filePath}" --output "${outputPath}"`;
    execSync(obfuscateCmd);

    // 2. Minify the obfuscated file using Terser
    console.log(`Minifying: ${outputPath}`);
    const minifyCmd = `npx terser "${outputPath}" -o "${outputPath}" --compress --mangle`;
    execSync(minifyCmd);

    console.log(`Success: ${outputPath}`);
  } catch (error) {
    console.error(`Error processing file ${filePath}:`, error.message);
  }
}

function processDirectory(dirPath) {
  fs.readdirSync(dirPath).forEach((file) => {
    const filePath = path.join(dirPath, file);
    const stats = fs.statSync(filePath);

    if (stats.isDirectory()) {
      processDirectory(filePath);
    } else if (path.extname(filePath) === '.js') {
      if (filePath !== path.join(__dirname, 'build.js')) {
        processFile(filePath);
      }
    }
  });
}

console.log('Starting JavaScript build process...');
processDirectory(sourceDir);
console.log('Build complete. Files are in the /dist/js folder.');