Skip to content

Commit

Permalink
Merge pull request #268 from NajSurf/master
Browse files Browse the repository at this point in the history
uploadOnSave fix
  • Loading branch information
NajSurf authored Dec 6, 2018
2 parents 8cc68b9 + 32a44c4 commit 9952110
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 85 deletions.
Binary file modified .DS_Store
Binary file not shown.
10 changes: 6 additions & 4 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var vscode = require('vscode')

global.STATUS_TIMEOUT = 3000;

Expand Down Expand Up @@ -37,24 +37,26 @@ function activate(context) {
var currentConfig = getSyncHelper().getConfig();
if (currentConfig.generatedFiles.uploadOnSave) {
fsw = vscode.workspace.createFileSystemWatcher( currentConfig.getGeneratedDir() + '/**');
fsw.onDidChange( function(ev) {
fsw.onDidChange(function(ev) {
//an attempt to normalize onDidChange with onDidSaveTextDocument.
ev['uri'] = {fsPath: ev.fsPath};
onSave(ev, getSyncHelper);
})
fsw.onDidCreate( function(ev) {
fsw.onDidCreate(function(ev) {
ev['uri'] = {fsPath: ev.fsPath};
onSave(ev, getSyncHelper);
})
fsw.onDidDelete( function(ev) {
fsw.onDidDelete(function(ev) {
ev['uri'] = {fsPath: ev.fsPath};
onSave(ev, getSyncHelper);
})
}

vscode.workspace.onDidSaveTextDocument( function(file) {
onSave(file, getSyncHelper);
});


context.subscriptions.push(initCommand);
context.subscriptions.push(syncCommand);
context.subscriptions.push(downloadCommand);
Expand Down
2 changes: 1 addition & 1 deletion modules/dirpick.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var dirpick = function(callback, currentDir) {
var selectListPrefix = currentDir != "." ? ".." : "";
var options = [{ label: path.join(selectListPrefix, path.basename(currentDir)), description: "Choose this folder"}];

getDirectories(path.join(ftpconfig.rootPath(), currentDir)).forEach(function(dir) {
getDirectories(path.join(ftpconfig.rootPath().fsPath, currentDir)).forEach(function(dir) {
options.push(path.join(selectListPrefix, path.basename(currentDir), dir));
})

Expand Down
6 changes: 3 additions & 3 deletions modules/downloadcurrent-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ module.exports = function(fileUrl, getFtpSync) {
return;
}

if (!ftpconfig.rootPath()) {
if (!ftpconfig.rootPath().fsPath) {
vscode.window.showErrorMessage("Ftp-sync: Cannot init ftp-sync without opened folder");
return;
}

if (filePath.indexOf(ftpconfig.rootPath()) < 0) {
if (filePath.indexOf(ftpconfig.rootPath().fsPath) < 0) {
vscode.window.showErrorMessage("Ftp-sync: Selected file is not a part of the workspace.");
return;
}
Expand All @@ -36,7 +36,7 @@ module.exports = function(fileUrl, getFtpSync) {

var fileName = path.basename(filePath);
var downloadStatus = vscode.window.setStatusBarMessage("Ftp-sync: Downloading " + fileName + " from FTP server...", STATUS_TIMEOUT);
getFtpSync().downloadFile(filePath, ftpconfig.rootPath(), function(err) {
getFtpSync().downloadFile(filePath, ftpconfig.rootPath().fsPath, function(err) {
downloadStatus.dispose();
if (err)
vscode.window.showErrorMessage("Ftp-sync: Downloading " + fileName + " failed: " + err);
Expand Down
9 changes: 5 additions & 4 deletions modules/ftp-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var upath = require("upath");

module.exports = {
rootPath: function() {
return vscode.workspace.workspaceFolders[0].uri.fsPath;
return vscode.workspace.workspaceFolders[0].uri;
},
getConfigPath: function() {
return this.getConfigDir() + "/ftp-sync.json";
},
getConfigDir: function() {
return this.rootPath() + "/.vscode";
return this.rootPath().fsPath + "/.vscode";
},
getGeneratedDir: function() {
return upath.join(this.rootPath(), this.generatedFiles.path);
return upath.join(this.rootPath().fsPath, this.generatedFiles.path);
},
defaultConfig: {
remotePath: "./",
Expand Down Expand Up @@ -86,7 +86,8 @@ module.exports = {
passphrase: config.passphrase,
agent: config.agent,
generatedFiles: config.generatedFiles,
debug: config.debug
debug: config.debug,
rootPath: this.rootPath
}
},
connectionChanged: function(oldConfig) {
Expand Down
6 changes: 3 additions & 3 deletions modules/ftp-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = function() {
var self = this;

const Ftp = require('ftp');
let ftp = new Ftp();
const fs = require('fs');
var Ftp = require('ftp');
var ftp = new Ftp();
var fs = require('fs');

self.connect = function(ftpConfig) {
ftp.connect(ftpConfig);
Expand Down
2 changes: 1 addition & 1 deletion modules/init-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');

module.exports = function() {

if(!ftpconfig.rootPath()) {
if(!ftpconfig.rootPath().fsPath) {
vscode.window.showErrorMessage("Ftp-sync: Cannot init ftp-sync without opened folder");
return;
}
Expand Down
8 changes: 4 additions & 4 deletions modules/list-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ module.exports = function(fileUrl, getFtpSync) {
return;
}

if (!ftpconfig.rootPath()) {
if (!ftpconfig.rootPath().fsPath) {
vscode.window.showErrorMessage('Ftp-sync: Cannot init ftp-sync without opened folder');
return;
}

if (filePath.indexOf(ftpconfig.rootPath()) < 0) {
if (filePath.indexOf(ftpconfig.rootPath().fsPath) < 0) {
vscode.window.showErrorMessage('Ftp-sync: Selected file is not a part of the workspace.');
return;
}
Expand All @@ -36,7 +36,7 @@ module.exports = function(fileUrl, getFtpSync) {
return;
}

let remotePath = getFatherPath(filePath.replace(ftpconfig.rootPath(), config.remotePath));
let remotePath = getFatherPath(filePath.replace(ftpconfig.rootPath().fsPath, config.remotePath));
function listAllFiles(filesRemotePath) {
getFtpSync().ListRemoteFilesByPath(filesRemotePath, function(err, files) {
if (err) {
Expand Down Expand Up @@ -136,7 +136,7 @@ function getLabel(file) {
}
function getLocalPath(fileRemotePath) {
return {
fsPath: fileRemotePath.replace(ftpconfig.getConfig().remotePath, ftpconfig.rootPath())
fsPath: fileRemotePath.replace(ftpconfig.getConfig().remotePath, ftpconfig.rootPath().fsPath)
};
}
function getFatherPath(son) {
Expand Down
4 changes: 2 additions & 2 deletions modules/on-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var upath = require("upath");

module.exports = function(document, getFtpSync, skipOnSaveCheck) {

if(document.uri.fsPath.indexOf(ftpconfig.rootPath()) < 0)
if(document.uri.fsPath.indexOf(ftpconfig.rootPath().fsPath) < 0)
return;

var config = ftpconfig.getConfig();
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = function(document, getFtpSync, skipOnSaveCheck) {
var fileName = path.basename(document.uri.fsPath);
var uploadingStatus = vscode.window.setStatusBarMessage("Ftp-sync: Uploading " + fileName + " to FTP server...");

getFtpSync().uploadFile(document.uri.fsPath, ftpconfig.rootPath(), function(err) {
getFtpSync().uploadFile(document.uri.fsPath, ftpconfig.rootPath().fsPath, function(err) {
uploadingStatus.dispose();
if(err)
vscode.window.showErrorMessage("Ftp-sync: Uploading " + fileName + " failed: " + err);
Expand Down
4 changes: 2 additions & 2 deletions modules/scp-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ module.exports = function () {
client.scp(local, Object.assign({}, config, { path: remote }), callback)
}

self.mkdir = function (remote, callback, recursive = false) {
client.mkdir(remote, recursive, callback);
self.mkdir = function (remote, callback) {
client.mkdir(remote, callback);
}

self.delete = function (remote, callback) {
Expand Down
6 changes: 3 additions & 3 deletions modules/sync-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function(isUpload, getSyncHelper) {

var showSyncSummary = function(sync, options) {
var syncJson = JSON.stringify(sync, null, 4);
var filePath = path.normalize(ftpconfig.rootPath() + "/.vscode/sync-summary-" + Math.floor(Date.now() / 1000) + ".json");
var filePath = path.normalize(ftpconfig.rootPath().fsPath + "/.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) {
Expand Down Expand Up @@ -103,8 +103,8 @@ module.exports = function(isUpload, getSyncHelper) {
if(!result) return;
var syncOptions = {
remotePath: path.join(getSyncHelper().getConfig().remote, dirPath),
localPath: path.join(ftpconfig.rootPath(), dirPath),
rootPath: ftpconfig.rootPath(),
localPath: path.join(ftpconfig.rootPath().fsPath, dirPath),
rootPath: ftpconfig.rootPath().fsPath,
upload: isUpload,
mode: result.mode
};
Expand Down
110 changes: 56 additions & 54 deletions modules/sync-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const listOneDeepRemoteFiles = function(remotePath, callback) {
});
}
});
const finish = () => {
const finish = function() {
result.forEach(function(item) {
if (_.startsWith(item.name, remotePath)) {
item.path = item.name;
Expand All @@ -198,7 +198,7 @@ const listOneDeepRemoteFiles = function(remotePath, callback) {
};
// the entry of list request
const ListRemoteFilesByPath = function(remotePath, callback) {
connect(err => {
connect(function(err) {
if (err) {
callback(err);
return;
Expand All @@ -208,7 +208,7 @@ const ListRemoteFilesByPath = function(remotePath, callback) {
};
const deleteRemoteFile = function(remoteFilePath) {
return new Promise(function(resolve, reject) {
connect(err => {
connect(function(err) {
if (err) {
reject(err);
return;
Expand All @@ -233,53 +233,53 @@ var listLocalFiles = function(localPath, rootPath, callback, options) {

var files = [];

if (localPath != rootPath) {
fswalk.dirs(
localPath,
function(basedir, filename, stat, next) {
var dirPath = path.join(basedir, filename);
if (isIgnored(dirPath, ftpConfig.allow, ftpConfig.ignore))
return next();
dirPath = dirPath.replace(localPath, "");
dirPath = upath.toUnix(dirPath);
if (dirPath[0] == "/") dirPath = dirPath.substr(1);
if (onPrepareLocalProgress) onPrepareLocalProgress(dirPath);
files.push({
name: dirPath,
size: stat.size,
isDir: stat.isDirectory()
});
next();
},
function(err) {
callback(err, files);
}
);
fswalk.files(
localPath,
function(basedir, filename, stat, next) {
var filePath = path.join(basedir, filename);
//when listing localFiles by onPrepareLocalProgress, ignore localfile
if (isIgnored(filePath, ftpConfig.allow, ftpConfig.ignore))
return next();
filePath = filePath.replace(localPath, "");
filePath = upath.toUnix(filePath);
if (filePath[0] == "/") filePath = filePath.substr(1);

if (onPrepareLocalProgress) onPrepareLocalProgress(filePath);
files.push({
name: filePath,
size: stat.size,
isDir: stat.isDirectory()
});
next();
},
function(err) {
callback(err, files);
}
);
}
if (localPath === rootPath) {
// if (localPath != rootPath) {
// fswalk.dirs(
// localPath,
// function(basedir, filename, stat, next) {
// var dirPath = path.join(basedir, filename);
// if (isIgnored(dirPath, ftpConfig.allow, ftpConfig.ignore))
// return next();
// dirPath = dirPath.replace(localPath, "");
// dirPath = upath.toUnix(dirPath);
// if (dirPath[0] == "/") dirPath = dirPath.substr(1);
// if (onPrepareLocalProgress) onPrepareLocalProgress(dirPath);
// files.push({
// name: dirPath,
// size: stat.size,
// isDir: stat.isDirectory()
// });
// next();
// },
// function(err) {
// callback(err, files);
// }
// );
// fswalk.files(
// localPath,
// function(basedir, filename, stat, next) {
// var filePath = path.join(basedir, filename);
// //when listing localFiles by onPrepareLocalProgress, ignore localfile
// if (isIgnored(filePath, ftpConfig.allow, ftpConfig.ignore))
// return next();
// filePath = filePath.replace(localPath, "");
// filePath = upath.toUnix(filePath);
// if (filePath[0] == "/") filePath = filePath.substr(1);

// if (onPrepareLocalProgress) onPrepareLocalProgress(filePath);
// files.push({
// name: filePath,
// size: stat.size,
// isDir: stat.isDirectory()
// });
// next();
// },
// function(err) {
// callback(err, files);
// }
// );
// }
// if (localPath === rootPath) {
fswalk.walk(
localPath,
function(basedir, filename, stat, next) {
Expand All @@ -304,7 +304,7 @@ var listLocalFiles = function(localPath, rootPath, callback, options) {
}
);
}
};
//};

var prepareSyncObject = function(remoteFiles, localFiles, options, callback) {
var from = options.upload ? localFiles : remoteFiles;
Expand Down Expand Up @@ -351,9 +351,9 @@ var prepareSyncObject = function(remoteFiles, localFiles, options, callback) {
});

if (options.mode == "full")
to.filter(toFile => {
to.filter(function(toFile) {
return !toFile.wasOnFrom;
}).forEach(toFile => {
}).forEach(function(toFile) {
if (toFile.isDir) dirsToRemove.push(toFile.name);
else filesToRemove.push(toFile.name);
});
Expand Down Expand Up @@ -582,7 +582,9 @@ var ensureDirExists = function(remoteDir, callback) {
ensureDirExists(path.posix.join(remoteDir, ".."), function() {
ensureDirExists(remoteDir, callback);
});
} else if (_.some(list, f => f.name == path.basename(remoteDir))) {
} else if (_.some(list, f => {
return f.name == path.basename(remoteDir);
})) {
callback();
} else {
ftp.mkdir(
Expand Down
6 changes: 3 additions & 3 deletions modules/uploadcurrent-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module.exports = function(fileUrl, getFtpSync) {
return;
}

if(!ftpconfig.rootPath()) {
if(!ftpconfig.rootPath().fsPath) {
vscode.window.showErrorMessage("Ftp-sync: Cannot init ftp-sync without opened folder");
return;
}

if(filePath.indexOf(ftpconfig.rootPath()) < 0) {
if(filePath.indexOf(ftpconfig.rootPath().fsPath) < 0) {
vscode.window.showErrorMessage("Ftp-sync: Selected file is not a part of the workspace.");
return;
}
Expand All @@ -36,7 +36,7 @@ module.exports = function(fileUrl, getFtpSync) {
var fileName = path.basename(filePath);
var uploadingStatus = vscode.window.setStatusBarMessage("Ftp-sync: Uploading " + fileName + " to FTP server...", STATUS_TIMEOUT);

getFtpSync().uploadFile(filePath, ftpconfig.rootPath(), function(err) {
getFtpSync().uploadFile(filePath, ftpconfig.rootPath().fsPath, function(err) {
uploadingStatus.dispose();
if(err)
vscode.window.showErrorMessage("Ftp-sync: Uploading " + fileName + " failed: " + err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ftp-sync",
"description": "Auto sync your work to remote FTP/SFTP server",
"version": "0.3.6",
"version": "0.3.7",
"publisher": "lukasz-wronski",
"engines": {
"vscode": "^0.10.1"
Expand Down

0 comments on commit 9952110

Please sign in to comment.