UNPKG

webpack-deploy-server

Version:

webpack打包后资源同步至ftp服务器,可用于vue,uni-app等项目。

88 lines (81 loc) 3.03 kB
const scp2 = require('scp2') const path = require('path') const ssh2 = require("ssh2").Client; const ssh2Conn = new ssh2(); const fs = require('fs'); const { connectConfig, verifyConfig, executeOrder } = require('./lib') class Upload { constructor(config = {}) { this.config = config this.connectInfo = { host: config['host'], username: config['username'], password: config['password'], path: config['from'], port: config['port'] || 22, privateKey: config['privateKey'] || 'password' } } apply(compiler) { try { compiler.hooks.afterEmit.tapPromise('webpackDeplyServer', async (compilation) => { // 校验参数 if (!verifyConfig(this.config)) { return; } // 创建ssh2连接所需的配置 const connect = connectConfig(this.config); ssh2Conn.on('ready', () => { // 前置命令执行 const prefixCmd = this.config.prefixCmd || ""; // 执行命令 executeOrder(ssh2Conn, prefixCmd, () => { if (prefixCmd) { console.log(`[webpack-deploy-server] 前置命令执行完成`); } if (fs.existsSync(this.config.from)) { this.uploadFiles(); } else { console.log(`[webpack-deploy-server] 不存在上传文件`); this.finish(); } }) }).on('error', (error) => { console.log(`[webpack-deploy-server] 服务器连接失败:`); console.log(error) }).connect(connect); }) } catch (error) { throw error; } } finish() { // 后置命令 const suffixCmd = this.config.suffixCmd || ""; executeOrder(ssh2Conn, suffixCmd, () => { if (suffixCmd) { console.log(`[webpack-deploy-server] 后置命令执行完成`); } ssh2Conn.end(); }); } uploadFiles() { console.log(`[webpack-deploy-server] 文件正在上传中,请稍后...`); const connect = connectConfig(this.connectInfo); scp2.scp( this.config.from, { ...connect, path: this.config.to }, (err) => { if (err) { console.log(`[webpack-deploy-server] 文件上传失败`); } else { console.log(`[webpack-deploy-server] 文件上传成功`); this.finish(); } } ) } } module.exports = Upload