前端一键部署网站至服务器FTP

首先在你的项目里需要安装依赖:

复制代码
npm install ftp -D

然后在项目根目录添加脚本:

javascript 复制代码
import FTP from 'ftp';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const client = new FTP();

const config = {
    host: '你服务器的公网IP',
    port: 21,
    user: '服务器那里配置的ftp账号',
    password: '服务器那里配置的ftp密码'
};

function uploadDir(client, localDir, remoteDir) {
    return new Promise((resolve, reject) => {
        fs.readdir(localDir, { withFileTypes: true }, (err, files) => {
            if (err) return reject(err);

            let pending = files.length;
            if (pending === 0) return resolve();

            files.forEach((file) => {
                const localPath = path.join(localDir, file.name);
                const remotePath = path.posix.join(remoteDir, file.name);

                if (file.isDirectory()) {
                    client.mkdir(remotePath, true, (err) => {
                        if (err && err.code !== 550) {
                            console.log(`创建目录失败: ${remotePath}`, err.message);
                        }
                        uploadDir(client, localPath, remotePath)
                            .then(() => {
                                if (--pending === 0) resolve();
                            })
                            .catch(reject);
                    });
                } else {
                    console.log(`上传中: ${file.name}`);
                    client.put(localPath, remotePath, (err) => {
                        if (err) {
                            console.log(`❌ 上传失败: ${file.name}`, err.message);
                        } else {
                            console.log(`✓ ${file.name}`);
                        }
                        if (--pending === 0) resolve();
                    });
                }
            });
        });
    });
}

client.on('ready', () => {
    console.log('✅ 连接成功!');

    const localRoot = path.join(__dirname, 'dist');
    const remoteRoot = 'ftp文件夹下的目录名称,如果没有直接填/';

    client.mkdir(remoteRoot, true, (err) => {
        if (err && err.code !== 550) {
            console.log('创建远程目录失败:', err.message);
        }

        uploadDir(client, localRoot, remoteRoot)
            .then(() => {
                console.log('✅ 上传成功!');
                client.end();
            })
            .catch((err) => {
                console.log('❌ 上传失败:', err);
                client.end();
            });
    });
});

client.on('error', (err) => {
    console.log('❌ FTP 错误:', err);
});

client.connect(config);

然后在 package.json 中添加脚本

javascript 复制代码
"scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "deploy": "vite build && node ftp-deploy.js" //添加这行
},

然后每次发布运行这个脚本即可

相关推荐
jzlhll12315 分钟前
kotlin Flow first() last()总结
开发语言·前端·kotlin
用头发抵命37 分钟前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌1 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛1 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
柳杉1 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化
Greg_Zhong2 小时前
前端基础知识实践总结,每日更新一点...
前端·前端基础·每日学习归类
We་ct2 小时前
LeetCode 148. 排序链表:归并排序详解
前端·数据结构·算法·leetcode·链表·typescript·排序算法
TON_G-T2 小时前
day.js和 Moment.js
开发语言·javascript·ecmascript
IT_陈寒3 小时前
JavaScript开发者必看:5个让你的代码性能翻倍的隐藏技巧
前端·人工智能·后端