deploy.js

This commit is contained in:
Никита Круглицкий
2025-08-15 20:02:32 +06:00
parent 2bee1f5d79
commit ff1340119a
3 changed files with 49 additions and 38 deletions

View File

@@ -1,7 +1,6 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import readline from 'node:readline'
import * as clack from '@clack/prompts'
import SftpClient from 'ssh2-sftp-client'
const HOST = '92.53.106.114'
@@ -12,73 +11,62 @@ const LOCAL_SRC_ARCHIVE = path.resolve('./src.zip')
const DATE = new Date().toISOString().replace(/[-:]/g, '').replace(/\..+/, '');
(async () => {
const spinner = clack.spinner()
try {
const password = await ask(`Введите пароль для ${USERNAME}@${HOST}: `)
clack.intro(`Деплой на ${HOST}`)
const password = await clack.text({ message: `Введите пароль для пользователя ${USERNAME}` })
// 1) yarn build
execSync('yarn build', { stdio: 'inherit' })
spinner.start('Собираем проект')
execSync('yarn build --logLevel=silent', { stdio: 'inherit' })
spinner.stop('Сборка выполнена')
// 2) git archive
spinner.start('Делаем архив с исходным кодом')
execSync(`git archive --format=zip --output=${LOCAL_SRC_ARCHIVE} main`, { stdio: 'inherit' })
spinner.stop('Архив с исходным кодом сохранен')
const sftp = new SftpClient()
spinner.start('Подключаемся к серверу')
await sftp.connect({
host: HOST,
port: 22,
username: USERNAME,
password,
})
spinner.stop('Подключились к серверу')
// 3) Переименование public_html
try {
spinner.start('Делаем бэкап public_html')
await sftp.rename(`${REMOTE_BASE}/public_html`, `${REMOTE_BASE}/public_html_${DATE}`)
console.log('Папка public_html переименована')
spinner.stop('Папка public_html переименована')
}
catch {
console.log('⚠️ public_html не найдена, пропускаю переименование')
spinner.stop('public_html не найдена, пропускаю переименование')
}
// 4) Загрузка dist
spinner.start('Загружаем dist в public_html')
await sftp.mkdir(`${REMOTE_BASE}/public_html`, true)
await uploadDir(sftp, LOCAL_DIST, `${REMOTE_BASE}/public_html`)
await sftp.uploadDir(LOCAL_DIST, `${REMOTE_BASE}/public_html`, undefined)
spinner.stop('Статические файлы загружены')
// 5) Загрузка src.zip
spinner.start('Загружаем архива с исходным кодом')
await sftp.mkdir(`${REMOTE_BASE}/src`, true)
await sftp.put(LOCAL_SRC_ARCHIVE, `${REMOTE_BASE}/src.zip`)
spinner.stop('Архив с исходным кодом загружен')
await sftp.end()
console.log('🎉 Деплой завершён успешно!')
clack.outro('Деплой завершён успешно!')
}
catch (err) {
console.error('❌ Ошибка деплоя:', err.message)
process.exit(1)
spinner.stop()
clack.cancel(`Ошибка: ${err.message}`)
process.exit(0)
}
})()
function ask(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
return new Promise((resolve) => {
rl.question(query, (value) => {
rl.close()
resolve(value)
})
})
}
async function uploadDir(sftp, localDir, remoteDir) {
const items = fs.readdirSync(localDir)
for (const item of items) {
const localPath = path.join(localDir, item)
const remotePath = `${remoteDir}/${item}`
if (fs.lstatSync(localPath).isDirectory()) {
await sftp.mkdir(remotePath, true)
await uploadDir(sftp, localPath, remotePath)
}
else {
await sftp.put(localPath, remotePath)
}
}
}