forked from swznd/sftp-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (145 loc) · 5.22 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const sftpClient = require('ssh2-sftp-client');
const { Readable, Transform } = require('stream');
const path = require('path');
const micromatch = require('micromatch');
(async () => {
let client = new sftpClient;
let connected = false;
try {
const host = core.getInput('host');
const port = core.getInput('port');
const user = core.getInput('user');
const password = core.getInput('password');
const privateKey = core.getInput('private_key');
const localPath = trimChar((core.getInput('local_path') || ''), '/').trim();
const remotePath = trimChar((core.getInput('remote_path') || ''), '/').trim();
const ignore = (core.getInput('ignore') || '').split(',').filter(Boolean);
const remoteRev = core.getInput('remote_revision');
const payload = github.context.payload;
const config = {
host: host,
username: user,
password: password,
port: port || 22,
privateKey: privateKey
};
await client.connect(config);
connected = true;
console.log('Connected. Current Working Directory:', await client.cwd());
let start = '';
if (remoteRev != '') {
start = remoteRev;
}
else if (await client.exists(remotePath + '/.revision')) {
const st = new Transform();
st._transform = function (chunk,encoding,done) {
this.push(chunk)
done();
};
await client.get(remotePath + '/.revision', st);
const remoteHash = new Promise((resolve, reject) => {
st.on('end', resolve(st.read()));
st.on('error', reject)
});
start = await remoteHash;
// try { start = (await git('rev-parse', '--verify', `${await remoteHash}^{commit}`)).trim(); } catch(e) {};
}
console.log('Remote Revision:', start.toString());
const end = payload.after;
if (start == '') {
console.log('Remote revision empty, get from initial commit');
start = await git('hash-object', '-t', 'tree', '/dev/null');
}
start = start.toString().trim();
console.log('Comparing', `${start}..${end}`);
const modified = await git('diff', '--name-only', '--diff-filter=AMR', '-M100%', start, end);
const deleted = await git('diff-tree', '--name-only', '--diff-filter=D', '-t', start, end);
const filterFile = file => {
if (file === '') return false;
if (['', './', '.'].indexOf(localPath) === -1 && !file.startsWith(localPath)) return false;
if (ignore.length && micromatch.isMatch(file, ignore)) return false;
return true;
}
const replacePath = file => {
if (localPath == '') return file;
const start = new RegExp('^' + localPath + '/');
return file.replace(start, '');
}
const filteredModified = modified.split("\n").filter(filterFile).map(replacePath);
const filteredDeleted = deleted.split("\n").filter(filterFile).map(replacePath);
if (filteredModified.length === 0 && filteredDeleted.length === 0) {
console.log('No Changes');
}
else {
for (let i = 0; i < filteredDeleted.length; i++) {
const file = filteredDeleted[i];
const remoteFile = remotePath + '/' + file;
const checkRemoteFile = await client.exists(remoteFile);
if ( ! checkRemoteFile) continue;
if (checkRemoteFile == 'd') {
await client.rmdir(remoteFile, true);
}
else {
await client.delete(remoteFile);
}
console.log('Deleted: ' + file);
}
for (let i = 0; i < filteredModified.length; i++) {
const file = filteredModified[i];
const remoteFile = remotePath + '/' + file;
const remoteFilePath = path.dirname(remoteFile);
const checkRemoteFilePath = await client.exists(remoteFilePath);
if (checkRemoteFilePath != 'd') {
if (checkRemoteFilePath) {
console.log('Conflict! it should be directory. Remove file: ' + remoteFilePath);
await client.delete(remoteFilePath);
}
await client.mkdir(remoteFilePath, true);
}
await client.fastPut(file, remoteFile);
console.log('Uploaded: ' + file);
}
}
await client.put(Readable.from(end), remotePath + '/.revision', { mode: 0o644 });
client.end();
} catch(e) {
core.setFailed(e.message);
if (client && connected) client.end();
}
function git() {
return new Promise(async (resolve, reject) => {
try {
let output = '';
let error = '';
await exec.exec('git', Array.from(arguments), {
listeners: {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
error += data.toString();
}
},
silent: false
});
if (error.length) {
return reject(error);
}
resolve(output);
} catch (e) {
reject(e);
}
});
}
// https://stackoverflow.com/a/32516190
function trimChar(s, c) {
if (c === "]") c = "\\]";
if (c === "\\") c = "\\\\";
return s.replace(new RegExp(
"^[" + c + "]+|[" + c + "]+$", "g"
), "");
}
})();