Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
denniswlin committed May 12, 2016
1 parent 4d75952 commit 5b2b09d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 40 deletions.
110 changes: 70 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,83 @@
/**
* fis-deploy-scp
*/
var _ = fis.util;
/*
* author: wanglin576
* date: 2016-05-10
* desc: 将输出打包成zip包,并scp到远程机
* from https://github.com/webplus/fis-deploy-scp
*/
var fs = require('fs');
var archive = require('archiver')('zip');
var cwd = process.cwd();
var path = require("path");
var fs = require("fs");
var child_process = require("child_process");

module.exports = function(dest, file, content, settings, callback) {
options = settings
if (!options.to) {
function normalizePath(to, root){
if (!to){
to = '/';
}else if(to[0] === '.'){
to = fis.util(cwd + '/' + to);
} else if(/^output\b/.test(to)){
to = fis.util(root + '/' + to);
}else {
to = fis.util(to);
}
return to;
}

function upload (conf) {
if (!conf.to) {
throw new Error('options.to is required!');
} else if (!options.source) {
} else if (!conf.file) {
throw new Error('options.source is required!');
}
var zipfile = conf.file.split('/').pop();
var scp_cmd = "scp " + conf.file + " " + conf.server + ":" + conf.to;
var unzip_cmd = "ssh " + conf.server + " \"cd " + conf.to + ";unzip -o " + zipfile + ";rm -rf "+ zipfile +";exit;\"";

var to = options.to;
var source = options.source;
var server = options.server;
var cmd = 'cd ' + path.join(process.cwd(), source) + ';zip -r dist.zip ./*';
var scp_cmd = "scp " + path.join(source, "./dist.zip") + " " + server + ":" + to + " ";
var unzip_cmd = "ssh " + server + " \"cd " + to + ";unzip -o dist.zip;rm -rf dist.zip;exit;\"";

child_process.exec(cmd, function(e, p) {
if (e) {
console.log(e);
child_process.exec(scp_cmd, function(e1, p1) {
if (e1) {
console.log(e1);
} else {
console.log("Local packing complete.")
console.log(scp_cmd + "\n");
console.log("Upload complete.")
}
child_process.exec(scp_cmd, function(e1, p1) {
if (e1) {
console.log(e1);
child_process.exec(unzip_cmd, function(e2, p2) {
if (e2) {
console.log(e2);
} else {
console.log(scp_cmd + "\n");
console.log("Upload complete.")
console.log("Remote deployment complete.")
}
child_process.exec(unzip_cmd, function(e2, p2) {
if (e2) {
console.log(e2);
} else {
console.log("Remote deployment complete.")
}
fs.unlink(path.join(source, "./dist.zip"), function(e3) {
if (e3) {
console.log(e3);
} else {
console.log("Local cleanup completed.")
}
})
})
})
})
};
}

module.exports = function(files, settings, callback) {
var conf = settings, targetPath, output;
if (!conf.file){
fis.log.error('[fis-deploy-scp] need specify the zip file path with option [file]')
}
targetPath = normalizePath(conf.file, fis.project.getProjectPath());
if (!fis.util.exists(targetPath)) {
fis.util.mkdir(fis.util.pathinfo(targetPath).dirname);
}
output = fs.createWriteStream(targetPath);
archive.pipe(output);
files.forEach(function(fileInfo){
var file = fileInfo.file;
if(!file.release){
fis.log.error('unable to get release path of file['
+ file.realpath
+ ']: Maybe this file is neither in current project or releasable');
}
var name = (('/') + fileInfo.dest.release).replace(/^\/*/g, '');
if (!/map\.json/.test(name)) {
archive.append(fileInfo.content, {name: name});
}
});
output.on('close', function(){
fis.log.debug('[fis-deploy-scp] zip end');
upload(conf)
callback && callback();
});
archive.finalize();
}
module.exports.fullpack = true;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"archiver": "0.14.3"
},
"repository": {
"type": "git",
"url": "git+https://github.com/webplus/fis-deploy-scp.git"
Expand Down

0 comments on commit 5b2b09d

Please sign in to comment.