ftp包中未找到直接上傳文件夾的方法,basic-ftp包可以直接上傳文件夾
basic-ftp文檔:basic-ftp - npm (npmjs.com)
1誉尖、安裝basic-ftp
npm install basic-ftp -D
2、項(xiàng)目根目錄創(chuàng)建up.js文件
3辨宠、示例代碼
// 將打包后dist上傳ftp
const ftp = require('basic-ftp')
const path = require('path')
// 上傳
up()
async function up() {
const client = new ftp.Client()
try {
await client.access({
host: '***.***.***.***',
user: '用戶名',
password: '密碼'
})
// 上傳進(jìn)度
client.trackProgress(info => {
if (info.bytes) {
console.log(
`上傳:${info.name}仓坞;大懈Ю:${
info.bytes > 1024
? info.bytes > 1024 * 1024
? (info.bytes / 1024 / 1024).toFixed(2) + 'M'
: (info.bytes / 1024).toFixed(2) + 'K'
: info.bytes + 'B'
}`
)
console.log('========================================')
}
})
// 本地dist打包路徑
const dist_path = path.resolve(process.cwd(), './dist')
// 遠(yuǎn)端ftp路徑
const ftp_path = '/www/xxxx'
// 進(jìn)入遠(yuǎn)端目錄春哨,沒(méi)有則創(chuàng)建荆隘,設(shè)當(dāng)前目錄為工作區(qū)
await client.ensureDir(ftp_path)
// 清空工作區(qū)文件
await client.clearWorkingDir()
// 上傳
await client.uploadFromDir(dist_path, ftp_path)
} catch (err) {
console.log('上傳失敗:', err)
}
// 關(guān)閉
client.close()
}
4赴背、執(zhí)行命令
node up