cara membuat aplikasi exe - wongganteng10/tutorial GitHub Wiki

To convert your HTML currency converter code into a standalone executable for Windows (e.g., converter-mata-uang.exe) that can run on an Ubuntu server using VirtualBox, you'll need to follow several steps. The process involves creating a simple desktop application with a graphical interface that can be compiled into an executable format. Here’s a high-level overview of the process:

  1. Choose a Development Framework: Use a cross-platform framework like Electron or a GUI library like PyQt or Tkinter for Python.

  2. Create the Application Code: Adapt the HTML/CSS/JavaScript into a desktop application using the chosen framework.

  3. Compile the Application: Use tools provided by the framework to compile the application into an executable.

Here’s how you can do this with Electron, a popular framework for building desktop applications with web technologies:

1. Set Up Your Development Environment

First, you need Node.js and npm installed. You can install them on your Ubuntu server if they’re not already present:

sudo apt update
sudo apt install nodejs npm
sudo apt install wine64 wine32
wine --version

2. Install Electron

Create a new directory for your project and install Electron:

mkdir currency-converter
cd currency-converter
npm init -y
npm install electron --save-dev

3. Set Up Your Project Structure

Create the following files in your currency-converter directory:

  • main.js (Main process)
  • index.html (Your HTML code)
  • styles.css (Your CSS code, if needed)
  • renderer.js (JavaScript code for the renderer process)

4. Create main.js

This file will create a window to display your HTML file:

const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'renderer.js'),
            nodeIntegration: true
        }
    });

    mainWindow.loadFile('index.html');
}

app.whenReady().then(() => {
    createWindow();

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

5. Create renderer.js

Move your existing JavaScript code into this file:

// Data mata uang sederhana (sebagai contoh)
const rates = {
    'USD': 1,
    'EUR': 0.85,
    'IDR': 14600
};

const fromCurrency = document.getElementById('from-currency');
const toCurrency = document.getElementById('to-currency');
const amountInput = document.getElementById('amount');
const toAmount = document.getElementById('to-amount');
const form = document.getElementById('currency-form');
const historyList = document.getElementById('history-list');

// Mengisi dropdown mata uang
for (const currency in rates) {
    const optionFrom = document.createElement('option');
    optionFrom.value = currency;
    optionFrom.textContent = currency;
    fromCurrency.appendChild(optionFrom);

    const optionTo = document.createElement('option');
    optionTo.value = currency;
    optionTo.textContent = currency;
    toCurrency.appendChild(optionTo);
}

// Menangani form submit
form.addEventListener('submit', (e) => {
    e.preventDefault();
    const from = fromCurrency.value;
    const to = toCurrency.value;
    const amount = parseFloat(amountInput.value);

    if (isNaN(amount) || amount <= 0) {
        toAmount.value = 'Jumlah tidak valid';
        return;
    }

    const result = (amount / rates[from]) * rates[to];
    toAmount.value = result.toFixed(2);

    // Menambahkan histori konversi
    const historyItem = document.createElement('li');
    historyItem.textContent = `${amount} ${from} = ${result.toFixed(2)} ${to}`;
    historyList.insertBefore(historyItem, historyList.firstChild);
});

6. Modify index.html

Move your HTML code into this file and ensure it references styles.css and renderer.js if necessary.

7. Update package.json

Add a start script to package.json to run your Electron app:

"scripts": {
    "start": "electron ."
}

8. Run Your Application

You can now run your application:

npm start

9. Create an Executable

To create an executable file (converter-mata-uang.exe), use a tool like electron-builder:

npm install electron-builder --save-dev

Update your package.json with build configuration:

"build": {
    "appId": "com.example.currencyconverter",
    "files": [
        "index.html",
        "main.js",
        "renderer.js",
        "styles.css"
    ],
    "directories": {
        "buildResources": "assets"
    }
}

Build the application:

npx electron-builder --win

This will generate the .exe file in the dist directory.

You can now use VirtualBox to test your application in a Windows environment, or distribute it as needed.





membuat aplikasi Electron menjadi portabel

Untuk membuat aplikasi Electron menjadi portabel, Anda bisa mengikuti langkah-langkah berikut. Aplikasi portabel tidak memerlukan instalasi dan dapat dijalankan langsung dari folder tempat aplikasi tersebut diekstrak. Ini membuat distribusi dan penggunaan lebih fleksibel. Berikut adalah panduan untuk membuat aplikasi Electron portabel:

1. Siapkan Proyek Anda

Pastikan Anda sudah memiliki proyek Electron yang berfungsi dengan baik dan semua dependensi telah diinstal.

2. Tambahkan Konfigurasi Build

Gunakan electron-builder untuk membuat versi portabel dari aplikasi Anda. Instal electron-builder jika belum diinstal:

npm install electron-builder --save-dev

3. Update package.json

Tambahkan konfigurasi build di package.json untuk menghasilkan versi portabel. Berikut adalah contoh konfigurasi untuk membuat aplikasi portabel untuk Windows:

{
  "name": "currency-converter",
  "version": "1.0.0",
  "description": "A currency converter application",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "build": "electron-builder"
  },
  "build": {
    "appId": "com.example.currencyconverter",
    "productName": "Currency Converter",
    "files": [
      "index.html",
      "main.js",
      "renderer.js",
      "styles.css"
    ],
    "directories": {
      "output": "dist"
    },
    "win": {
      "target": [
        "nsis",
        "zip"
      ]
    }
  },
  "devDependencies": {
    "electron": "^XX.XX.XX",
    "electron-builder": "^XX.XX.XX"
  }
}

Dalam contoh ini:

  • nsis adalah installer yang dapat diinstal, tetapi zip adalah target portabel yang tidak memerlukan instalasi.
  • "directories": { "output": "dist" } menentukan folder output tempat hasil build akan disimpan.

4. Bangun Aplikasi Portabel

Jalankan perintah berikut untuk membuat versi portabel dari aplikasi Anda:

npm run build

5. Verifikasi dan Distribusi

  • Windows: Setelah build selesai, periksa folder dist untuk file zip yang berisi aplikasi portabel Anda. Ekstrak file zip di folder manapun, dan Anda akan menemukan file executable yang dapat dijalankan langsung tanpa instalasi.

  • macOS: Jika Anda juga menargetkan macOS, Anda akan mendapatkan file .app di folder dist, yang dapat dijalankan langsung atau dikemas dalam file zip untuk distribusi portabel.

  • Linux: Untuk Linux, Anda mungkin mendapatkan file binari atau arsip yang dapat diekstrak dan dijalankan secara langsung.

6. Testing

Pastikan untuk menguji aplikasi portabel di berbagai lingkungan untuk memastikan bahwa aplikasi berjalan dengan baik dan tidak memiliki masalah dependensi atau konfigurasi yang hilang.

Dengan langkah-langkah ini, Anda akan mendapatkan aplikasi Electron portabel yang dapat didistribusikan dan dijalankan tanpa memerlukan proses instalasi tambahan.