From 55902c3b56c6573253e56012af5742290da027e2 Mon Sep 17 00:00:00 2001 From: Marcel Kloubert Date: Fri, 16 Jun 2017 18:33:23 +0200 Subject: [PATCH] can also use SFTP chmod for directories now --- CHANGELOG.md | 3 +- package.json | 7 +++- src/plugins/sftp.ts | 79 +++++++++++++++++++++++++++++++-------------- 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe82648..fe618dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # Change Log (vs-deploy) -## 9.26.0 (June 16th, 2017; SFTP) +## 9.26.1 (June 16th, 2017; SFTP) +* added `updateModesOfDirectories` setting for [sftp targets](https://github.com/mkloubert/vs-deploy/wiki/target_sftp), which indicates if a value in `modes` property should also be applied to the directory of a file or not * bugfixes in [sftp target](https://github.com/mkloubert/vs-deploy/wiki/target_sftp) when setting [file mode](https://github.com/mkloubert/vs-deploy/wiki/target_sftp#modes-for-specific-files) after a file has been uploaded ## 9.25.0 (June 12th, 2017; russian translation) diff --git a/package.json b/package.json index af07bb4..a5f221e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vs-deploy", "displayName": "Deploy", "description": "Commands for deploying files of your workspace to a destination.", - "version": "9.26.0", + "version": "9.26.1", "publisher": "mkloubert", "engines": { "vscode": "^1.6.0" @@ -24453,6 +24453,11 @@ "description": "Prompt for a password if not defined.", "default": true }, + "updateModesOfDirectories": { + "type": "boolean", + "description": "Also execute 'chmod' for the directory of a file.", + "default": false + }, "description": { "type": "string", "description": "A description for the target." diff --git a/src/plugins/sftp.ts b/src/plugins/sftp.ts index 40435cb..4b2ef8d 100644 --- a/src/plugins/sftp.ts +++ b/src/plugins/sftp.ts @@ -60,6 +60,7 @@ interface DeployTargetSFTP extends deploy_contracts.TransformableDeployTarget, d uploaded?: SSHCommands; connected?: SSHCommands; closing?: SSHCommands; + updateModesOfDirectories?: boolean; } interface FileToUpload { @@ -516,36 +517,45 @@ class SFtpPlugin extends deploy_objects.DeployPluginWithContextBase let targetFile = toSFTPPath(Path.join(dir, relativeFilePath)); let targetDirectory = toSFTPPath(Path.dirname(targetFile)); - let putOpts: any = {}; - if (!deploy_helpers.isNullOrUndefined(target.modes)) { + let getModeValue = (pathVal: string) => { let mode: number; - let asOctalNumber = (val: any): number => { - if (deploy_helpers.isNullUndefinedOrEmptyString(val)) { - return; - } + if (!deploy_helpers.isNullOrUndefined(target.modes)) { + let asOctalNumber = (val: any): number => { + if (deploy_helpers.isNullUndefinedOrEmptyString(val)) { + return; + } - return parseInt(deploy_helpers.toStringSafe(val).trim(), - 8); - }; - - if ('object' === typeof target.modes) { - for (let p in target.modes) { - let r = new RegExp(p); + return parseInt(deploy_helpers.toStringSafe(val).trim(), + 8); + }; + + if ('object' === typeof target.modes) { + for (let p in target.modes) { + let r = new RegExp(p); - if (r.test(targetFile)) { - mode = asOctalNumber(target.modes[p]); + if (r.test(deploy_helpers.toStringSafe(pathVal))) { + mode = asOctalNumber(target.modes[p]); + } } } - } - else { - // handle as string or number - mode = asOctalNumber(target.modes); + else { + // handle as string or number + mode = asOctalNumber(target.modes); + } } - if (!deploy_helpers.isNullUndefinedOrEmptyString(mode)) { - putOpts['mode'] = mode; + if (deploy_helpers.isNullUndefinedOrEmptyString(mode)) { + mode = undefined; } + + return mode; + }; + + let putOpts: any = {}; + putOpts['mode'] = getModeValue(targetFile); + if (deploy_helpers.toBooleanSafe(target.updateModesOfDirectories)) { + putOpts['dirMode'] = getModeValue(targetDirectory); } // upload the file @@ -776,10 +786,31 @@ class SFtpPlugin extends deploy_objects.DeployPluginWithContextBase }); } + let changeModForDirectory = (initDirCache?: boolean) => { + if (ctx.hasCancelled) { + completed(); // cancellation requested + return; + } + + if (deploy_helpers.isNullUndefinedOrEmptyString(putOpts['dirMode'])) { + uploadFile(initDirCache); + } + else { + ctx.connection.sftp.chmod(targetDirectory, putOpts['dirMode'], (err) => { + if (err) { + completed(err); + } + else { + uploadFile(initDirCache); + } + }); + } + }; + if (deploy_helpers.isNullOrUndefined(ctx.cachedRemoteDirectories[targetDirectory])) { // first check if target directory exists ctx.connection.list(targetDirectory).then(() => { - uploadFile(true); + changeModForDirectory(true); }).catch((err) => { // no => try to create @@ -789,14 +820,14 @@ class SFtpPlugin extends deploy_objects.DeployPluginWithContextBase } ctx.connection.mkdir(targetDirectory, true).then(() => { - uploadFile(true); + changeModForDirectory(true); }).catch((err) => { completed(err); }); }); } else { - uploadFile(); + changeModForDirectory(); } } }