使用ftp-deploy实现项目打包完成后自动上传到服务器

npm install ftp-deploy --save-dev

在./build下新建ftp.js文件

const FtpDeploy = require('ftp-deploy');
const ftpServ = new FtpDeploy();
let config = {
    user: "username",                   // 用户名
    password: "password",           // 密码
    host: "host", // ftp 主机地址
    port: 21, // 端口
    localRoot: './../dist', // 本地资源路径
    remoteRoot: '/htdocs/phpcrm/dist/', // 远程资源路径
    include: ['*', '**/*'], // 包含文件
    exclude: ['dist/**/*.map'],     // 排除文件
    deleteRemote: true,              // 上传前是否删除
    forcePasv: true                 // 主动模式/被动模式
};
// 上传完成后回调
ftpServ.deploy(config)
    .then(res => { // 上传成功
        console.log('finished:', res);
        return false;
    })
    .catch(err => { // 上传失败
        console.log(err);
        return false;
    });

编辑package.json文件

"scripts": {
    "upload": "node build/ftp.js",
    ...
},

部署:

npm run build

npm run upload

也可以写成这样,在打包完成后自动上传

"scripts": {
    "build": "node build/build.js && node build/ftp.js",
    ...
},

如果嫌英文文档麻烦可以试试我这款,基于promise-ftp开发,使用简单。

https://github.com/acccccccb/ftp-auto-deploy

https://gitee.com/GLUESTICK/ftp-auto-deploy