前端一键部署网站至服务器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" //添加这行
},

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

相关推荐
GuWenyue43 分钟前
放弃云端API!一套React+WebGPU本地LLM方案,零数据上传、离线可用
前端·人工智能·前端框架
原则猫2 小时前
函数/变量提升
前端
码兄科技3 小时前
实战:基于Spring Boot + UniApp的地理信息小程序开发
spring boot·后端·uni-app
独泪了无痕3 小时前
Vue3 Hooks使用实战解析
前端·vue.js
shawxlee4 小时前
vue3在public下封装config.js自定义配置动态数据,可在打包后直接修改,方便后端部署及后续维护
前端·javascript·经验分享·vue·团队开发·js·项目优化
用户059540174465 小时前
把记忆存储的回归测试从手工换成 Playwright + GitHub Actions,线上缺陷降低 80%
前端·css
触底反弹5 小时前
🚀 从 DOM0 级到 React 合成事件:前端事件监听的 20 年演进史
前端·react.js·面试
kyriewen5 小时前
我给前端项目的接口请求套了6层保护——才发现以前一直在裸奔
前端·javascript·面试
颜酱5 小时前
04 | 召回前置准备:搭好召回所需的四个数据库
前端·人工智能·后端
郝亚军6 小时前
如何安装webstorm、Node.js和vue CLI
前端·javascript·vue.js