Back

Netlify Deploy

81 views
rapp Code by: @rapp

Description

Base url: https://netlify.com Features that make it easier for you to deploy to Netlify

Features

Code (javascript)

netlify.js
import { unzipper } from 'unzipper';
import fetch from 'node-fetch';
import archiver from 'archiver';
import { Readable } from 'stream';

export default {
  name: 'nweb',
  command: ['nweb', 'netlifyweb'],
  description: 'Deploy website to Netlify',
  async execute(m, { conn, text, qmsg }) {
    if (!text) return m.reply('Penggunaan: .netlifyweb <namaWeb>');
    if (!qmsg || !/zip|html/.test(qmsg.mimetype)) return m.reply('Reply file .zip atau .html');

    const siteName = text.trim().toLowerCase().replace(/[^a-z0-9-_]/g, '');
    const quotedFile = await conn.downloadMediaMessage(qmsg);
    let zipBuffer;

    if (qmsg.mimetype.includes('zip')) {
      zipBuffer = Buffer.from(quotedFile);
      const directory = await unzipper.Open.buffer(zipBuffer);

      const hasIndexHtml = directory.files.some(file => 
        file.type === 'File' && file.path.toLowerCase().endsWith('index.html')
      );
      
      if (!hasIndexHtml) {
        return m.reply('File index.html tidak ditemukan dalam struktur ZIP.');
      }
    } else if (!qmsg.mimetype.includes('html')) {
      return m.reply('File tidak dikenali. Kirim file .zip atau .html.');
    }

    try {
      // Jika file HTML, buat ZIP terlebih dahulu
      let deployBody;
      if (qmsg.mimetype.includes('html')) {
        deployBody = await createZipFromHtml(quotedFile);
      } else {
        deployBody = zipBuffer;
      }

      const deployResponse = await fetch('https://api.netlify.com/api/v1/sites', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${global.nftoken}`,
          'Content-Type': 'application/zip'
        },
        body: deployBody
      });

      const deployData = await deployResponse.json();
      
      if (!deployResponse.ok) {
        console.error('Netlify Error:', deployData);
        return m.reply(`Gagal deploy ke Netlify:\n${deployData.message || JSON.stringify(deployData)}`);
      }

      m.reply(`āœ… Website berhasil dibuat!\n\n🌐 URL: ${deployData.ssl_url || deployData.url}\nšŸ”§ Admin: ${deployData.admin_url}`);
    } catch (error) {
      console.error('Netlify Deployment Error:', error);
      m.reply(`Terjadi kesalahan saat deploy ke Netlify: ${error.message}`);
    }
  }
};

function createZipFromHtml(htmlBuffer) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    const archive = archiver('zip', { zlib: { level: 9 } });
    
    archive.on('data', (chunk) => chunks.push(chunk));
    archive.on('end', () => resolve(Buffer.concat(chunks)));
    archive.on('error', reject);
    
    archive.append(htmlBuffer, { name: 'index.html' });
    archive.finalize();
  });
}