Commit 38be39a7 authored by Lukáš Gorazd Hrodek's avatar Lukáš Gorazd Hrodek
Browse files

style: Format deploy.js code with consistent quotes and indentation

parent 2cd737d3
Loading
Loading
Loading
Loading
+36 −25
Original line number Diff line number Diff line
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import fs from "fs";
import path from "path";
import { execSync } from "child_process";

const config = {
  WEB_HOST: process.env.WEB_HOST || 'festival.fi.muni.cz',
  WEB_USER: process.env.WEB_USER || 'gitlabworker',
  ARCHIVE_PATH: process.env.ARCHIVE_PATH || '/var/www/html/festival.fi.muni.cz/archiv',
  WEB_HOST: process.env.WEB_HOST || "festival.fi.muni.cz",
  WEB_USER: process.env.WEB_USER || "gitlabworker",
  ARCHIVE_PATH:
    process.env.ARCHIVE_PATH || "/var/www/html/festival.fi.muni.cz/archiv",
  SOURCE_DIR: process.cwd(),
  EXPORT_DIR: path.join(process.cwd(), 'web-archive'),
  EXPORT_DIR: path.join(process.cwd(), "web-archive"),
};

export function createTarArchive() {
  if (!fs.existsSync(config.EXPORT_DIR)) {
    throw new Error('Export directory not found. Run export first.');
    throw new Error("Export directory not found. Run export first.");
  }

  const archivePath = path.join(config.SOURCE_DIR, 'archive-content.tar.gz');
  const archivePath = path.join(config.SOURCE_DIR, "archive-content.tar.gz");
  if (fs.existsSync(archivePath)) {
    fs.unlinkSync(archivePath);
  }

  const command = process.platform === 'win32'
  const command =
    process.platform === "win32"
      ? `tar -czf archive-content.tar.gz -C "${config.EXPORT_DIR}" .`
      : `tar -czf archive-content.tar.gz -C ${config.EXPORT_DIR} .`;

  execSync(command, { stdio: 'pipe' });
  execSync(command, { stdio: "pipe" });
  return archivePath;
}

export function deployToServer(archivePath) {
  const remoteUser = `${config.WEB_USER}@${config.WEB_HOST}`;

  execSync(`scp "${archivePath}" ${remoteUser}:/tmp/archive-content.tar.gz`, { stdio: 'pipe' });
  execSync(`scp "${archivePath}" ${remoteUser}:/tmp/archive-content.tar.gz`, {
    stdio: "pipe",
  });

  const remoteCommands = `
    set -euo pipefail;
@@ -50,29 +54,34 @@ export function deployToServer(archivePath) {
    find ${config.ARCHIVE_PATH} -type f -exec chmod 644 {} + || true;
  `;

  execSync(`ssh ${remoteUser} "${remoteCommands}"`, { stdio: 'pipe' });
  execSync(`ssh ${remoteUser} "${remoteCommands}"`, { stdio: "pipe" });
}

export function deployLocally() {
  if (!fs.existsSync(config.EXPORT_DIR)) {
    throw new Error('Export directory not found. Run export first.');
    throw new Error("Export directory not found. Run export first.");
  }

  const localArchivePath = path.join(config.SOURCE_DIR, 'archiv');
  const localArchivePath = path.join(config.SOURCE_DIR, "archiv");

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

  const entries = fs.readdirSync(localArchivePath, { withFileTypes: true });
  entries.forEach(entry => {
  entries.forEach((entry) => {
    if (entry.isDirectory()) {
      fs.rmSync(path.join(localArchivePath, entry.name), { recursive: true, force: true });
      fs.rmSync(path.join(localArchivePath, entry.name), {
        recursive: true,
        force: true,
      });
    }
  });

  const exportEntries = fs.readdirSync(config.EXPORT_DIR, { withFileTypes: true });
  exportEntries.forEach(entry => {
  const exportEntries = fs.readdirSync(config.EXPORT_DIR, {
    withFileTypes: true,
  });
  exportEntries.forEach((entry) => {
    if (entry.isDirectory()) {
      const src = path.join(config.EXPORT_DIR, entry.name);
      const dest = path.join(localArchivePath, entry.name);
@@ -83,7 +92,7 @@ export function deployLocally() {

export function createBackup() {
  const remoteUser = `${config.WEB_USER}@${config.WEB_HOST}`;
  const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
  const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, -5);
  const backupName = `archive-backup-${timestamp}.tar.gz`;

  const backupCommands = `
@@ -92,13 +101,15 @@ export function createBackup() {
    cd ${config.ARCHIVE_PATH} && tar -czf ~/backups/${backupName} . || true;
  `;

  execSync(`ssh ${remoteUser} "${backupCommands}"`, { stdio: 'pipe' });
  execSync(`ssh ${remoteUser} "${backupCommands}"`, { stdio: "pipe" });

  const backupDir = path.join(config.SOURCE_DIR, 'backup');
  const backupDir = path.join(config.SOURCE_DIR, "backup");
  if (!fs.existsSync(backupDir)) {
    fs.mkdirSync(backupDir, { recursive: true });
  }

  execSync(`scp ${remoteUser}:~/backups/${backupName} "${backupDir}/"`, { stdio: 'pipe' });
  execSync(`scp ${remoteUser}:~/backups/${backupName} "${backupDir}/"`, {
    stdio: "pipe",
  });
  return backupName;
}