-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (79 loc) · 2.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* 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 child_process = require("child_process");
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 (!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;\"";
child_process.exec(scp_cmd, function(e1, p1) {
if (e1) {
console.log(e1);
} else {
console.log(scp_cmd + "\n");
console.log("Upload complete.")
}
child_process.exec(unzip_cmd, function(e2, p2) {
if (e2) {
console.log(e2);
} else {
console.log("Remote deployment complete.")
}
})
})
}
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;