前端node.js 自动打包上传到sftp 并通知钉钉机器人

sftp上传

const path = require('path')
const axios = require('axios');
const package=require('./package.json')
const shell = require("shelljs");
// 复制文件
shell.cp(
    path.resolve(__dirname, `./index.html`),
    path.resolve(__dirname, `../../public/index.html`),
)



if (shell.exec("yarn build").code !== 0) {
    // 执行npm run build 命令
    shell.echo("Error: get coding failed");
    shell.exit(1);
}


// 钉钉机器人请求地址
const apiUrl='https://oapi.dingtalk.com/robot/send?access_token=***'
const param={
    "msgtype": "link",
    "link": {
        "title": "",
        "text": ``,
        "messageUrl": ""
    }
}
const data = JSON.stringify(param)

const webHookConfig = {
    method: 'post',
    url: apiUrl,
    headers: {
        'Content-Type': 'application/json'
    },
    data : data
};




const Client = require('ssh2-sftp-client');
const sftp = new Client();

const config = {
    host: '',
    port: '22',
    username: '',
    password: ''
}
/**
 * 上传文件到sftp
 * @param { Object } config    sftp 链接配置参数
 * @param { String } config.host sftp 主机地址
 * @param { String } config.port sftp 端口号
 * @param { String } config.username sftp 用户名
 * @param { String } config.password sftp 密码
 *
 * @param { Object } options 配置参数
 * @param { String } localStatic // 本地静态资源文件夹路径
 * @param { String } remoteStatic // 服务器静态资源文件夹路径
 * @param { String } localFile // 本地html页面
 * @param { String } remoteFile // 服务器html页面
 */
function upload(config, options) {
    sftp.connect(config)
        .then(() => {
            console.log('sftp链接成功')
            console.log('文件上传中');
            return sftp.uploadDir(options.localStatic, options.remoteStatic);
        }).then((data) => {
            console.log('文件上传成功');
            axios(webHookConfig)
                .then(function (response) {
                    console.log(JSON.stringify(response.data));
                })
                .catch(function (error) {
                    console.log(error);
                });
            sftp.end();
    }).catch((err) => {
        console.log('上传失败', err);
        sftp.end();
    })
}

// 上传文件
upload(config, {
    localStatic: path.resolve(__dirname, './.vitepress/dist'), // 本地文件夹路径
    remoteStatic: '/', // 服务器文件夹路径器
})

ftp上传

const options = {
    host: "",
    port: "21",
    user: "",
    password: ""
    // proxy:{
    //   host: '',
    //   port: '',
    // }
};

const FtpDeploy = require("ftp-deploy");


const ftpDeploy = new FtpDeploy();

const config = {
    ...options,
    // localRoot: path.resolve(__dirname,'../zip'),
    // include: ["dist.zip"], // this would upload everything except dot files
    localRoot: path.resolve(__dirname,'./.vitepress/dist'),
    include: ["*", "**/*"], // this would upload everything except dot files
    remoteRoot: "/",
    // include: ["*.php", "dist/*", ".*"],
    // e.g. exclude sourcemaps, and ALL files in node_modules (including dot files)
    // exclude: ["dist/**/*.map", "node_modules/**", "node_modules/**/.*", ".git/**"],
    exclude: [],
    // delete ALL existing files at destination before uploading, if true
    deleteRemote: false,
    // Passive mode is forced (EPSV command is not sent)
    forcePasv: false
};


// use with callback
ftpDeploy.deploy(config, (err, res) => {
    if (err) {
        console.log(err);
    } else {
        console.log("finished:", res);
        axios(webHookConfig)
          .then(function (response) {
              console.log(JSON.stringify(response.data));
          })
          .catch(function (error) {
              console.log(error);
          });
    }
});

ftpDeploy.on("uploading", data => {
    console.log(`start up ${data.filename} ,fileSize ${data.totalFilesCount}`); // partial path with filename being uploaded
});
ftpDeploy.on("uploaded", data => {
    console.log("\033[33m " + data.filename + " upload done \033[39m"); // same data as uploading event
});
ftpDeploy.on("log", data => {
    console.log(data); // same data as uploading event
});
ftpDeploy.on("upload-error", data => {
    console.log(data.err); // data will also include filename, relativePath, and other goodies
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木贝西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值