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

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

相关推荐
linux_cfan1 小时前
17 · 引擎适配器全景:HLS/DASH/Vimeo/Mux/Cast
前端·javascript·音视频
计算机魔术师2 小时前
烧掉2780亿美元还不够?OpenAI的资本豪赌让人头皮发麻
前端
IT_陈寒2 小时前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
卡布鲁3 小时前
React useMemo 与 useCallback 完全指南:原理、场景与避坑
前端·react.js
碳基修炼3 小时前
排查记:本地 devServer 是 http,浏览器却把 302 重定向升级成了 https
前端·http
步行cgn3 小时前
Spring p 命名空间注入详解
java·前端·spring
何何____3 小时前
Vue 生命周期详解
前端·javascript
江米小枣tonylua3 小时前
TypeScript 全面の拥抱 !Prisma 8 + Electron 升级实战
前端
闪耀之光M784 小时前
npm命令解析
前端
咸鱼老弟4 小时前
AI Agent 的"自主性悖论"——为什么给它的自由度越大,越要配一套更硬的护栏
前端·人工智能