/
sync-command.js
123 lines (108 loc) · 4.15 KB
/
sync-command.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
/* global STATUS_TIMEOUT */
var vscode = require('vscode');
var ftpconfig = require('./ftp-config');
var dirpick = require('./dirpick');
var path = require('path');
var helper = require('./command-helper');
module.exports = function(isUpload, getSyncHelper) {
if(!ftpconfig.validateConfig())
return;
var showSyncSummary = function(sync, options) {
var syncJson = JSON.stringify(sync, null, 4);
var filePath = path.normalize(vscode.workspace.rootPath + "/.vscode/sync-summary-" + Math.floor(Date.now() / 1000) + ".json");
var uri = vscode.Uri.parse("untitled:" + filePath);
var prepareSyncDocument = vscode.workspace.openTextDocument(uri);
prepareSyncDocument.then(function(document) {
var showSyncDocument = vscode.window.showTextDocument(document);
showSyncDocument.then(function() {
var edit = vscode.window.activeTextEditor.edit(function(editBuilder) {
editBuilder.delete(new vscode.Range(
new vscode.Position(0,0),
new vscode.Position(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
));
});
edit.then(function () {
vscode.window.activeTextEditor.edit(function (editBuilder) {
editBuilder.insert(new vscode.Position(0, 0), syncJson);
});
});
helper.getStore(vscode.window.activeTextEditor.document).syncOptions = options;
}, function(err) {
vscode.window.showErrorMessage("Ftp-sync: sync error: " + err)
})
}, function(err) {
vscode.window.showErrorMessage("Ftp-sync: sync error: " + err)
});
}
var prepareProgressMessage;
getSyncHelper().onPrepareRemoteProgress(function(path) {
if(prepareProgressMessage) prepareProgressMessage.dispose();
prepareProgressMessage = vscode.window.setStatusBarMessage("Ftp-sync: collecting remote files list (" + path + ")");
});
getSyncHelper().onPrepareLocalProgress(function(path) {
if(prepareProgressMessage) prepareProgressMessage.dispose();
prepareProgressMessage = vscode.window.setStatusBarMessage("Ftp-sync: collecting local files list (" + path + ")");
});
var prepareSync = function(options) {
var syncMessage = vscode.window.setStatusBarMessage("Ftp-sync: sync prepare in progress...");
getSyncHelper().prepareSync(options, function(err, sync) {
syncMessage.dispose();
if(prepareProgressMessage) prepareProgressMessage.dispose();
if(err) vscode.window.showErrorMessage("Ftp-sync: sync error: " + err);
else {
var pickOptions = [{
label: "Run",
description: "Run all " + getSyncHelper().totalOperations(sync) + " operations now",
operation: "run"
}, {
label: "Review",
description: "Let me review and change operations list",
operation: "review"
}, {
label: "Cancel",
description: "I've changed my mind, cancel sync"
}];
var pickResult = vscode.window.showQuickPick(pickOptions, {
placeHolder: "There are " + getSyncHelper().totalOperations(sync) + " operations to perform"
});
pickResult.then(function(result) {
if(result && result.operation == "run")
helper.executeSync(getSyncHelper(), sync, options)
else if(result && result.operation == "review")
showSyncSummary(sync, options);
})
}
});
}
var prepareoptions = function(dirPath) {
var pickResult = vscode.window.showQuickPick([{
label: "Full-sync",
description: "Removes orphan files on " + (isUpload ? "remote" : "local"),
mode: "full"
}, {
label: "Safe-sync",
description: "Don't remove orphan files on " + (isUpload ? "remote" : "local"),
mode: "safe"
}, {
label: isUpload ? "Force-upload" : "Force download",
description: (isUpload ? "Uploads" : "Downloads") + " files, no matter changed or not",
mode: "force"
}], { placeHolder: "How do you like to sync your files?" });
pickResult.then(function(result) {
if(!result) return;
var syncOptions = {
remotePath: path.join(getSyncHelper().getConfig().remote, dirPath),
localPath: path.join(vscode.workspace.rootPath, dirPath),
upload: isUpload,
mode: result.mode
};
prepareSync(syncOptions);
})
}
var syncDir = function(dirPath) {
if(dirPath) {
prepareoptions(dirPath);
}
}
dirpick(syncDir);
}