simple-sftp-deploy
Advanced tools
Comparing version 0.0.2 to 0.1.2
{ | ||
"name": "simple-sftp-deploy", | ||
"version": "0.0.2", | ||
"version": "0.1.2", | ||
"description": "deploy project with ssh2-sftp-client", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"ssh2-sftp-client": "^2.4.0" | ||
"ssh2-sftp-client": "^4.2.3" | ||
}, | ||
@@ -19,0 +19,0 @@ "homepage": "https://github.com/unclemeric/simple-sftp-deploy#readme", |
@@ -6,2 +6,3 @@ # simple-sftp-deploy | ||
- 引入 | ||
``` | ||
@@ -12,3 +13,5 @@ npm install simple-sftp-deploy -S | ||
``` | ||
- 使用 | ||
``` | ||
@@ -18,2 +21,3 @@ const ssd = require('simple-sftp-deploy') | ||
ssd.start({ | ||
is_bak: true, // 是否备份 | ||
remote_path: '/data/webapps/xxx',//服务器项目根路径 | ||
@@ -20,0 +24,0 @@ assets_path: path.resolve(__dirname,'./dist'),//编译后资源文件夹名称(需上传的文件夹) |
@@ -1,5 +0,5 @@ | ||
const registConf = function({remote_path, assets_path, host, port='22', user, password}) { | ||
const registConf = function({ remote_path, assets_path, is_bak = false, host, port = '22', user, password }) { | ||
return { | ||
remote_path,//服务器项目根路径 | ||
assets_path,//存放编译之后资源文件夹 此项目需同_config.yml的public_dir值一样 Vue项目为./dist | ||
remote_path, //服务器项目根路径 | ||
assets_path, //存放编译之后资源文件夹 此项目需同_config.yml的public_dir值一样 Vue项目为./dist | ||
options: { | ||
@@ -13,2 +13,3 @@ host, //ftp服务器ip | ||
}, | ||
is_bak, // 是否备份, 默认不备份 | ||
project_remote_path: remote_path, //项目服务器路径 | ||
@@ -15,0 +16,0 @@ public_asset_path: assets_path //指定需要上传的文件夹目录 |
163
src/start.js
@@ -7,6 +7,6 @@ const Client = require('ssh2-sftp-client') | ||
const staticFilesPath = { | ||
folder: { | ||
local: '', | ||
remote: '' | ||
} | ||
folder: { | ||
local: '', | ||
remote: '' | ||
} | ||
} | ||
@@ -16,24 +16,27 @@ /** | ||
**/ | ||
let localFileJson = []; | ||
let localFileJson = [] | ||
function handleFilePath(obj, type) { | ||
const { local, remote } = obj; | ||
const files = fs.readdirSync(local); | ||
const { local, remote } = obj | ||
const files = fs.readdirSync(local) | ||
files.forEach(file => { | ||
const _lp = `${local}/${file}`; | ||
type = fs.statSync(_lp).isFile() ? 'file' : fs.statSync(_lp).isDirectory() ? 'folder' : ''; | ||
const _lp = `${local}/${file}` | ||
type = fs.statSync(_lp).isFile() ? 'file' : fs.statSync(_lp).isDirectory() ? 'folder' : '' | ||
let item = { | ||
type: type, | ||
file: file, | ||
// localPath: type !== "img" ? _lp : fs.readFileSync(_lp), | ||
localPath: _lp, | ||
remotePath: `${remote}/${file}` | ||
type: type, | ||
file: file, | ||
// localPath: type !== "img" ? _lp : fs.readFileSync(_lp), | ||
localPath: _lp, | ||
remotePath: `${remote}/${file}` | ||
} | ||
localFileJson.push(item); | ||
if(type=='folder'){ | ||
handleFilePath({ | ||
local: item.localPath, | ||
remote: `${remote}/${file}` | ||
},'folder'); | ||
localFileJson.push(item) | ||
if (type == 'folder') { | ||
handleFilePath( | ||
{ | ||
local: item.localPath, | ||
remote: `${remote}/${file}` | ||
}, | ||
'folder' | ||
) | ||
} | ||
}); | ||
}) | ||
} | ||
@@ -45,61 +48,81 @@ /** | ||
Object.keys(staticFilesPath).forEach(key => { | ||
handleFilePath(staticFilesPath[key], key) | ||
handleFilePath(staticFilesPath[key], key) | ||
}) | ||
const tasks = localFileJson.map(item => { | ||
return new Promise((resolve, reject) => { | ||
if(item.type == 'folder'){ | ||
sftp.mkdir(item.remotePath,false);//false:不设置递归创建文件夹 | ||
resolve(); | ||
}else{ | ||
if (item.type == 'folder') { | ||
sftp.mkdir(item.remotePath, false) //false:不设置递归创建文件夹 | ||
resolve() | ||
} else { | ||
sftp | ||
.put(item.localPath, item.remotePath) | ||
.then(() => { | ||
console.log(`${item.localPath}上传完成`); | ||
resolve(); | ||
}) | ||
.catch(err => { | ||
console.log(`${item.localPath}上传失败`); | ||
reject(); | ||
}); | ||
.put(item.localPath, item.remotePath) | ||
.then(() => { | ||
console.log(`${item.localPath}上传完成`) | ||
resolve() | ||
}) | ||
.catch(err => { | ||
console.log(`${item.localPath}上传失败`) | ||
reject() | ||
}) | ||
} | ||
}); | ||
}); | ||
}) | ||
}) | ||
console.log('开始上传...') | ||
return Promise.all(tasks); | ||
return Promise.all(tasks) | ||
} | ||
const start = function(options){ | ||
const config = registConf(options); | ||
const start = function(options) { | ||
const config = registConf(options) | ||
staticFilesPath.folder = { | ||
local: config.assets_path, | ||
remote: config.remote_path | ||
local: config.assets_path, | ||
remote: config.remote_path | ||
} | ||
sftp.connect(config.options) | ||
.then((data) => { | ||
console.log('ftp文件服务器连接成功'); | ||
try { | ||
const bak_name = `${config.project_remote_path}-${new Date().getTime()}` | ||
console.log(`目录${config.project_remote_path}存在,准备备份为${bak_name}`); | ||
sftp.rename(config.project_remote_path, bak_name); | ||
} catch (e) { | ||
console.log(`目录${config.project_remote_path}不存在`); | ||
} | ||
console.log(`准备创建项目根路目录${config.project_remote_path}`); | ||
sftp.mkdir(config.project_remote_path, false);//false:不设置递归创建文件夹 | ||
console.log('正在上传...'); | ||
uploadFile() | ||
.then(() => { | ||
console.log('------所有文件上传完成!-------\n'); | ||
sftp.end(); | ||
}) | ||
.catch(() => { | ||
console.error('------上传失败,请检查!-------\n'); | ||
sftp.end(); | ||
}); | ||
}) | ||
.catch(err => { | ||
console.error(err, 'catch error'); | ||
sftp.end(); | ||
}); | ||
sftp | ||
.connect(config.options) | ||
.then(data => { | ||
console.log('ftp文件服务器连接成功') | ||
const deployFiles = function() { | ||
console.log(`准备创建项目根路目录${config.project_remote_path}`) | ||
sftp.mkdir(config.project_remote_path, false) //false:不设置递归创建文件夹 | ||
console.log('正在上传...') | ||
uploadFile() | ||
.then(() => { | ||
console.log('------所有文件上传完成!-------\n') | ||
sftp.end() | ||
}) | ||
.catch(() => { | ||
console.error('------上传失败,请检查!-------\n') | ||
sftp.end() | ||
}) | ||
} | ||
if (config.is_bak) { | ||
const bak_name = `${config.project_remote_path}-${new Date().getTime()}` | ||
console.log(`目录${config.project_remote_path}存在,准备备份为${bak_name}`) | ||
sftp | ||
.rename(config.project_remote_path, bak_name) | ||
.then(res => { | ||
deployFiles() | ||
}) | ||
.catch(e => { | ||
console.log(`目录${config.project_remote_path}不存在`) | ||
deployFiles() | ||
}) | ||
} else { | ||
// 删除旧文件 | ||
sftp | ||
.rmdir(config.project_remote_path, true) | ||
.then(res => { | ||
deployFiles() | ||
}) | ||
.catch(e => { | ||
console.log(`删除目录${config.project_remote_path}失败`) | ||
deployFiles() | ||
}) | ||
} | ||
}) | ||
.catch(err => { | ||
console.error(err, 'catch error') | ||
sftp.end() | ||
}) | ||
} | ||
module.exports = start; | ||
module.exports = start |
6627
145
28
+ Addedretry@0.12.0(transitive)
+ Addedssh2-sftp-client@4.3.1(transitive)
- Removedretry@0.10.1(transitive)
- Removedssh2-sftp-client@2.5.2(transitive)
Updatedssh2-sftp-client@^4.2.3