node.js自動上傳ftp的腳本分享
起因
剛加入一個小組的項(xiàng)目開發(fā),開發(fā)環(huán)境是基于node環(huán)境,通過webpack打包構(gòu)建代碼,然后上傳sftp,在瀏覽器測試。這種開發(fā)模式無可厚非,但是每次修改源代碼,然后build,然后upload,不勝其煩。之前項(xiàng)目中有過 gulp-sftp任務(wù)腳本,然而并不是生效。于是自力更生,另謀他法,搞一個自動上傳sftp的服務(wù)腳本。
設(shè)想
因?yàn)榛趙ebpack,所以直接啟動webpack編譯的watch監(jiān)聽即可,在watch回調(diào)里執(zhí)行stfp的上傳,上傳去npm社區(qū)找一個sftp的客戶端插件
實(shí)現(xiàn)
使用了插件ssh2-sftp-client,文檔有使用說明和api
寫書寫了一個 sftp 模塊,連接完,直接導(dǎo)出
const Client = require('ssh2-sftp-client');
const fs = require('fs');
const sftp = new Client();
sftp
.connect({
host: '0.0.0.0', // ftp服務(wù)器ip地址
port: '22', // ftp服務(wù)器port
username: 'yourname', // 你的登錄用戶名
password: 'yourpass', // 你的密碼
privateKey: fs.readFileSync('/Users/yourname/.ssh/id_rsa'), // 私鑰
passphrase: 'yourpass', // 私鑰密碼
})
.then(() => {
console.log('ftp文件服務(wù)器連接成功');
})
.catch(err => {
console.log(err, 'catch error');
});
module.exports = sftp;
然后在webpack的watch里進(jìn)行 上傳文件即可,關(guān)于上傳文件,圖片的等類型需要使用Buffer類型上傳,做一個特殊處理
const path = require('path');
const fs = require('fs');
const yargs = require('yargs');
const webpack = require('webpack');
const webpackConfig = require('./webpack.prod.config');
const sftp = require('./sftp');
const user = yargs.argv.user || '';
console.log(user);
const staticFilesPath = {
js: {
local: path.resolve(__dirname, '../dist/js'),
remote: `/upload_code/${user}/static/mobile/js/dist`,
},
css: {
local: path.resolve(__dirname, '../dist/css'),
remote: `/upload_code/${user}/static/mobile/css/`,
},
img: {
local: path.resolve(__dirname, '../dist/images'),
remote: `/upload_code/${user}/static/mobile/images/`,
},
};
let isFirstBuild = true;
const compiler = webpack(webpackConfig);
const watching = compiler.watch(
{
ignored: /node_modules/,
aggregateTimeout: 100,
poll: 1000,
},
(err, stats) => {
if (err || stats.hasErrors()) {
console.log(err);
}
console.log('編譯成功!');
if (isFirstBuild) {
isFirstBuild = false;
return;
}
console.log('正在上傳...');
uploadFile()
.then(() => {
console.log('------所有文件上傳完成!-------\n');
})
.catch(() => {
console.log('------上傳失敗,請檢查!-------\n');
});
}
);
/**
* 處理文件路徑,循環(huán)所有文件,如果是圖片需要讀取成Buffer類型
**/
function handleFilePath(obj, type) {
const { local, remote } = obj;
const files = fs.readdirSync(local);
return files.map(file => {
const _lp = `${local}/${file}`;
return {
type: type,
file: file,
localPath: type !== 'img' ? _lp : fs.readFileSync(_lp),
remotePath: `${remote}/${file}`,
};
});
}
/**
* 上傳文件
**/
function uploadFile() {
let files = [];
Object.keys(staticFilesPath).forEach(key => {
files = files.concat(handleFilePath(staticFilesPath[key], key));
});
const tasks = files.map(item => {
return new Promise((resolve, reject) => {
sftp
.put(item.localPath, item.remotePath)
.then(() => {
console.log(`${item.file}上傳完成`);
resolve();
})
.catch(err => {
console.log(`${item.file}上傳失敗`);
reject();
});
});
});
return Promise.all(tasks);
}
注意點(diǎn):
- 連接sftp服務(wù)器,推薦使用 私鑰文件連接,使用password出錯可能性比較大
- 上傳文件部分,目前不支持上傳一個目錄,所以需要循環(huán)處理文件
- 上傳文件部分,容易出錯,一定要保證遠(yuǎn)端服務(wù)器存在對應(yīng)目錄,目前插件沒有自動創(chuàng)建目錄的機(jī)制
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
node+experss實(shí)現(xiàn)爬取電影天堂爬蟲
本文給大家分享的是node+experss制作爬蟲的第二篇,我們來爬取電影天堂最新更新的電影迅雷下載鏈接,有需要的小伙伴可以參考下2016-11-11
nodejs做個爬蟲爬取騰訊動漫內(nèi)容簡單實(shí)現(xiàn)
這篇文章主要為大家介紹了nodejs做個爬蟲爬取騰訊動漫內(nèi)容簡單實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
express框架中使用jwt實(shí)現(xiàn)驗(yàn)證的方法
這篇文章主要給大家介紹了關(guān)于express框架中使用jwt實(shí)現(xiàn)驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用express具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
node.js 核心http模塊,起一個服務(wù)器,返回一個頁面的實(shí)例
下面小編就為大家?guī)硪黄猲ode.js 核心http模塊,起一個服務(wù)器,返回一個頁面的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
express結(jié)合nodejs開啟服務(wù)示例模版
這篇文章主要為大家展現(xiàn)了express結(jié)合nodejs開啟服務(wù)的代碼示例模版,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Node.js+Express+Vue+MySQL+axios的項(xiàng)目搭建全過程
這篇文章主要介紹了Node.js+Express+Vue+MySQL+axios的項(xiàng)目搭建全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

